-
-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathrunTestFailureTest.ts
229 lines (209 loc) · 9.44 KB
/
runTestFailureTest.ts
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
import { BrowserLauncher, TestRunnerCoreConfig, TestSession } from '@web/test-runner-core';
import { runTests } from '@web/test-runner-core/test-helpers';
import { legacyPlugin } from '@web/dev-server-legacy';
import { resolve, sep } from 'path';
import { expect } from 'chai';
const ERROR_NOT_IMPORTABLE = {
message:
'Could not import your test module. Check the browser logs or open the browser in debug mode for more information.',
};
const FAILED_TO_FETCH_MESSAGES = [
// chromium
'Failed to fetch dynamically imported module',
// firefox
'error loading dynamically imported module',
// safari
'Importing a module script failed',
];
function expectFetchModuleFailed(msg: string) {
if (!FAILED_TO_FETCH_MESSAGES.some(m => msg.includes(m))) {
throw new Error(`Expected a failed to fetch module message, but got error message: ${msg}`);
}
}
export function runTestFailureTest(
config: Partial<TestRunnerCoreConfig> & { browsers: BrowserLauncher[] },
) {
describe('test-failure', async function () {
const browserCount = config.browsers.length;
let allSessions: TestSession[];
before(async () => {
const result = await runTests(
{
...config,
files: [...(config.files ?? []), resolve(__dirname, 'browser-tests', '*.test.js')],
plugins: [...(config.plugins ?? []), legacyPlugin()],
},
undefined,
{ allowFailure: true, reportErrors: false },
);
allSessions = result.sessions;
expect(allSessions.every(s => s.passed)).to.equal(false, 'All sessions should have failed');
});
it('handles tests with 404 imports', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-404-import.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.suites.length).to.equal(0);
expect(session.testResults!.tests.length).to.equal(0);
expect(session.request404s).to.eql([
'integration/test-runner/tests/test-failure/browser-tests/non-existing.js',
]);
expect(session.errors).to.eql([ERROR_NOT_IMPORTABLE]);
expect(session.logs.length).to.equal(1);
expectFetchModuleFailed((session.logs[0] as any)[0]);
}
});
it('handles tests that error with a circular reference', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-circular-error.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.tests.map(t => t.name)).to.eql(['bad predicate']);
expect(session.passed).to.be.false;
expect(session.testResults!.tests![0].error!.message).to.equal(
"expected { x: 'x', circle: [Circular] } to equal null",
);
}
});
it('handles tests that throw in afterEach', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-after-each.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.tests.map(t => t.name)).to.eql([
'true is true',
'true is really true',
]);
expect(session.errors.length).to.equal(1);
expect(session.errors[0].message).to.include('error thrown in afterEach hook');
expect(session.errors[0].stack).to.include(
`test-failure${sep}browser-tests${sep}fail-after-each.test.js`,
);
expect(session.logs).to.eql([]);
}
});
it('handles tests that throw in after', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-after.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.tests.map(t => t.name)).to.eql([
'true is true',
'true is really true',
]);
expect(session.errors.length).to.equal(1);
expect(session.errors[0].message).to.include('error thrown in after hook');
expect(session.errors[0].stack).to.include(
`test-failure${sep}browser-tests${sep}fail-after.test.js`,
);
expect(session.logs).to.eql([]);
}
});
it('handles tests that throw in beforeEach', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-before-each.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.tests.map(t => t.name)).to.eql([
'true is true',
'true is really true',
]);
expect(session.errors.length).to.equal(1);
expect(session.errors[0].message).to.include('error thrown in beforeEach hook');
expect(session.errors[0].stack).to.include(
`test-failure${sep}browser-tests${sep}fail-before-each.test.js`,
);
expect(session.logs).to.eql([]);
}
});
it('handles tests that throw in before', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-before.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.tests.map(t => t.name)).to.eql([
'true is true',
'true is really true',
]);
expect(session.errors.length).to.equal(1);
expect(session.errors[0].message).to.include('error thrown in before hook');
expect(session.errors[0].stack).to.include(
`test-failure${sep}browser-tests${sep}fail-before.test.js`,
);
expect(session.logs).to.eql([]);
}
});
it('handles a custom thrown error', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-custom-error.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.tests.map(t => t.name)).to.eql(['custom error']);
expect(session.testResults!.tests[0].error!.message).to.include('a custom error thrown');
expect(session.testResults!.tests[0].error!.stack).to.include(
`browser-tests${sep}fail-custom-error.test.js`,
);
expect(session.errors).to.eql([]);
expect(session.logs).to.eql([]);
}
});
it('handles an error executing a module', () => {
const sessions = allSessions.filter(s =>
s.testFile.endsWith('fail-error-module-exec.test.js'),
);
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.suites.length).to.equal(0);
expect(session.testResults!.tests.length).to.equal(0);
expect(session.errors).to.eql([ERROR_NOT_IMPORTABLE]);
expect(session.logs[0][0]).to.include('This is thrown before running tests');
}
});
it('handles error stack traces correctly', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-stack-trace.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.tests.length).to.equal(1);
expect(session.testResults!.tests[0]!.error!.message).to.include('My error');
expect(session.testResults!.tests[0]!.error!.stack).to.include('throwErrorC');
expect(session.testResults!.tests[0]!.error!.stack).to.include('fail-stack-trace-c.js');
expect(session.errors).to.eql([]);
expect(session.logs).to.eql([]);
}
});
it('handles string diffs correctly', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-string-diff.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.tests[0]!.name).to.equal('string diff');
expect(session.testResults!.tests[0]!.error!.message).to.include(
"expected 'foo' to equal 'bar'",
);
expect(session.testResults!.tests[0]!.error!.expected).to.equal('bar');
expect(session.testResults!.tests[0]!.error!.actual).to.equal('foo');
expect(session.testResults!.tests.length).to.equal(1);
expect(session.errors).to.eql([]);
expect(session.logs).to.eql([]);
}
});
it('handles syntax errors', () => {
const sessions = allSessions.filter(s => s.testFile.endsWith('fail-syntax-error.test.js'));
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.suites.length).to.eql(0);
expect(session.testResults!.tests.length).to.eql(0);
expect(session.errors).to.eql([ERROR_NOT_IMPORTABLE]);
}
});
it('handles tests that error with an immutable or unserializable actual', () => {
const sessions = allSessions.filter(s =>
s.testFile.endsWith('fail-unserializable-actual.test.js'),
);
expect(sessions.length === browserCount).to.equal(true);
for (const session of sessions) {
expect(session.testResults!.tests.map(t => t.name)).to.eql(['unserializable actual']);
expect(session.passed).to.be.false;
expect(session.testResults!.tests![0].error!.message).to.equal(
'expected { x: {} } to equal null',
);
expect(session.testResults!.tests![1].error!.message).to.equal(
'expected { x: {} } to equal null',
);
}
});
});
}