diff --git a/cloud-sql/postgres/knex/package.json b/cloud-sql/postgres/knex/package.json index f4a2af9bd6..614c4ee92a 100644 --- a/cloud-sql/postgres/knex/package.json +++ b/cloud-sql/postgres/knex/package.json @@ -13,7 +13,7 @@ "node": ">=8" }, "scripts": { - "unit-test": "ava --verbose test/*.test.js", + "unit-test": "mocha test/*.test.js --timeout=60000 --exit", "start-proxy": "! pgrep cloud_sql_proxy > /dev/null && cloud_sql_proxy -dir=/cloudsql -instances=$CLOUD_SQL_INSTANCE_NAME &", "system-test": "repo-tools test app -- server.js", "system-test-proxy": "npm run start-proxy; npm run system-test", @@ -30,8 +30,8 @@ "winston": "^3.1.0" }, "devDependencies": { - "@google-cloud/nodejs-repo-tools": "^3.2.0", - "ava": "^0.25.0", + "@google-cloud/nodejs-repo-tools": "3.2.0", + "mocha": "^6.0.0", "proxyquire": "^2.1.0", "supertest": "^4.0.0", "sinon": "^7.1.1" diff --git a/cloud-sql/postgres/knex/test/createTable.test.js b/cloud-sql/postgres/knex/test/createTable.test.js index 2cfda1654f..8e054a42af 100644 --- a/cloud-sql/postgres/knex/test/createTable.test.js +++ b/cloud-sql/postgres/knex/test/createTable.test.js @@ -15,15 +15,15 @@ 'use strict'; -const test = require(`ava`); -const path = require(`path`); -const proxyquire = require(`proxyquire`).noPreserveCache(); -const sinon = require(`sinon`); -const tools = require(`@google-cloud/nodejs-repo-tools`); +const assert = require('assert'); +const path = require('path'); +const proxyquire = require('proxyquire').noPreserveCache(); +const sinon = require('sinon'); +const tools = require('@google-cloud/nodejs-repo-tools'); -const SAMPLE_PATH = path.join(__dirname, `../createTables.js`); +const SAMPLE_PATH = path.join(__dirname, '../createTable.js'); -const exampleConfig = [`user`, `password`, `database`]; +const exampleConfig = ['user', 'password', 'database']; function getSample() { const configMock = exampleConfig; @@ -58,10 +58,10 @@ function getSample() { }; } -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +beforeEach(tools.stubConsole); +afterEach(tools.restoreConsole); -test.cb.serial(`should create a table`, t => { +it('should create a table', async () => { const sample = getSample(); const expectedResult = `Successfully created 'votes' table.`; @@ -70,30 +70,34 @@ test.cb.serial(`should create a table`, t => { prompt: sample.mocks.prompt, }); - t.true(sample.mocks.prompt.start.calledOnce); - t.true(sample.mocks.prompt.get.calledOnce); - t.deepEqual(sample.mocks.prompt.get.firstCall.args[0], exampleConfig); - - setTimeout(() => { - t.true(sample.mocks.Knex.calledOnce); - t.deepEqual(sample.mocks.Knex.firstCall.args, [ - { - client: 'pg', - connection: exampleConfig, - }, - ]); - - t.true(sample.mocks.knex.schema.createTable.calledOnce); - t.is(sample.mocks.knex.schema.createTable.firstCall.args[0], 'votes'); - - t.true(console.log.calledWith(expectedResult)); - t.true(sample.mocks.knex.destroy.calledOnce); - t.end(); - }, 10); + assert.ok(sample.mocks.prompt.start.calledOnce); + assert.ok(sample.mocks.prompt.get.calledOnce); + assert.deepStrictEqual( + sample.mocks.prompt.get.firstCall.args[0], + exampleConfig + ); + + await new Promise(r => setTimeout(r, 10)); + assert.ok(sample.mocks.Knex.calledOnce); + assert.deepStrictEqual(sample.mocks.Knex.firstCall.args, [ + { + client: 'pg', + connection: exampleConfig, + }, + ]); + + assert.ok(sample.mocks.knex.schema.createTable.calledOnce); + assert.strictEqual( + sample.mocks.knex.schema.createTable.firstCall.args[0], + 'votes' + ); + + assert.ok(console.log.calledWith(expectedResult)); + assert.ok(sample.mocks.knex.destroy.calledOnce); }); -test.cb.serial(`should handle prompt error`, t => { - const error = new Error(`error`); +it('should handle prompt error', async () => { + const error = new Error('error'); const sample = getSample(); sample.mocks.prompt.get = sinon.stub().yields(error); @@ -102,16 +106,14 @@ test.cb.serial(`should handle prompt error`, t => { prompt: sample.mocks.prompt, }); - setTimeout(() => { - t.true(console.error.calledOnce); - t.true(console.error.calledWith(error)); - t.true(sample.mocks.Knex.notCalled); - t.end(); - }, 10); + await new Promise(r => setTimeout(r, 10)); + assert.ok(console.error.calledOnce); + assert.ok(console.error.calledWith(error)); + assert.ok(sample.mocks.Knex.notCalled); }); -test.cb.serial(`should handle knex creation error`, t => { - const error = new Error(`error`); +it('should handle knex creation error', async () => { + const error = new Error('error'); const sample = getSample(); sample.mocks.knex.schema.createTable = sinon .stub() @@ -122,10 +124,8 @@ test.cb.serial(`should handle knex creation error`, t => { prompt: sample.mocks.prompt, }); - setTimeout(() => { - t.true(console.error.calledOnce); - t.true(console.error.calledWith(`Failed to create 'votes' table:`, error)); - t.true(sample.mocks.knex.destroy.calledOnce); - t.end(); - }, 10); + await new Promise(r => setTimeout(r, 10)); + assert.ok(console.error.calledOnce); + assert.ok(console.error.calledWith(`Failed to create 'votes' table:`, error)); + assert.ok(sample.mocks.knex.destroy.calledOnce); }); diff --git a/cloud-sql/postgres/knex/test/server.test.js b/cloud-sql/postgres/knex/test/server.test.js index ea2dfa8e4e..a7e7f26cc8 100644 --- a/cloud-sql/postgres/knex/test/server.test.js +++ b/cloud-sql/postgres/knex/test/server.test.js @@ -15,19 +15,19 @@ 'use strict'; -const express = require(`express`); -const path = require(`path`); -const proxyquire = require(`proxyquire`).noCallThru(); -const request = require(`supertest`); -const sinon = require(`sinon`); -const test = require(`ava`); -const tools = require(`@google-cloud/nodejs-repo-tools`); +const express = require('express'); +const path = require('path'); +const proxyquire = require('proxyquire').noCallThru(); +const request = require('supertest'); +const sinon = require('sinon'); +const assert = require('assert'); +const tools = require('@google-cloud/nodejs-repo-tools'); -const SAMPLE_PATH = path.join(__dirname, `../server.js`); +const SAMPLE_PATH = path.join(__dirname, '../server.js'); function getSample() { const testApp = express(); - sinon.stub(testApp, `listen`).yields(); + sinon.stub(testApp, 'listen').yields(); const expressMock = sinon.stub().returns(testApp); const timestamp = new Date(); const resultsMock = [ @@ -75,15 +75,15 @@ function getSample() { }; } -test.beforeEach(tools.stubConsole); -test.afterEach.always(tools.restoreConsole); +beforeEach(tools.stubConsole); +afterEach(tools.restoreConsole); -test(`should set up sample in Postgres`, t => { +it('should set up sample in Postgres', () => { const sample = getSample(); - t.true(sample.mocks.express.calledOnce); - t.true(sample.mocks.Knex.calledOnce); - t.deepEqual(sample.mocks.Knex.firstCall.args, [ + assert.ok(sample.mocks.express.calledOnce); + assert.ok(sample.mocks.Knex.calledOnce); + assert.deepStrictEqual(sample.mocks.Knex.firstCall.args, [ { client: 'pg', connection: { @@ -95,30 +95,28 @@ test(`should set up sample in Postgres`, t => { ]); }); -test.cb(`should display the default page`, t => { +it('should display the default page', async () => { const sample = getSample(); - const expectedResult = `Tabs VS Spaces`; + const expectedResult = 'Tabs VS Spaces'; - request(sample.app) - .get(`/`) + await request(sample.app) + .get('/') .expect(200) .expect(response => { - t.is(response.text, expectedResult); - }) - .end(t.end); + assert.strictEqual(response.text, expectedResult); + }); }); -test.cb(`should handle insert error`, t => { +it('should handle insert error', async () => { const sample = getSample(); const expectedResult = 'Invalid team specified'; sample.mocks.knex.limit.returns(Promise.reject()); - request(sample.app) - .post(`/`) + await request(sample.app) + .post('/') .expect(400) .expect(response => { - t.is(response.text.includes(expectedResult), true); - }) - .end(t.end); + assert.ok(response.text.includes(expectedResult)); + }); });