Skip to content

Commit 1a058e6

Browse files
authored
fix: clean up test helpers (#3127)
* fix: clean up test helpers * chore: rename node-test-helpers.js to node.js
1 parent dcee2cc commit 1a058e6

52 files changed

Lines changed: 503 additions & 538 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/cyan-clubs-serve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@web/dev-server-core': major
3+
---
4+
5+
Removed test helpers timeout, fetchText, expectIncludes, expectNotIncludes, virtualFilesPlugin.

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { expectIncludes } from '@web/dev-server-core/test-helpers';
21
import { legacyPlugin } from '@web/dev-server-legacy';
32
import type { BrowserLauncher, TestRunnerCoreConfig, TestSession } from '@web/test-runner-core';
43
import { runTests } from '@web/test-runner-core/test-helpers';
54
import assert from 'node:assert/strict';
65
import { before, describe, it } from 'node:test';
76
import { resolve } from 'path';
7+
import { assertIncludes } from '../../../../test-helpers/node.js';
88

99
export function runLocationChangeTest(
1010
config: Partial<TestRunnerCoreConfig> & { browsers: BrowserLauncher[] },
@@ -42,11 +42,11 @@ export function runLocationChangeTest(
4242
assert.equal(session.testResults, undefined);
4343
assert.deepEqual(session.logs, []);
4444
assert.equal(session.errors.length, 1);
45-
expectIncludes(
45+
assertIncludes(
4646
session.errors[0].message,
4747
'Tests were interrupted because the page navigated to',
4848
);
49-
expectIncludes(
49+
assertIncludes(
5050
session.errors[0].message,
5151
'This can happen when clicking a link, submitting a form or interacting with window.location.',
5252
);
@@ -77,12 +77,12 @@ export function runLocationChangeTest(
7777
assert.equal(session.testResults, undefined);
7878
assert.deepEqual(session.logs, []);
7979
assert.equal(session.errors.length, 1);
80-
expectIncludes(
80+
assertIncludes(
8181
session.errors[0].message,
8282
'Tests were interrupted because the page navigated to',
8383
);
84-
expectIncludes(session.errors[0].message, '/new-page/');
85-
expectIncludes(
84+
assertIncludes(session.errors[0].message, '/new-page/');
85+
assertIncludes(
8686
session.errors[0].message,
8787
'This can happen when clicking a link, submitting a form or interacting with window.location.',
8888
);

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { expectIncludes } from '@web/dev-server-core/test-helpers';
21
import { legacyPlugin } from '@web/dev-server-legacy';
32
import type { BrowserLauncher, TestRunnerCoreConfig, TestSession } from '@web/test-runner-core';
43
import { runTests } from '@web/test-runner-core/test-helpers';
54
import assert from 'node:assert/strict';
65
import { before, describe, it } from 'node:test';
76
import { resolve, sep } from 'path';
7+
import { assertIncludes } from '../../../../test-helpers/node.js';
88

99
const ERROR_NOT_IMPORTABLE = {
1010
message:
@@ -94,7 +94,7 @@ export function runTestFailureTest(
9494
['true is true', 'true is really true'],
9595
);
9696
assert.equal(session.errors.length, 1);
97-
expectIncludes(session.errors[0].message, 'error thrown in afterEach hook');
97+
assertIncludes(session.errors[0].message, 'error thrown in afterEach hook');
9898
assert.ok(
9999
session.errors[0].stack!.includes(
100100
`test-failure${sep}browser-tests${sep}fail-after-each.test.js`,
@@ -113,7 +113,7 @@ export function runTestFailureTest(
113113
['true is true', 'true is really true'],
114114
);
115115
assert.equal(session.errors.length, 1);
116-
expectIncludes(session.errors[0].message, 'error thrown in after hook');
116+
assertIncludes(session.errors[0].message, 'error thrown in after hook');
117117
assert.ok(
118118
session.errors[0].stack!.includes(
119119
`test-failure${sep}browser-tests${sep}fail-after.test.js`,
@@ -132,7 +132,7 @@ export function runTestFailureTest(
132132
['true is true', 'true is really true'],
133133
);
134134
assert.equal(session.errors.length, 1);
135-
expectIncludes(session.errors[0].message, 'error thrown in beforeEach hook');
135+
assertIncludes(session.errors[0].message, 'error thrown in beforeEach hook');
136136
assert.ok(
137137
session.errors[0].stack!.includes(
138138
`test-failure${sep}browser-tests${sep}fail-before-each.test.js`,
@@ -151,7 +151,7 @@ export function runTestFailureTest(
151151
['true is true', 'true is really true'],
152152
);
153153
assert.equal(session.errors.length, 1);
154-
expectIncludes(session.errors[0].message, 'error thrown in before hook');
154+
assertIncludes(session.errors[0].message, 'error thrown in before hook');
155155
assert.ok(
156156
session.errors[0].stack!.includes(
157157
`test-failure${sep}browser-tests${sep}fail-before.test.js`,
@@ -169,7 +169,7 @@ export function runTestFailureTest(
169169
session.testResults!.tests.map(t => t.name),
170170
['custom error'],
171171
);
172-
expectIncludes(session.testResults!.tests[0].error!.message, 'a custom error thrown');
172+
assertIncludes(session.testResults!.tests[0].error!.message, 'a custom error thrown');
173173
assert.ok(
174174
session.testResults!.tests[0].error!.stack!.includes(
175175
`browser-tests${sep}fail-custom-error.test.js`,
@@ -189,7 +189,7 @@ export function runTestFailureTest(
189189
assert.equal(session.testResults!.suites.length, 0);
190190
assert.equal(session.testResults!.tests.length, 0);
191191
assert.deepEqual(session.errors, [ERROR_NOT_IMPORTABLE]);
192-
expectIncludes(session.logs[0][0] as string, 'This is thrown before running tests');
192+
assertIncludes(session.logs[0][0] as string, 'This is thrown before running tests');
193193
}
194194
});
195195

@@ -198,9 +198,9 @@ export function runTestFailureTest(
198198
assert.equal(sessions.length === browserCount, true);
199199
for (const session of sessions) {
200200
assert.equal(session.testResults!.tests.length, 1);
201-
expectIncludes(session.testResults!.tests[0]!.error!.message, 'My error');
202-
expectIncludes(session.testResults!.tests[0]!.error!.stack!, 'throwErrorC');
203-
expectIncludes(session.testResults!.tests[0]!.error!.stack!, 'fail-stack-trace-c.js');
201+
assertIncludes(session.testResults!.tests[0]!.error!.message, 'My error');
202+
assertIncludes(session.testResults!.tests[0]!.error!.stack!, 'throwErrorC');
203+
assertIncludes(session.testResults!.tests[0]!.error!.stack!, 'fail-stack-trace-c.js');
204204
assert.deepEqual(session.errors, []);
205205
assert.deepEqual(session.logs, []);
206206
}

packages/browser-logs/test/serialize-deserialize.test.ts

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { expectIncludes } from '@web/dev-server-core/test-helpers';
21
import assert from 'node:assert/strict';
32
import fs from 'node:fs';
43
import path from 'node:path';
54
import { after, before, describe, it } from 'node:test';
5+
import { assertIncludes } from '../../../test-helpers/node.js';
66

77
import type { Browser, Page } from 'puppeteer';
88
import puppeteer from 'puppeteer';
@@ -350,11 +350,11 @@ describe('serialize deserialize', { timeout: 10000 }, function () {
350350
});
351351
const deserialized = await deserialize(serialized, defaultOptions);
352352
assert.equal(typeof deserialized, 'string');
353-
expectIncludes(deserialized, 'my error msg');
354-
expectIncludes(deserialized, '2:23');
355-
expectIncludes(deserialized, '3:23');
356-
expectIncludes(deserialized, '4:23');
357-
expectIncludes(deserialized, '5:35');
353+
assertIncludes(deserialized, 'my error msg');
354+
assertIncludes(deserialized, '2:23');
355+
assertIncludes(deserialized, '3:23');
356+
assertIncludes(deserialized, '4:23');
357+
assertIncludes(deserialized, '5:35');
358358
});
359359

360360
it('handles errors in objects', async () => {
@@ -366,11 +366,11 @@ describe('serialize deserialize', { timeout: 10000 }, function () {
366366
});
367367
const deserialized = await deserialize(serialized, defaultOptions);
368368
assert.equal(typeof deserialized.myError, 'string');
369-
expectIncludes(deserialized.myError, 'my error msg');
370-
expectIncludes(deserialized.myError, '2:23');
371-
expectIncludes(deserialized.myError, '3:23');
372-
expectIncludes(deserialized.myError, '4:23');
373-
expectIncludes(deserialized.myError, '5:46');
369+
assertIncludes(deserialized.myError, 'my error msg');
370+
assertIncludes(deserialized.myError, '2:23');
371+
assertIncludes(deserialized.myError, '3:23');
372+
assertIncludes(deserialized.myError, '4:23');
373+
assertIncludes(deserialized.myError, '5:46');
374374
});
375375

376376
it('handles errors in arrays', async () => {
@@ -382,20 +382,20 @@ describe('serialize deserialize', { timeout: 10000 }, function () {
382382
});
383383
const deserialized = await deserialize(serialized, defaultOptions);
384384
assert.equal(typeof deserialized[0], 'string');
385-
expectIncludes(deserialized[0], 'my error msg');
386-
expectIncludes(deserialized[0], '2:23');
387-
expectIncludes(deserialized[0], '3:23');
388-
expectIncludes(deserialized[0], '4:23');
389-
expectIncludes(deserialized[0], '5:36');
385+
assertIncludes(deserialized[0], 'my error msg');
386+
assertIncludes(deserialized[0], '2:23');
387+
assertIncludes(deserialized[0], '3:23');
388+
assertIncludes(deserialized[0], '4:23');
389+
assertIncludes(deserialized[0], '5:36');
390390
assert.equal(typeof deserialized[1], 'string');
391-
expectIncludes(deserialized[1], 'my error msg');
392-
expectIncludes(deserialized[1], '2:23');
393-
expectIncludes(deserialized[1], '3:23');
394-
expectIncludes(deserialized[1], '5:41');
391+
assertIncludes(deserialized[1], 'my error msg');
392+
assertIncludes(deserialized[1], '2:23');
393+
assertIncludes(deserialized[1], '3:23');
394+
assertIncludes(deserialized[1], '5:41');
395395
assert.equal(typeof deserialized[2], 'string');
396-
expectIncludes(deserialized[2], 'my error msg');
397-
expectIncludes(deserialized[2], '2:23');
398-
expectIncludes(deserialized[2], '5:46');
396+
assertIncludes(deserialized[2], 'my error msg');
397+
assertIncludes(deserialized[2], '2:23');
398+
assertIncludes(deserialized[2], '5:46');
399399
});
400400

401401
it('can map stack trace locations', async () => {
@@ -410,8 +410,8 @@ describe('serialize deserialize', { timeout: 10000 }, function () {
410410
mapStackLocation: l => ({ ...l, filePath: `${l.filePath}__MAPPED__`, line: 1, column: 2 }),
411411
});
412412
assert.equal(typeof deserialized, 'string');
413-
expectIncludes(deserialized, 'my error msg');
414-
expectIncludes(deserialized, `__MAPPED__:1:2`);
413+
assertIncludes(deserialized, 'my error msg');
414+
assertIncludes(deserialized, `__MAPPED__:1:2`);
415415
});
416416

417417
it('mapped stack traces can be async', async () => {
@@ -429,8 +429,8 @@ describe('serialize deserialize', { timeout: 10000 }, function () {
429429
},
430430
});
431431
assert.equal(typeof deserialized, 'string');
432-
expectIncludes(deserialized, 'my error msg');
433-
expectIncludes(deserialized, `__MAPPED__:1:2`);
432+
assertIncludes(deserialized, 'my error msg');
433+
assertIncludes(deserialized, `__MAPPED__:1:2`);
434434
});
435435

436436
it('can define a cwd below current directory', async () => {
@@ -445,11 +445,11 @@ describe('serialize deserialize', { timeout: 10000 }, function () {
445445
cwd: path.resolve(import.meta.dirname, '..'),
446446
});
447447
assert.equal(typeof deserialized, 'string');
448-
expectIncludes(deserialized, 'my error msg');
449-
expectIncludes(deserialized, `2:23`);
450-
expectIncludes(deserialized, `3:23`);
451-
expectIncludes(deserialized, `4:23`);
452-
expectIncludes(deserialized, `5:35`);
448+
assertIncludes(deserialized, 'my error msg');
449+
assertIncludes(deserialized, `2:23`);
450+
assertIncludes(deserialized, `3:23`);
451+
assertIncludes(deserialized, `4:23`);
452+
assertIncludes(deserialized, `5:35`);
453453
});
454454

455455
it('can define a cwd above current directory', async () => {
@@ -464,11 +464,11 @@ describe('serialize deserialize', { timeout: 10000 }, function () {
464464
browserRootDir: path.resolve(import.meta.dirname, '..'),
465465
});
466466
assert.equal(typeof deserialized, 'string');
467-
expectIncludes(deserialized, 'my error msg');
468-
expectIncludes(deserialized, `2:23`);
469-
expectIncludes(deserialized, `3:23`);
470-
expectIncludes(deserialized, `4:23`);
471-
expectIncludes(deserialized, `5:35`);
467+
assertIncludes(deserialized, 'my error msg');
468+
assertIncludes(deserialized, `2:23`);
469+
assertIncludes(deserialized, `3:23`);
470+
assertIncludes(deserialized, `4:23`);
471+
assertIncludes(deserialized, `5:35`);
472472
});
473473

474474
it('handles null', async () => {
Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { green, red, yellow } from 'nanocolors';
21
import portfinder from 'portfinder';
32

43
import { Logger } from './logger/Logger.js';
5-
import { Plugin } from './plugins/Plugin.js';
64
import { DevServer } from './server/DevServer.js';
75
import { DevServerCoreConfig } from './server/DevServerCoreConfig.js';
86

@@ -23,17 +21,6 @@ const mockLogger: Logger = {
2321
},
2422
};
2523

26-
export function virtualFilesPlugin(servedFiles: Record<string, string>): Plugin {
27-
return {
28-
name: 'test-helpers-virtual-files',
29-
serve(context) {
30-
if (context.path in servedFiles) {
31-
return servedFiles[context.path];
32-
}
33-
},
34-
};
35-
}
36-
3724
export async function createTestServer(
3825
config: Partial<DevServerCoreConfig>,
3926
_mockLogger = mockLogger,
@@ -56,25 +43,3 @@ export async function createTestServer(
5643
url.port = port.toString();
5744
return { server, port, host: url.toString().slice(0, -1) };
5845
}
59-
60-
export const timeout = (ms = 0) => new Promise(resolve => setTimeout(resolve, ms));
61-
62-
export async function fetchText(url: string, init?: RequestInit) {
63-
const response = await fetch(url, init);
64-
if (response.status !== 200) {
65-
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
66-
}
67-
return response.text();
68-
}
69-
70-
export function expectIncludes(text: string, expected: string) {
71-
if (!text.includes(expected)) {
72-
throw new Error(red(`Expected "${yellow(expected)}" in string: \n\n${green(text)}`));
73-
}
74-
}
75-
76-
export function expectNotIncludes(text: string, expected: string) {
77-
if (text.includes(expected)) {
78-
throw new Error(`Did not expect "${expected}" in string: \n\n${text}`);
79-
}
80-
}
Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
11
// this file is autogenerated with the generate-mjs-dts-entrypoints script
22
import cjsEntrypoint from './dist/test-helpers.js';
33

4-
const {
5-
virtualFilesPlugin,
6-
createTestServer,
7-
timeout,
8-
fetchText,
9-
expectIncludes,
10-
expectNotIncludes,
11-
} = cjsEntrypoint;
4+
const { createTestServer } = cjsEntrypoint;
125

13-
export {
14-
virtualFilesPlugin,
15-
createTestServer,
16-
timeout,
17-
fetchText,
18-
expectIncludes,
19-
expectNotIncludes,
20-
};
6+
export { createTestServer };
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import path from 'path';
22
import type { DevServerCoreConfig } from '../dist/server/DevServerCoreConfig.js';
3-
import {
4-
expectIncludes,
5-
expectNotIncludes,
6-
fetchText,
7-
createTestServer as originalCreateTestServer,
8-
timeout,
9-
virtualFilesPlugin,
10-
} from '../dist/test-helpers.js';
3+
import { createTestServer as originalCreateTestServer } from '../dist/test-helpers.js';
114

125
export function createTestServer(config: Partial<DevServerCoreConfig> = {}) {
136
return originalCreateTestServer({
147
rootDir: path.resolve(import.meta.dirname, 'fixtures', 'basic'),
158
...config,
169
});
1710
}
18-
19-
export { timeout, fetchText, expectIncludes, expectNotIncludes, virtualFilesPlugin };

0 commit comments

Comments
 (0)