Skip to content

Commit

Permalink
refactor(functions/tips): ava to mocha (#1241)
Browse files Browse the repository at this point in the history
  • Loading branch information
AVaksman authored and fhinkel committed Apr 6, 2019
1 parent f430af0 commit bb8dadc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
5 changes: 4 additions & 1 deletion functions/tips/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
32 changes: 19 additions & 13 deletions functions/tips/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', done => {
// Retry by throwing an error
t.throws(() => {
assert.throws(() => {
sample.retryPromise({
data: {
retry: true,
Expand All @@ -35,10 +34,16 @@ 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...');
sample.retryPromise({data: {}}).then(
() => {},
error => {
assert.strictEqual(error.message, 'Not retrying...');
done();
}
);
});

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!');

Expand All @@ -51,14 +56,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,
Expand All @@ -76,6 +82,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));
});

0 comments on commit bb8dadc

Please sign in to comment.