1515
1616'use strict' ;
1717
18- const test = require ( `ava` ) ;
19- const path = require ( ` path` ) ;
20- const proxyquire = require ( ` proxyquire` ) . noPreserveCache ( ) ;
21- const sinon = require ( ` sinon` ) ;
22- const tools = require ( ` @google-cloud/nodejs-repo-tools` ) ;
18+ const assert = require ( 'assert' ) ;
19+ const path = require ( ' path' ) ;
20+ const proxyquire = require ( ' proxyquire' ) . noPreserveCache ( ) ;
21+ const sinon = require ( ' sinon' ) ;
22+ const tools = require ( ' @google-cloud/nodejs-repo-tools' ) ;
2323
24- const SAMPLE_PATH = path . join ( __dirname , ` ../createTables .js` ) ;
24+ const SAMPLE_PATH = path . join ( __dirname , ' ../createTable .js' ) ;
2525
26- const exampleConfig = [ ` user` , ` password` , ` database` ] ;
26+ const exampleConfig = [ ' user' , ' password' , ' database' ] ;
2727
2828function getSample ( ) {
2929 const configMock = exampleConfig ;
@@ -58,10 +58,10 @@ function getSample() {
5858 } ;
5959}
6060
61- test . beforeEach ( tools . stubConsole ) ;
62- test . afterEach . always ( tools . restoreConsole ) ;
61+ beforeEach ( tools . stubConsole ) ;
62+ afterEach ( tools . restoreConsole ) ;
6363
64- test . cb . serial ( ` should create a table` , t => {
64+ it ( ' should create a table' , async ( ) => {
6565 const sample = getSample ( ) ;
6666 const expectedResult = `Successfully created 'votes' table.` ;
6767
@@ -70,30 +70,34 @@ test.cb.serial(`should create a table`, t => {
7070 prompt : sample . mocks . prompt ,
7171 } ) ;
7272
73- t . true ( sample . mocks . prompt . start . calledOnce ) ;
74- t . true ( sample . mocks . prompt . get . calledOnce ) ;
75- t . deepEqual ( sample . mocks . prompt . get . firstCall . args [ 0 ] , exampleConfig ) ;
76-
77- setTimeout ( ( ) => {
78- t . true ( sample . mocks . Knex . calledOnce ) ;
79- t . deepEqual ( sample . mocks . Knex . firstCall . args , [
80- {
81- client : 'pg' ,
82- connection : exampleConfig ,
83- } ,
84- ] ) ;
85-
86- t . true ( sample . mocks . knex . schema . createTable . calledOnce ) ;
87- t . is ( sample . mocks . knex . schema . createTable . firstCall . args [ 0 ] , 'votes' ) ;
88-
89- t . true ( console . log . calledWith ( expectedResult ) ) ;
90- t . true ( sample . mocks . knex . destroy . calledOnce ) ;
91- t . end ( ) ;
92- } , 10 ) ;
73+ assert . ok ( sample . mocks . prompt . start . calledOnce ) ;
74+ assert . ok ( sample . mocks . prompt . get . calledOnce ) ;
75+ assert . deepStrictEqual (
76+ sample . mocks . prompt . get . firstCall . args [ 0 ] ,
77+ exampleConfig
78+ ) ;
79+
80+ await new Promise ( r => setTimeout ( r , 10 ) ) ;
81+ assert . ok ( sample . mocks . Knex . calledOnce ) ;
82+ assert . deepStrictEqual ( sample . mocks . Knex . firstCall . args , [
83+ {
84+ client : 'pg' ,
85+ connection : exampleConfig ,
86+ } ,
87+ ] ) ;
88+
89+ assert . ok ( sample . mocks . knex . schema . createTable . calledOnce ) ;
90+ assert . strictEqual (
91+ sample . mocks . knex . schema . createTable . firstCall . args [ 0 ] ,
92+ 'votes'
93+ ) ;
94+
95+ assert . ok ( console . log . calledWith ( expectedResult ) ) ;
96+ assert . ok ( sample . mocks . knex . destroy . calledOnce ) ;
9397} ) ;
9498
95- test . cb . serial ( ` should handle prompt error` , t => {
96- const error = new Error ( ` error` ) ;
99+ it ( ' should handle prompt error' , async ( ) => {
100+ const error = new Error ( ' error' ) ;
97101 const sample = getSample ( ) ;
98102 sample . mocks . prompt . get = sinon . stub ( ) . yields ( error ) ;
99103
@@ -102,16 +106,14 @@ test.cb.serial(`should handle prompt error`, t => {
102106 prompt : sample . mocks . prompt ,
103107 } ) ;
104108
105- setTimeout ( ( ) => {
106- t . true ( console . error . calledOnce ) ;
107- t . true ( console . error . calledWith ( error ) ) ;
108- t . true ( sample . mocks . Knex . notCalled ) ;
109- t . end ( ) ;
110- } , 10 ) ;
109+ await new Promise ( r => setTimeout ( r , 10 ) ) ;
110+ assert . ok ( console . error . calledOnce ) ;
111+ assert . ok ( console . error . calledWith ( error ) ) ;
112+ assert . ok ( sample . mocks . Knex . notCalled ) ;
111113} ) ;
112114
113- test . cb . serial ( ` should handle knex creation error` , t => {
114- const error = new Error ( ` error` ) ;
115+ it ( ' should handle knex creation error' , async ( ) => {
116+ const error = new Error ( ' error' ) ;
115117 const sample = getSample ( ) ;
116118 sample . mocks . knex . schema . createTable = sinon
117119 . stub ( )
@@ -122,10 +124,8 @@ test.cb.serial(`should handle knex creation error`, t => {
122124 prompt : sample . mocks . prompt ,
123125 } ) ;
124126
125- setTimeout ( ( ) => {
126- t . true ( console . error . calledOnce ) ;
127- t . true ( console . error . calledWith ( `Failed to create 'votes' table:` , error ) ) ;
128- t . true ( sample . mocks . knex . destroy . calledOnce ) ;
129- t . end ( ) ;
130- } , 10 ) ;
127+ await new Promise ( r => setTimeout ( r , 10 ) ) ;
128+ assert . ok ( console . error . calledOnce ) ;
129+ assert . ok ( console . error . calledWith ( `Failed to create 'votes' table:` , error ) ) ;
130+ assert . ok ( sample . mocks . knex . destroy . calledOnce ) ;
131131} ) ;
0 commit comments