Skip to content

Commit 33f0fe3

Browse files
committed
fix(test-runner): use expectIncludes for better assertion errors in integration tests
Add local expectIncludes helper to replace assert.ok(x.includes(y)) for better error messages on failure. Assisted-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c94513b commit 33f0fe3

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

integration/test-runner/tests/location-change/runLocationChangeTest.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { runTests } from '@web/test-runner-core/test-helpers';
55
import { legacyPlugin } from '@web/dev-server-legacy';
66
import { resolve } from 'path';
77

8+
function expectIncludes(actual: string, expected: string) {
9+
if (!actual.includes(expected)) {
10+
throw new Error(
11+
`Expected substring not found.\n\nExpected:\n${expected}\n\nActual:\n${actual}`,
12+
);
13+
}
14+
}
15+
816
export function runLocationChangeTest(
917
config: Partial<TestRunnerCoreConfig> & { browsers: BrowserLauncher[] },
1018
) {
@@ -83,7 +91,7 @@ export function runLocationChangeTest(
8391
'Tests were interrupted because the page navigated to',
8492
),
8593
);
86-
assert.ok(session.errors[0].message.includes('/new-page/'));
94+
expectIncludes(session.errors[0].message, '/new-page/');
8795
assert.ok(
8896
session.errors[0].message.includes(
8997
'This can happen when clicking a link, submitting a form or interacting with window.location.',

integration/test-runner/tests/test-failure/runTestFailureTest.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import { runTests } from '@web/test-runner-core/test-helpers';
55
import { legacyPlugin } from '@web/dev-server-legacy';
66
import { resolve, sep } from 'path';
77

8+
function expectIncludes(actual: string, expected: string) {
9+
if (!actual.includes(expected)) {
10+
throw new Error(
11+
`Expected substring not found.\n\nExpected:\n${expected}\n\nActual:\n${actual}`,
12+
);
13+
}
14+
}
15+
816
const ERROR_NOT_IMPORTABLE = {
917
message:
1018
'Could not import your test module. Check the browser logs or open the browser in debug mode for more information.',
@@ -93,7 +101,7 @@ export function runTestFailureTest(
93101
['true is true', 'true is really true'],
94102
);
95103
assert.equal(session.errors.length, 1);
96-
assert.ok(session.errors[0].message.includes('error thrown in afterEach hook'));
104+
expectIncludes(session.errors[0].message, 'error thrown in afterEach hook');
97105
assert.ok(
98106
session.errors[0].stack!.includes(
99107
`test-failure${sep}browser-tests${sep}fail-after-each.test.js`,
@@ -112,7 +120,7 @@ export function runTestFailureTest(
112120
['true is true', 'true is really true'],
113121
);
114122
assert.equal(session.errors.length, 1);
115-
assert.ok(session.errors[0].message.includes('error thrown in after hook'));
123+
expectIncludes(session.errors[0].message, 'error thrown in after hook');
116124
assert.ok(
117125
session.errors[0].stack!.includes(
118126
`test-failure${sep}browser-tests${sep}fail-after.test.js`,
@@ -131,7 +139,7 @@ export function runTestFailureTest(
131139
['true is true', 'true is really true'],
132140
);
133141
assert.equal(session.errors.length, 1);
134-
assert.ok(session.errors[0].message.includes('error thrown in beforeEach hook'));
142+
expectIncludes(session.errors[0].message, 'error thrown in beforeEach hook');
135143
assert.ok(
136144
session.errors[0].stack!.includes(
137145
`test-failure${sep}browser-tests${sep}fail-before-each.test.js`,
@@ -150,7 +158,7 @@ export function runTestFailureTest(
150158
['true is true', 'true is really true'],
151159
);
152160
assert.equal(session.errors.length, 1);
153-
assert.ok(session.errors[0].message.includes('error thrown in before hook'));
161+
expectIncludes(session.errors[0].message, 'error thrown in before hook');
154162
assert.ok(
155163
session.errors[0].stack!.includes(
156164
`test-failure${sep}browser-tests${sep}fail-before.test.js`,
@@ -168,7 +176,7 @@ export function runTestFailureTest(
168176
session.testResults!.tests.map(t => t.name),
169177
['custom error'],
170178
);
171-
assert.ok(session.testResults!.tests[0].error!.message.includes('a custom error thrown'));
179+
expectIncludes(session.testResults!.tests[0].error!.message, 'a custom error thrown');
172180
assert.ok(
173181
session.testResults!.tests[0].error!.stack!.includes(
174182
`browser-tests${sep}fail-custom-error.test.js`,
@@ -188,7 +196,7 @@ export function runTestFailureTest(
188196
assert.equal(session.testResults!.suites.length, 0);
189197
assert.equal(session.testResults!.tests.length, 0);
190198
assert.deepEqual(session.errors, [ERROR_NOT_IMPORTABLE]);
191-
assert.ok((session.logs[0][0] as any).includes('This is thrown before running tests'));
199+
expectIncludes(session.logs[0][0] as string, 'This is thrown before running tests');
192200
}
193201
});
194202

@@ -197,9 +205,9 @@ export function runTestFailureTest(
197205
assert.equal(sessions.length === browserCount, true);
198206
for (const session of sessions) {
199207
assert.equal(session.testResults!.tests.length, 1);
200-
assert.ok(session.testResults!.tests[0]!.error!.message.includes('My error'));
201-
assert.ok(session.testResults!.tests[0]!.error!.stack!.includes('throwErrorC'));
202-
assert.ok(session.testResults!.tests[0]!.error!.stack!.includes('fail-stack-trace-c.js'));
208+
expectIncludes(session.testResults!.tests[0]!.error!.message, 'My error');
209+
expectIncludes(session.testResults!.tests[0]!.error!.stack!, 'throwErrorC');
210+
expectIncludes(session.testResults!.tests[0]!.error!.stack!, 'fail-stack-trace-c.js');
203211
assert.deepEqual(session.errors, []);
204212
assert.deepEqual(session.logs, []);
205213
}

0 commit comments

Comments
 (0)