-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
esm-interop.spec.js
754 lines (739 loc) · 20 KB
/
esm-interop.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
'use strict';
const assert = require('assert');
const path = require('path');
const fs = require('fs');
const { execCommand } = require('cli-testlab');
const sqlite3 = require('@vscode/sqlite3');
const semver = require('semver');
const { createTable } = require('./cli-test-utils');
const KNEX = path.normalize(__dirname + '/../../bin/cli.js');
const TEST_BASE = '../test/jake-util';
const fixture = [
/** MIGRATIONS */
{
title: 'migrates esm modules',
testCase: 'knexfile-esm',
knexArgs: ['migrate:latest', '--esm'],
dropDb: true,
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
},
{
title: 'migrates esm modules from a module',
testCase: 'knexfile-esm-module',
knexArgs: ['migrate:latest'],
expectedOutput: 'Batch 1 run: 1 migrations',
dropDb: true,
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
},
{
title: 'migrate esm module without --esm flag from a module',
testCase: 'knexfile-esm-module',
knexArgs: ['migrate:latest'],
dropDb: true,
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
},
{
title: 'migrates mjs modules',
testCase: 'knexfile-mjs',
knexfile: 'knexfile.mjs',
knexArgs: ['migrate:latest'],
dropDb: true,
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
},
/** SEEDS */
{
title: 'seeds esm files',
testCase: 'knexfile-esm',
expectedOutput: 'Ran 1 seed files',
knexArgs: ['seed:run', '--esm'],
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: 'seeds esm files from module',
testCase: 'knexfile-esm-module',
expectedOutput: 'Ran 1 seed files',
knexArgs: ['seed:run'],
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
dropDb: true,
},
{
title: 'seeds esm files from module without --esm flag',
testCase: 'knexfile-esm-module',
expectedOutput: 'Ran 1 seed files',
knexArgs: ['seed:run'],
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
dropDb: true,
},
{
title: 'seeds mjs files',
testCase: 'knexfile-mjs',
knexfile: 'knexfile.mjs',
knexArgs: ['seed:run'],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
/** SPECIAL CASES */
{
title: 'mjs files with mjs top level imports',
testCase: 'knexfile-imports',
knexfile: 'knexfile.mjs',
knexArgs: ['migrate:latest'],
dropDb: true,
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
},
{
title:
'Directory import is not supported, resolving ES modules imported from knexfile.mjs',
testCase: 'knexfile-imports',
knexfile: 'knexfile1.mjs',
knexArgs: ['migrate:latest'],
expectedErrorMessage: `Error [ERR_UNSUPPORTED_DIR_IMPORT]`,
},
{
title: 'dynamic importing js file from NON module package is not supported',
testCase: 'knexfile-imports',
knexfile: 'knexfile2.mjs',
knexArgs: ['migrate:latest'],
expectedErrorMessage: semver.eq(process.version, 'v14.13.0')
? 'Unexpected export statement in CJS module'
: semver.gte(process.version, 'v14.14.0')
? 'Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.'
: "Unexpected token 'export'",
},
{
title: 'static importing js file from NON module package is not supported',
testCase: 'knexfile-imports',
knexfile: 'knexfile2.mjs',
knexArgs: ['migrate:latest'],
expectedErrorMessage: semver.eq(process.version, 'v14.13.0')
? 'Unexpected export statement in CJS module'
: semver.gte(process.version, 'v14.14.0')
? 'Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.'
: "Unexpected token 'export'",
},
{
//Example: external module.type='module' by url 'packane-name/index.js'
title: 'can dynamically import external ESM module package by URL',
testCase: 'knexfile-imports',
knexfile: 'knexfile4.mjs',
knexArgs: ['migrate:latest'],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'Importing commonjs from a mjs module',
testCase: 'knexfile-imports',
knexfile: 'knexfile5.mjs',
knexArgs: ['migrate:latest'],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'Importing commonjs from a js ESM module and --esm interop',
testCase: 'knexfile-imports',
knexfile: 'knexfile6.js',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'Importing js ESM from js ESM with --esm interop',
testCase: 'knexfile-imports',
knexfile: 'knexfile7.js',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'Dynamic importing js ESM from js ESM with --esm interop',
testCase: 'knexfile-imports',
knexfile: 'knexfile8.js',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'Static top level cjs import from js ESM with --esm interop',
testCase: 'knexfile-imports',
knexfile: 'knexfile9.js',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'Dynamic ESM js import from commonjs/js with esm migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile10.js',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'Imports commonjs/cjs provides js/esm migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile11.js',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'Imports commonjs/cjs provides cjs migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile11.js',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'cjs knexfile Imports commonjs/cjs provides cjs migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile12.cjs',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'cjs knexfile Imports commonjs/js provides js migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile13.cjs',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'cjs knexfile provides esm migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile14.cjs',
knexArgs: ['migrate:latest', `--esm`],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'cjs knexfile provides mjs migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile15.cjs',
knexArgs: ['migrate:latest', '--esm'],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'mjs knexfile provides cjs migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile16.mjs',
knexArgs: ['migrate:latest'],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
/**
* This is Standard NODEJS resolution
* */
title: 'mjs knexfile provides ESM/js migrations #1',
testCase: 'knexfile-imports',
knexfile: 'knexfile17.mjs',
knexArgs: ['migrate:latest'],
/**
* Migration DOESN'T RUN?, files aren't found
* config.migrations.loadExtensions defaults to ['.mjs']
*/
expectedOutput: 'Already up to date',
/** confirmation, migration didn't run */
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
], //schema is default knex schema
dropDb: true,
},
{
/**
* This is Standard NODEJS resolution
* even with the 'esm' module loader, AKA --esm
* no 'esm' involved, knexfile.mjs is navite/'imported'
*/
title: 'mjs knexfile provides ESM/js migrations #2',
testCase: 'knexfile-imports',
knexfile: 'knexfile17.mjs',
knexArgs: ['migrate:latest'],
/**
* Migration DOESN'T RUN, files aren't found
* config.migrations.loadExtensions defaults to ['.mjs']
*/
expectedOutput: 'Already up to date',
/** confirmation, migration didn't run */
expectedSchema: [
//schema is default knex schema
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
],
dropDb: true,
},
{
title: 'mjs knexfile provides ESM/js migrations if .js in loadExtensions',
testCase: 'knexfile-imports',
knexfile: 'knexfile18.mjs',
knexArgs: ['migrate:latest', '--esm'],
dropDb: true,
},
{
title:
"mjs knexfile CAN'T provide ESM/js migrations if .js in loadExtensions without --esm",
testCase: 'knexfile-imports',
knexfile: 'knexfile18.mjs',
knexArgs: ['migrate:latest'],
// Fails on NODE 12 & 14
expectedErrorMessage: "Unexpected token 'export'",
dropDb: true,
},
{
title: 'ESM/js knexfile provides cjs migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile19.js',
knexArgs: ['migrate:latest', '--esm'],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
{
title: 'ESM/js knexfile provides mjs migrations',
testCase: 'knexfile-imports',
knexfile: 'knexfile20.js',
knexArgs: ['migrate:latest', '--esm'],
expectedOutput: 'Batch 1 run: 1 migrations',
expectedSchema: [
'knex_migrations',
'sqlite_sequence',
'knex_migrations_lock',
'xyz',
],
dropDb: true,
},
/**
* Seed tests for the above cases
*/
{
title: `Seeds knexfile20.js`,
testCase: 'knexfile-imports',
knexfile: 'knexfile20.js',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile19.js`,
testCase: 'knexfile-imports',
knexfile: 'knexfile19.js',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile18.mjs`,
testCase: 'knexfile-imports',
knexfile: 'knexfile18.mjs',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile17.mjs`,
testCase: 'knexfile-imports',
knexfile: 'knexfile17.mjs',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile16.mjs`,
testCase: 'knexfile-imports',
knexfile: 'knexfile16.mjs',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile15.cjs`,
testCase: 'knexfile-imports',
knexfile: 'knexfile15.cjs',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile14.cjs`,
testCase: 'knexfile-imports',
knexfile: 'knexfile14.cjs',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile13.cjs`,
testCase: 'knexfile-imports',
knexfile: 'knexfile13.cjs',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile12.cjs`,
testCase: 'knexfile-imports',
knexfile: 'knexfile12.cjs',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile11.js`,
testCase: 'knexfile-imports',
knexfile: 'knexfile11.js',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile10.js`,
testCase: 'knexfile-imports',
knexfile: 'knexfile10.js',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile9.js`,
testCase: 'knexfile-imports',
knexfile: 'knexfile9.js',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile8.js`,
testCase: 'knexfile-imports',
knexfile: 'knexfile8.js',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile7.js`,
testCase: 'knexfile-imports',
knexfile: 'knexfile7.js',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
title: `Seeds knexfile6.js`,
testCase: 'knexfile-imports',
knexfile: 'knexfile6.js',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
// This case failure on Node10 is already documented
title: `Seeds knexfile5.mjs`,
testCase: 'knexfile-imports',
knexfile: 'knexfile5.mjs',
knexArgs: ['seed:run', `--esm`],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
{
// This case failure on Node10 is already documented
title: `Seeds knexfile4.mjs`,
testCase: 'knexfile-imports',
knexfile: 'knexfile4.mjs',
knexArgs: ['seed:run'],
expectedOutput: 'Ran 1 seed files',
dropDb: true,
before: async ({ dbPath }) => {
const db = new sqlite3.Database(dbPath);
await createTable(db, `xyz (name TEXT)`);
},
},
].filter(Boolean);
describe('esm interop and mjs support', () => {
before(() => {
process.env.KNEX_PATH = '../knex.js';
});
for (const spec of fixture) {
it(spec.title, async function () {
// This whole suite got broken in this version
if (semver.gte(process.version, 'v16.6.0')) {
return this.skip();
}
const {
testCase,
knexfile = 'knexfile.js',
dbName = 'test.sqlite3',
nodeArgs = [],
knexArgs,
expectedOutput,
expectedSchema,
expectedErrorMessage,
before,
dropDb = false,
} = spec;
const cwd = path.resolve(path.dirname(KNEX), TEST_BASE, testCase);
const dbPath = path.resolve(cwd, dbName);
// ...
const cmd = [
`node`,
...nodeArgs,
KNEX,
...knexArgs,
`--cwd="${cwd}"`,
`--knexfile=${path.resolve(cwd, knexfile)}`,
`--knexpath=${KNEX}`,
]
.filter(Boolean)
.join(' ');
// ...
if (dropDb && fs.existsSync(dbPath)) fs.unlinkSync(dbPath);
// ...
if (before) {
await before({
...spec,
cmd,
cwd,
testCase,
knexfile,
dbName,
dbPath,
nodeArgs,
knexArgs,
expectedOutput,
expectedSchema,
expectedErrorMessage,
});
}
// ...
await execCommand(cmd, { expectedOutput, expectedErrorMessage });
// ...
if (expectedSchema) {
const db = new sqlite3.Database(dbPath);
const result = await getSchema(db);
await closeDB(db);
// migration performed
assert.deepEqual(
result.map(({ name }) => name),
expectedSchema
);
}
});
}
});
function getSchema(db) {
return new Promise((resolve, reject) => {
db.all('SELECT name from SQLITE_MASTER', (err, rows) => {
if (err) reject(err);
else resolve(rows);
});
});
}
function closeDB(db) {
return new Promise((resolve, reject) =>
db.close((err) => {
if (err) reject(err);
else resolve();
})
);
}