This repository contains samples that the documentation on Google Cloud
Platform references. Samples for a client library should be added to
the client repository, not this repository. (For example, the functions folder
is reserved for samples used in
cloud.google.com/functions). If you wrote
a great sample but it is not used in Google's official documentation, there are
better suited places to publish it such as a community
tutorial.
-
Obtain authentication credentials. Depending on the sample, you need to enable the appropriate APIs in the Cloud Console.
gcloud auth application-default login -
Change directory to one of the sample folders, e.g.
run/helloworld.cd run/helloworld -
Install the dependencies.
npm install -
Run the tests.
npm test
When a Pull Request is opened, reopened, or has new commits pushed the sample tests (unit, integration, end-to-end) will be run.
If the tests for a sample change do not run, they can be triggered by adding the actions:force-run label.
If tests need to be triggered multiple times, manually remove actions:force-run and then re-add this label.
The automatic clean-up of labels is currently disabled. Please remove the actions:force-run before merging the Pull Request.
All samples must have tests. We use mocha as testing framework. The
package.json file within your sample directory must contain a test script that
executes the mocha tests via npm test
(example).
Contributors are encouraged to use vanilla Node.js as much as pragmatically possible to standardize writing, reviewing, and maintaining samples and their tests, ideally reducing dependencies on third party libraries.
For tests, we recommend using the standard library assert. The library provides a strict and a legacy mode; please use the strict mode as shown below:
const assert = require('node:assert/strict');tl;dr: Any check with
(dev),(experimental), or(legacy)can be ignored and should not block your PR from merging.
This repository uses the 🍮 Custard CI from
GoogleCloudPlatform/cloud-samples-tools
to set up and run tests.
First, it checks which files changed in a PR to find which packages were affected.
In this repo, a package is defined as a directory containing a package.json file.
If a global file (not belonging to a package) is changed, all packages are marked as affected. Global files are can contain repo-level configurations that could affect other packages. Typically, contributors should only modify files in a package.
Only affected packages are linted and tested. For tests, we use a matrix strategy to generate isolated jobs for each affected test.
We're working to improve the testing infrastructure, while keeping tests stable. This means you'll sometimes see some new experimental tests, you can generally ignore them. Or two versions of one test running while we make sure the new version works well.
Here's a list of which tests are required and which ones you can ignore.
Custard CI / lint: (required) runs only for affected packagesCustard CI / tests: (required) this runs until all tests have finishedCustard CI / test (package): (required) runs only for affected packages(dev) Custard CI / test (package): (ignore) this test has errors we're working on(experimental) Custard CI / ...: (ignore) this is a new unstable versionCustard CI / (legacy) ...: (ignore) this is the old version running while we make sure the new one works
This repository also supports TypeScript samples. We use the typeless-sample-bot to convert TypeScript samples to pure JavaScript, which avoids having to maintain both TypeScript and JavaScript variants.
If you choose to write a TypeScript based sample, please follow these guidelines:
- Style:
- See the Google TypeScript Guide. In particular, note to use types wherever possible, except for trivially inferred types that do not aid in readability.
- TypeScript Configuration:
- Include a tsconfig.json in the root of your sample directory.
- It is recommended to set
https://www.typescriptlang.org/tsconfig#noImplicitAny to
false, but it may be needed to set this totrueif you haven't fully migrated the sample to TypeScript. - You can find a minimal example here.
- Include an
excludesentry with your test files to avoid building*.jsversions (which is already in the example).
- Testing: Use a
.tstest runner.- It should be executed by the
npm testcommandmocha --require ts-node/register test/*.tsinpackage.json. - A
ts-nodedependency will need to be added todevDependenciesin your package.json file. - To execute TypeScript samples from your test, use
node --loader ts-node/esm <sample.ts>.
- It should be executed by the
- Imports: Use an
importstatement at the beginning of the file to enable importing types.- Within each commented "region tag," import required libraries with
requiredusing the CommonJS require module loader. (Note that theimportstyle cannot be used anywhere except the top of the file.) - Each of these region tag sections are directly embedded in the Cloud
documentation, so including the imports at the top of each region tag
section shows our users which libraries are needed. They should be imported
using
requireeven if you have already imported the module at the top of the file outside of the region tag. - Some dependencies do not include type definitions. You may see a warning in
your IDE that no types can be found. There are often additional
@typesdependencies, e.g.@types/mochathat provide these. They can be included in thedevDependenciessection of thepackage.json. If you are using imports from Node itself, you may need to also include@types/node.
- Within each commented "region tag," import required libraries with
- Other Recommendations:
- Linting: See the example .eslintrc.yml.
- JavaScript: Do not modify any
.jsfiles. The typeless-sample-bot will overwrite them as it converts from TypeScript into JavaScript. Do not check in any generated.jstests from runningnpm build. package.json: See a full set of npm targets in the scheduler package.json.
You can look at the scheduler sample directory for an example of a TypeScript sample and its matching test runner.
The Google Cloud Samples Style Guide is considered the primary guidelines for all Google Cloud samples.
Samples in this repository also follow the JavaScript coding standards. See instructions below to run the linter to match our JavaScript coding standards:
-
Install dependencies at the root of the
nodejs-docs-samplesdirectory.npm install -
Run the linter for all samples, including the ones you're adding.
npm run lint
Required tests need to pass. Tests that are not required are expected to fail,
they are usually work in progress.