From a186d11e4eee525c018712c648c529c8137ade5c Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 3 Apr 2019 14:49:07 -0400 Subject: [PATCH 1/2] functions/tips tests to mocha add script to run tests --- functions/tips/package.json | 5 ++++- functions/tips/test/index.test.js | 31 ++++++++++++++++++------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/functions/tips/package.json b/functions/tips/package.json index 26560528df..6f883de045 100644 --- a/functions/tips/package.json +++ b/functions/tips/package.json @@ -11,13 +11,16 @@ "engines": { "node": ">=8" }, + "scripts": { + "test": "mocha test/*.test.js --timeout=60000 --exit" + }, "dependencies": { "@google-cloud/pubsub": "^0.22.2", "safe-buffer": "^5.1.2" }, "devDependencies": { "@google-cloud/nodejs-repo-tools": "^3.0.0", - "ava": "^0.25.0", + "mocha": "^6.0.0", "sinon": "^7.0.0" }, "cloud-repo-tools": { diff --git a/functions/tips/test/index.test.js b/functions/tips/test/index.test.js index 2c460a05d3..efd07198e9 100644 --- a/functions/tips/test/index.test.js +++ b/functions/tips/test/index.test.js @@ -16,17 +16,16 @@ 'use strict'; const sinon = require(`sinon`); -const test = require(`ava`); +const assert = require(`assert`); const tools = require(`@google-cloud/nodejs-repo-tools`); const sample = require(`../`); +beforeEach(tools.stubConsole); +afterEach(tools.restoreConsole); -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); - -test(`should demonstrate retry behavior for a promise`, async t => { +it('should demonstrate retry behavior for a promise', async () => { // Retry by throwing an error - t.throws(() => { + assert.throws(() => { sample.retryPromise({ data: { retry: true, @@ -35,10 +34,15 @@ test(`should demonstrate retry behavior for a promise`, async t => { }, 'Retrying...'); // Terminate by returning a rejected promise - await t.throws(sample.retryPromise({data: {}}), 'Not retrying...'); + await sample + .retryPromise({data: {}}) + .then( + () => {}, + error => assert.strictEqual(error.message, 'Not retrying...') + ); }); -test(`should demonstrate retry behavior for a callback`, t => { +it('should demonstrate retry behavior for a callback', done => { const cb = sinon.stub(); const err = new Error('Error!'); @@ -51,14 +55,15 @@ test(`should demonstrate retry behavior for a callback`, t => { }, cb ); - t.deepEqual(cb.firstCall.args, [err]); + assert.deepStrictEqual(cb.firstCall.args, [err]); // Terminate by passing nothing to the callback sample.retryCallback({data: {}}, cb); - t.deepEqual(cb.secondCall.args, []); + assert.deepStrictEqual(cb.secondCall.args, []); + done(); }); -test(`should call a GCP API`, async t => { +it('should call a GCP API', async () => { const reqMock = { body: { topic: process.env.FUNCTIONS_TOPIC, @@ -76,6 +81,6 @@ test(`should call a GCP API`, async t => { // use a delay here and keep the sample idiomatic await new Promise(resolve => setTimeout(resolve, 1000)); - t.true(resMock.status.calledOnce); - t.true(resMock.status.calledWith(200)); + assert.ok(resMock.status.calledOnce); + assert.ok(resMock.status.calledWith(200)); }); From d021ed2056b8c6169f1054aca6aa9e0cc4246868 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 3 Apr 2019 15:06:43 -0400 Subject: [PATCH 2/2] fixup --- functions/tips/test/index.test.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/functions/tips/test/index.test.js b/functions/tips/test/index.test.js index efd07198e9..4fd5d1211c 100644 --- a/functions/tips/test/index.test.js +++ b/functions/tips/test/index.test.js @@ -23,7 +23,7 @@ const sample = require(`../`); beforeEach(tools.stubConsole); afterEach(tools.restoreConsole); -it('should demonstrate retry behavior for a promise', async () => { +it('should demonstrate retry behavior for a promise', done => { // Retry by throwing an error assert.throws(() => { sample.retryPromise({ @@ -34,12 +34,13 @@ it('should demonstrate retry behavior for a promise', async () => { }, 'Retrying...'); // Terminate by returning a rejected promise - await sample - .retryPromise({data: {}}) - .then( - () => {}, - error => assert.strictEqual(error.message, 'Not retrying...') - ); + sample.retryPromise({data: {}}).then( + () => {}, + error => { + assert.strictEqual(error.message, 'Not retrying...'); + done(); + } + ); }); it('should demonstrate retry behavior for a callback', done => {