-
Notifications
You must be signed in to change notification settings - Fork 30.6k
/
Copy pathhooks.js
163 lines (141 loc) Β· 4.58 KB
/
hooks.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
// Flags: --no-warnings
'use strict';
const common = require('../../../common');
const assert = require('assert');
const { test, describe, it, before, after, beforeEach, afterEach } = require('node:test');
before((t) => t.diagnostic('before 1 called'));
describe('describe hooks', () => {
const testArr = [];
before(function() {
testArr.push('before ' + this.name);
});
after(function() {
testArr.push('after ' + this.name);
assert.deepStrictEqual(testArr, [
'before describe hooks',
'beforeEach 1', '1', 'afterEach 1',
'beforeEach 2', '2', 'afterEach 2',
'beforeEach nested',
'before nested',
'beforeEach nested 1', 'nested 1', 'afterEach nested 1',
'beforeEach nested 2', 'nested 2', 'afterEach nested 2',
'after nested',
'afterEach nested',
'after describe hooks',
]);
});
beforeEach(function() {
testArr.push('beforeEach ' + this.name);
});
afterEach(function() {
testArr.push('afterEach ' + this.name);
});
it('1', () => testArr.push('1'));
test('2', () => testArr.push('2'));
describe('nested', () => {
before(function() {
testArr.push('before ' + this.name);
});
after(function() {
testArr.push('after ' + this.name);
});
beforeEach(function() {
testArr.push('beforeEach ' + this.name);
});
afterEach(function() {
testArr.push('afterEach ' + this.name);
});
it('nested 1', () => testArr.push('nested 1'));
test('nested 2', () => testArr.push('nested 2'));
});
});
describe('before throws', () => {
before(() => { throw new Error('before'); });
it('1', () => {});
test('2', () => {});
});
describe('after throws', () => {
after(() => { throw new Error('after'); });
it('1', () => {});
test('2', () => {});
});
describe('beforeEach throws', () => {
beforeEach(() => { throw new Error('beforeEach'); });
it('1', () => {});
test('2', () => {});
});
describe('afterEach throws', () => {
afterEach(() => { throw new Error('afterEach'); });
it('1', () => {});
test('2', () => {});
});
describe('afterEach when test fails', () => {
afterEach(common.mustCall(2));
it('1', () => { throw new Error('test'); });
test('2', () => {});
});
describe('afterEach throws and test fails', () => {
afterEach(() => { throw new Error('afterEach'); });
it('1', () => { throw new Error('test'); });
test('2', () => {});
});
test('test hooks', async (t) => {
const testArr = [];
t.before((t) => testArr.push('before ' + t.name));
t.after(common.mustCall((t) => testArr.push('after ' + t.name)));
t.beforeEach((t) => testArr.push('beforeEach ' + t.name));
t.afterEach((t) => testArr.push('afterEach ' + t.name));
await t.test('1', () => testArr.push('1'));
await t.test('2', () => testArr.push('2'));
await t.test('nested', async (t) => {
t.beforeEach((t) => testArr.push('nested beforeEach ' + t.name));
t.afterEach((t) => testArr.push('nested afterEach ' + t.name));
await t.test('nested 1', () => testArr.push('nested1'));
await t.test('nested 2', () => testArr.push('nested 2'));
});
assert.deepStrictEqual(testArr, [
'beforeEach 1', 'before test hooks', '1', 'afterEach 1',
'beforeEach 2', '2', 'afterEach 2',
'beforeEach nested',
'nested beforeEach nested 1', 'nested1', 'nested afterEach nested 1',
'nested beforeEach nested 2', 'nested 2', 'nested afterEach nested 2',
'afterEach nested',
]);
});
test('t.before throws', async (t) => {
t.after(common.mustCall());
t.before(() => { throw new Error('before'); });
await t.test('1', () => {});
await t.test('2', () => {});
});
test('t.beforeEach throws', async (t) => {
t.after(common.mustCall());
t.beforeEach(() => { throw new Error('beforeEach'); });
await t.test('1', () => {});
await t.test('2', () => {});
});
test('t.afterEach throws', async (t) => {
t.after(common.mustCall());
t.afterEach(() => { throw new Error('afterEach'); });
await t.test('1', () => {});
await t.test('2', () => {});
});
test('afterEach when test fails', async (t) => {
t.after(common.mustCall());
t.afterEach(common.mustCall(2));
await t.test('1', () => { throw new Error('test'); });
await t.test('2', () => {});
});
test('afterEach throws and test fails', async (t) => {
t.after(common.mustCall());
t.afterEach(() => { throw new Error('afterEach'); });
await t.test('1', () => { throw new Error('test'); });
await t.test('2', () => {});
});
test('t.after() is called if test body throws', (t) => {
t.after(() => {
t.diagnostic('- after() called');
});
throw new Error('bye');
});
before((t) => t.diagnostic('before 2 called'));