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');
For new samples, a GitHub Actions workflow should be created to run your tests on the CI system:
-
Check that your new samples and sample tests are on a branch created directly from this repo
GoogleCloudPlatform/nodejs-docs-samples
. Not a fork. -
Add an entry to .github/workflows/utils/workflows.json matching the directory with your sample code.
-
From the root of the repo, generate a new workflow in the workflows directory. You can specify a
path
to only generate the specific workflow, e.g.cloud-tasks
. If the path is omitted, all workflows will be generated.node .github/workflows/utils/generate.js -s [path]
Note There are some existing samples that use an alternative CI system. It is recommended to use GitHub Actions for new samples, but these instructions are provided below for your reference.
Add a build configuration file (
.cfg
) for your samples in.kokoro/
. Check existing config files for the right format.All tests need a corresponding job file outside of GitHub. If you are a Googler, please provide the CL alongside your PR. See the internal codelab for Kokoro for details. If you don't work at Google, the person reviewing your PR will create the job config for you.
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 totrue
if you haven't fully migrated the sample to TypeScript. - You can find a minimal example here.
- Include an
excludes
entry with your test files to avoid building*.js
versions (which is already in the example).
- Testing: Use a
.ts
test runner.- It should be executed by the
npm test
commandmocha --require ts-node/register test/*.ts
inpackage.json
. - A
ts-node
dependency will need to be added todevDependencies
in 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
import
statement at the beginning of the file to enable importing types.- Within each commented "region tag," import required libraries with
required
using the CommonJS require module loader. (Note that theimport
style 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
require
even 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
@types
dependencies, e.g.@types/mocha
that provide these. They can be included in thedevDependencies
section 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
.js
files. The typeless-sample-bot will overwrite them as it converts from TypeScript into JavaScript. Do not check in any generated.js
tests 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-samples
directory.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.