-
Notifications
You must be signed in to change notification settings - Fork 0
/
30-suite-hooks.test.js
49 lines (43 loc) · 1.2 KB
/
30-suite-hooks.test.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
import { strict as assert } from 'node:assert';
import { dirname, basename } from 'node:path';
import { fileURLToPath } from 'node:url';
import { TestRunner } from '../dist/mjs/index.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
let DB = null;
const suite = new TestRunner(basename(__filename), {
before: async () => {
// simulate connect to DB
return new Promise((resolve) => {
DB = {};
resolve();
});
},
beforeEach: async () => {
// assert "connected"
if (!DB) {
throw new Error('Not connected...');
}
// simulate some data fixtures
DB['1'] = { name: 'foo' };
// returned value will be passed as (deep cloned) { arg } to testFn
return { DB };
},
afterEach: async () => {
// reset db
DB = {};
},
after: async () => {
// simulate disconnect
DB = null;
},
});
suite.test('Working with data fixtures', ({ DB }) => {
assert(DB['1'].name === 'foo'); // asserts the expected fixture
DB['2'] = { name: 'bar' }; // touch the DB
});
suite.test('Means they must stay fixed', ({ DB }) => {
assert(DB['1'].name === 'foo'); // asserts the expected fixture
assert(!DB['2']); // assert the touch from above was reset
});
export default suite;