Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(functions/tips): ava to mocha #1241

Merged
merged 4 commits into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
});