|
| 1 | +<!-- |
| 2 | +# @title Testing Functions |
| 3 | +--> |
| 4 | + |
| 5 | +# Testing Functions |
| 6 | + |
| 7 | +This guide covers writing unit tests for functions using the Functions Framework |
| 8 | +for Node.js. |
| 9 | + |
| 10 | +## Overview of function testing |
| 11 | + |
| 12 | +One of the benefits of the functions-as-a-service paradigm is that functions are |
| 13 | +easy to test. In many cases, you can simply call a function with input, and test |
| 14 | +the output. You do not need to set up (or mock) an actual server. |
| 15 | + |
| 16 | +The Functions Framework provides utility methods that streamline the process of |
| 17 | +setting up functions and the environment for testing, constructing input |
| 18 | +parameters, and interpreting results. These are available in the |
| 19 | +`@google-cloud/functions-framework/testing` module. |
| 20 | + |
| 21 | +## Loading functions for testing |
| 22 | + |
| 23 | +The easiest way to get started unit testing Node.js Cloud Functions is to explicitly |
| 24 | +export the functions you wish to unit test. |
| 25 | + |
| 26 | +```js |
| 27 | +// hello_tests.js |
| 28 | +import * as functions from '@google-cloud/functions-framework'; |
| 29 | + |
| 30 | +// declare a cloud function and export it so that it can be |
| 31 | +// imported in unit tests |
| 32 | +export const HelloTests = (req, res) => { |
| 33 | + res.send('Hello, World!'); |
| 34 | +}; |
| 35 | + |
| 36 | +// register the HelloTests with the Functions Framework |
| 37 | +functions.http('HelloTests', HelloTests); |
| 38 | +``` |
| 39 | + |
| 40 | +This is a perfectly acceptable approach that allows you to keep your application |
| 41 | +code decoupled from the Functions Framework, but it also has some drawbacks. |
| 42 | +You won't automatically benefit from the implicit type hints and autocompletion |
| 43 | +that are available when you pass a callback to `functions.http` directly: |
| 44 | + |
| 45 | +```js |
| 46 | +// hello_tests.js |
| 47 | +import * as functions from '@google-cloud/functions-framework'; |
| 48 | + |
| 49 | +// register the HelloTests with the Functions Framework |
| 50 | +functions.http('HelloTests', (req, res) => { |
| 51 | + // req and res are strongly typed here |
| 52 | +}); |
| 53 | +``` |
| 54 | + |
| 55 | +The testing module provides a `getFunction` helper method that can be used to |
| 56 | +access a function that was registered with the Functions Framework. To use it in |
| 57 | +your unit test you must first load the module that registers the function you wish |
| 58 | +to test. |
| 59 | + |
| 60 | +```js |
| 61 | +import {getFunction} from "@google-cloud/functions-framework/testing"; |
| 62 | + |
| 63 | +describe("HelloTests", () => { |
| 64 | + before(async () => { |
| 65 | + // load the module that defines HelloTests |
| 66 | + await import("./hello_tests.js"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("is testable", () => { |
| 70 | + // get the function using the name it was registered with |
| 71 | + const HelloTest = getFunction("HelloTests"); |
| 72 | + // ... |
| 73 | + }); |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +## Testing HTTP functions |
| 78 | + |
| 79 | +Testing an HTTP function is generally as simple as generating a request, calling |
| 80 | +the function, and asserting against the response. |
| 81 | + |
| 82 | +HTTP functions are passed an [express.Request](https://expressjs.com/en/api.html#req) |
| 83 | +and an [express.Response](https://expressjs.com/en/api.html#res) as arguments. You can |
| 84 | +create simple stubs to use in unit tests. |
| 85 | + |
| 86 | +```js |
| 87 | +import assert from "assert"; |
| 88 | +import {getFunction} from "@google-cloud/functions-framework/testing"; |
| 89 | + |
| 90 | +describe("HelloTests", () => { |
| 91 | + before(async () => { |
| 92 | + // load the module that defines HelloTests |
| 93 | + await import("./hello_tests.js"); |
| 94 | + }); |
| 95 | + |
| 96 | + it("is testable", () => { |
| 97 | + // get the function using the name it was registered with |
| 98 | + const HelloTest = getFunction("HelloTests"); |
| 99 | + |
| 100 | + // a Request stub with a simple JSON payload |
| 101 | + const req = { |
| 102 | + body: { foo: "bar" }, |
| 103 | + }; |
| 104 | + // a Response stub that captures the sent response |
| 105 | + let result; |
| 106 | + const res = { |
| 107 | + send: (x) => { |
| 108 | + result = x; |
| 109 | + }, |
| 110 | + }; |
| 111 | + |
| 112 | + // invoke the function |
| 113 | + HelloTest(req, res); |
| 114 | + |
| 115 | + // assert the response matches the expected value |
| 116 | + assert.equal(result, "Hello, World!"); |
| 117 | + }); |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +## Testing CloudEvent functions |
| 122 | + |
| 123 | +Testing a CloudEvent function works similarly. The |
| 124 | +[JavaScript SDK for CloudEvents](https://github.com/cloudevents/sdk-javascript) provides |
| 125 | +APIs to create stub CloudEvent objects for use in tests. |
| 126 | + |
| 127 | +Unlike HTTP functions, event functions do not accept a response argument. Instead, you |
| 128 | +will need to test side effects. A common approach is to replace your function's |
| 129 | +dependencies with mock objects that can be used to verify its behavior. The |
| 130 | +[sinonjs](https://sinonjs.org/) is a standalone library for creating mocks that work with |
| 131 | +any Javascript testing framework: |
| 132 | + |
| 133 | +```js |
| 134 | +import assert from "assert"; |
| 135 | +import sinon from "sinon"; |
| 136 | +import {CloudEvent} from "cloudevents"; |
| 137 | +import {getFunction} from "@google-cloud/functions-framework/testing"; |
| 138 | + |
| 139 | +import {MyDependency} from "./my_dependency.js"; |
| 140 | + |
| 141 | +describe("HelloCloudEvent", () => { |
| 142 | + before(async () => { |
| 143 | + // load the module that defines HelloCloudEvent |
| 144 | + await import("./hello_cloud_event.js"); |
| 145 | + }); |
| 146 | + |
| 147 | + const sandbox = sinon.createSandbox(); |
| 148 | + |
| 149 | + beforeEach(() => { |
| 150 | + sandbox.spy(MyDependency); |
| 151 | + }); |
| 152 | + |
| 153 | + afterEach(() => { |
| 154 | + sandbox.restore(); |
| 155 | + }); |
| 156 | + |
| 157 | + it("uses MyDependency", () => { |
| 158 | + const HelloCloudEvent = getFunction("HelloCloudEvent"); |
| 159 | + HelloCloudEvent(new CloudEvent({ |
| 160 | + type: 'com.google.cloud.functions.test', |
| 161 | + source: 'https://github.com/GoogleCloudPlatform/functions-framework-nodejs', |
| 162 | + })); |
| 163 | + // assert that the cloud function invoked `MyDependency.someMethod()` |
| 164 | + assert(MyDependency.someMethod.calledOnce); |
| 165 | + }); |
| 166 | +}); |
| 167 | +``` |
| 168 | + |
| 169 | +## Integration testing with SuperTest |
| 170 | + |
| 171 | +The `testing` module also includes utilities that help you write high-level, integration |
| 172 | +tests to verify the behavior of the Functions Framework HTTP server that invokes your function |
| 173 | +to respond to requests. The [SuperTest](https://github.com/visionmedia/supertest) library |
| 174 | +provides a developer friendly API for writing HTTP integration tests in javascript. The |
| 175 | +`testing` module includes a `getTestServer` helper to help you test your functions using |
| 176 | +SuperTest. |
| 177 | + |
| 178 | +```js |
| 179 | +import supertest from 'supertest'; |
| 180 | +import {getTestServer} from '@google-cloud/functions-framework/testing'; |
| 181 | + |
| 182 | +describe("HelloTests", function () { |
| 183 | + before(async () => { |
| 184 | + // load the module that defines HelloTests |
| 185 | + await import("./hello_tests.js"); |
| 186 | + }); |
| 187 | + |
| 188 | + it("uses works with SuperTest", async () => { |
| 189 | + // call getTestServer with the name of function you wish to test |
| 190 | + const server = getTestServer("HelloTests"); |
| 191 | + |
| 192 | + // invoke HelloTests with SuperTest |
| 193 | + await supertest(server) |
| 194 | + .post("/") |
| 195 | + .send({ some: "payload" }) |
| 196 | + .set("Content-Type", "application/json") |
| 197 | + .expect("Hello, World!") |
| 198 | + .expect(200); |
| 199 | + }); |
| 200 | +}); |
| 201 | +``` |
0 commit comments