diff --git a/functions/node8/package.json b/functions/node8/package.json index 1d786a3470..665830e98e 100644 --- a/functions/node8/package.json +++ b/functions/node8/package.json @@ -3,8 +3,12 @@ "version": "0.0.1", "description": "Google Cloud Functions samples for Node.js 8", "scripts": { + "pretest": "semistandard *.js test/*.js", + "test": "ava test/*" + }, + "engines": { + "node": "^8" }, - "engines": { "node": "^8"}, "repository": { "type": "git", "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" @@ -13,5 +17,11 @@ "license": "Apache-2.0", "bugs": { "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples/issues" + }, + "dependencies": { + "ava": "^0.25.0" + }, + "devDependencies": { + "semistandard": "^12.0.1" } } diff --git a/functions/node8/test/sample.unit.pubsub.test.js b/functions/node8/test/sample.unit.pubsub.test.js new file mode 100644 index 0000000000..d85f9e55cd --- /dev/null +++ b/functions/node8/test/sample.unit.pubsub.test.js @@ -0,0 +1,44 @@ +/** + * Copyright 2018, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START functions_pubsub_unit_test] +const test = require(`ava`); +const uuid = require(`uuid`); +const sinon = require(`sinon`); + +const helloPubSub = require(`..`).helloPubSub; +const consoleLog = sinon.stub(console, 'log'); + +test(`helloPubSub: should print a name`, async t => { + // Initialize mocks + const name = uuid.v4(); + const event = { + data: Buffer.from(name).toString(`base64`) + }; + + // Call tested function and verify its behavior + await helloPubSub(event); + t.true(consoleLog.calledWith(`Hello, ${name}!`)); +}); + +test(`helloPubSub: should print hello world`, async t => { + // Initialize mocks + const event = {}; + + // Call tested function and verify its behavior + await helloPubSub(event); + t.true(consoleLog.calledWith(`Hello, World!`)); +}); +// [END functions_pubsub_unit_test] diff --git a/functions/node8/test/sample.unit.storage.test.js b/functions/node8/test/sample.unit.storage.test.js new file mode 100644 index 0000000000..595c6dff7f --- /dev/null +++ b/functions/node8/test/sample.unit.storage.test.js @@ -0,0 +1,65 @@ +/** + * Copyright 2018, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START functions_storage_unit_test] +const test = require(`ava`); +const uuid = require(`uuid`); +const sinon = require(`sinon`); + +const helloGCS = require(`..`).helloGCS; +const consoleLog = sinon.stub(console, 'log'); + +test(`helloGCS: should print uploaded message`, async t => { + // Initialize mocks + const filename = uuid.v4(); + const event = { + name: filename, + resourceState: 'exists', + metageneration: '1' + }; + + // Call tested function and verify its behavior + await helloGCS(event); + t.true(consoleLog.calledWith(`File ${filename} uploaded.`)); +}); + +test(`helloGCS: should print metadata updated message`, async t => { + // Initialize mocks + const filename = uuid.v4(); + const event = { + name: filename, + resourceState: 'exists', + metageneration: '2' + }; + + // Call tested function and verify its behavior + await helloGCS(event); + t.true(consoleLog.calledWith(`File ${filename} metadata updated.`)); +}); + +test(`helloGCS: should print deleted message`, async t => { + // Initialize mocks + const filename = uuid.v4(); + const event = { + name: filename, + resourceState: 'not_exists', + metageneration: '3' + }; + + // Call tested function and verify its behavior + await helloGCS(event); + t.true(consoleLog.calledWith(`File ${filename} deleted.`)); +}); +// [END functions_storage_unit_test]