Skip to content

Commit 970c48b

Browse files
authored
Merge pull request #3086 from modernweb-dev/migrate/polyfills-loader-node-test
refactor(polyfills-loader): migrate tests to node:test
2 parents 3cb708f + 69e5c22 commit 970c48b

4 files changed

Lines changed: 54 additions & 42 deletions

File tree

packages/polyfills-loader/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
},
2727
"scripts": {
2828
"build": "tsc",
29-
"test:node": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --reporter dot",
30-
"test:update-snapshots": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --update-snapshots",
31-
"test:watch": "mocha \"test/**/*.test.{ts,js,mjs,cjs}\" --require ts-node/register --watch --watch-files src,test"
29+
"test:node": "node --experimental-strip-types --test --test-force-exit test/**/*.test.ts",
30+
"test:watch": "node --experimental-strip-types --test --test-force-exit --watch test/**/*.test.ts"
3231
},
3332
"files": [
3433
"*.d.ts",

packages/polyfills-loader/test/createPolyfillsData.test.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert/strict';
13
import path from 'path';
2-
import { expect } from 'chai';
34

4-
import { PolyfillsLoaderConfig, PolyfillFile } from '../src/types.js';
5-
import { createPolyfillsData } from '../src/createPolyfillsData.js';
6-
import { noModuleSupportTest, fileTypes } from '../src/utils.js';
5+
import type { PolyfillsLoaderConfig, PolyfillFile } from '../dist/types.js';
6+
import { createPolyfillsData } from '../dist/createPolyfillsData.js';
7+
import { noModuleSupportTest, fileTypes } from '../dist/utils.js';
78

89
function cleanupPolyfill(polyfill: PolyfillFile) {
910
if (!polyfill) {
@@ -40,11 +41,11 @@ describe('polyfills', () => {
4041

4142
const polyfillFiles = await createPolyfillsData(config);
4243
for (const p of polyfillFiles) {
43-
expect(p.content).to.be.a('string', `Polyfill ${p.name} has no content`);
44+
assert.equal(typeof p.content, 'string', `Polyfill ${p.name} has no content`);
4445
cleanupPolyfill(p);
4546
}
4647

47-
expect(polyfillFiles).to.eql([
48+
assert.deepEqual(polyfillFiles, [
4849
{
4950
name: 'core-js',
5051
type: fileTypes.SCRIPT,
@@ -138,11 +139,11 @@ describe('polyfills', () => {
138139
};
139140
const polyfillFiles = await createPolyfillsData(config);
140141
for (const p of polyfillFiles) {
141-
expect(p.content).to.be.a('string', `Polyfill ${p.name} has no content`);
142+
assert.equal(typeof p.content, 'string', `Polyfill ${p.name} has no content`);
142143
cleanupPolyfill(p);
143144
}
144145

145-
expect(polyfillFiles).to.eql([
146+
assert.deepEqual(polyfillFiles, [
146147
{
147148
name: 'fetch',
148149
path: 'polyfills/fetch.js',
@@ -165,11 +166,11 @@ describe('polyfills', () => {
165166

166167
const polyfillFiles = await createPolyfillsData(config);
167168
for (const p of polyfillFiles) {
168-
expect(p.content).to.be.a('string', `Polyfill ${p.name} has no content`);
169+
assert.equal(typeof p.content, 'string', `Polyfill ${p.name} has no content`);
169170
cleanupPolyfill(p);
170171
}
171172

172-
expect(polyfillFiles).to.eql([
173+
assert.deepEqual(polyfillFiles, [
173174
{
174175
name: 'webcomponents-shady-css-custom-style',
175176
type: fileTypes.SCRIPT,
@@ -197,11 +198,11 @@ describe('polyfills', () => {
197198

198199
const polyfillFiles = await createPolyfillsData(config);
199200
for (const p of polyfillFiles) {
200-
expect(p.content).to.be.a('string', `Polyfill ${p.name} has no content`);
201+
assert.equal(typeof p.content, 'string', `Polyfill ${p.name} has no content`);
201202
cleanupPolyfill(p);
202203
}
203204

204-
expect(polyfillFiles).to.eql([
205+
assert.deepEqual(polyfillFiles, [
205206
{
206207
name: 'systemjs',
207208
type: fileTypes.SCRIPT,
@@ -232,11 +233,11 @@ describe('polyfills', () => {
232233

233234
const polyfillFiles = await createPolyfillsData(config);
234235
for (const p of polyfillFiles) {
235-
expect(p.content).to.be.a('string', `Polyfill ${p.name} has no content`);
236+
assert.equal(typeof p.content, 'string', `Polyfill ${p.name} has no content`);
236237
cleanupPolyfill(p);
237238
}
238239

239-
expect(polyfillFiles).to.eql([
240+
assert.deepEqual(polyfillFiles, [
240241
{
241242
name: 'systemjs',
242243
type: fileTypes.SCRIPT,
@@ -257,11 +258,11 @@ describe('polyfills', () => {
257258

258259
const polyfillFiles = await createPolyfillsData(config);
259260
for (const p of polyfillFiles) {
260-
expect(p.content).to.be.a('string', `Polyfill ${p.name} has no content`);
261+
assert.equal(typeof p.content, 'string', `Polyfill ${p.name} has no content`);
261262
cleanupPolyfill(p);
262263
}
263264

264-
expect(polyfillFiles).to.eql([
265+
assert.deepEqual(polyfillFiles, [
265266
{
266267
name: 'systemjs',
267268
type: fileTypes.SCRIPT,
@@ -277,12 +278,12 @@ describe('polyfills', () => {
277278
name: 'polyfill-a',
278279
test: "'foo' in window",
279280
content: '',
280-
path: path.resolve(__dirname, 'custom-polyfills/polyfill-a.js'),
281+
path: path.resolve(import.meta.dirname, 'custom-polyfills/polyfill-a.js'),
281282
},
282283
{
283284
name: 'polyfill-b',
284285
content: '',
285-
path: path.resolve(__dirname, 'custom-polyfills/polyfill-b.js'),
286+
path: path.resolve(import.meta.dirname, 'custom-polyfills/polyfill-b.js'),
286287
},
287288
];
288289

@@ -300,11 +301,11 @@ describe('polyfills', () => {
300301

301302
const polyfillFiles = await createPolyfillsData(config);
302303
for (const p of polyfillFiles) {
303-
expect(p.content).to.be.a('string', `Polyfill ${p.name} has no content`);
304+
assert.equal(typeof p.content, 'string', `Polyfill ${p.name} has no content`);
304305
cleanupPolyfill(p);
305306
}
306307

307-
expect(polyfillFiles).to.eql([
308+
assert.deepEqual(polyfillFiles, [
308309
{
309310
name: 'core-js',
310311
type: fileTypes.SCRIPT,
@@ -338,11 +339,11 @@ describe('polyfills', () => {
338339

339340
const polyfillFiles = await createPolyfillsData(config);
340341
for (const p of polyfillFiles) {
341-
expect(p.content).to.be.a('string', `Polyfill ${p.name} has no content`);
342+
assert.equal(typeof p.content, 'string', `Polyfill ${p.name} has no content`);
342343
cleanupPolyfill(p);
343344
}
344345

345-
expect(polyfillFiles).to.eql([
346+
assert.deepEqual(polyfillFiles, [
346347
{
347348
name: 'systemjs',
348349
type: fileTypes.SCRIPT,

packages/polyfills-loader/test/createPolyfillsLoader.test.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { expect } from 'chai';
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert/strict';
23
import fs from 'fs';
34
import path from 'path';
4-
import { PolyfillsLoaderConfig } from '../src/types.js';
5-
import { createPolyfillsLoader } from '../src/createPolyfillsLoader.js';
6-
import { noModuleSupportTest, fileTypes } from '../src/utils.js';
5+
import type { PolyfillsLoaderConfig } from '../dist/types.js';
6+
import { createPolyfillsLoader } from '../dist/createPolyfillsLoader.js';
7+
import { noModuleSupportTest, fileTypes } from '../dist/utils.js';
78

89
const updateSnapshots = process.argv.includes('--update-snapshots');
910

@@ -14,26 +15,31 @@ interface TestSnapshotArgs {
1415
}
1516

1617
async function testSnapshot({ name, config, expectedFiles = [] }: TestSnapshotArgs) {
17-
const snapshotPath = path.join(__dirname, 'snapshots', 'createPolyfillsLoader', `${name}.js`);
18+
const snapshotPath = path.join(
19+
import.meta.dirname,
20+
'snapshots',
21+
'createPolyfillsLoader',
22+
`${name}.js`,
23+
);
1824
const loader = await createPolyfillsLoader(config);
1925
if (!loader) {
2026
throw new Error('No loader was generated');
2127
}
2228

23-
expect(loader.polyfillFiles.map(f => f.path)).to.eql(expectedFiles);
29+
assert.deepEqual(
30+
loader.polyfillFiles.map(f => f.path),
31+
expectedFiles,
32+
);
2433

2534
if (updateSnapshots) {
2635
fs.writeFileSync(snapshotPath, loader.code, 'utf-8');
2736
} else {
2837
const snapshot = fs.readFileSync(snapshotPath, 'utf-8');
29-
expect(loader.code.trim()).to.equal(snapshot.trim());
38+
assert.equal(loader.code.trim(), snapshot.trim());
3039
}
3140
}
3241

33-
describe('createPolyfillsLoader', function describe() {
34-
// bootup of the first test can take a long time in CI to load all the polyfills
35-
this.timeout(5000);
36-
42+
describe('createPolyfillsLoader', { timeout: 5000 }, () => {
3743
it('generates a loader script with one module resource', async () => {
3844
await testSnapshot({
3945
name: 'module-resource',

packages/polyfills-loader/test/injectPolyfillsLoader.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { expect } from 'chai';
1+
import { describe, it } from 'node:test';
2+
import assert from 'node:assert/strict';
23
import path from 'path';
34
import fs from 'fs';
4-
import { injectPolyfillsLoader } from '../src/injectPolyfillsLoader.js';
5-
import { noModuleSupportTest, fileTypes } from '../src/utils.js';
6-
import { PolyfillsLoaderConfig } from '../src/types.js';
5+
import { injectPolyfillsLoader } from '../dist/injectPolyfillsLoader.js';
6+
import { noModuleSupportTest, fileTypes } from '../dist/utils.js';
7+
import type { PolyfillsLoaderConfig } from '../dist/types.js';
78

89
const updateSnapshots = process.argv.includes('--update-snapshots');
910

@@ -15,14 +16,19 @@ const defaultConfig = {
1516
};
1617

1718
async function testSnapshot(name: string, htmlString: string, config: PolyfillsLoaderConfig) {
18-
const snapshotPath = path.join(__dirname, 'snapshots', 'injectPolyfillsLoader', `${name}.html`);
19+
const snapshotPath = path.join(
20+
import.meta.dirname,
21+
'snapshots',
22+
'injectPolyfillsLoader',
23+
`${name}.html`,
24+
);
1925
const result = await injectPolyfillsLoader(htmlString, config);
2026

2127
if (updateSnapshots) {
2228
fs.writeFileSync(snapshotPath, result.htmlString, 'utf-8');
2329
} else {
2430
const snapshot = fs.readFileSync(snapshotPath, 'utf-8');
25-
expect(result.htmlString.trim()).to.equal(snapshot.trim());
31+
assert.equal(result.htmlString.trim(), snapshot.trim());
2632
}
2733
}
2834

0 commit comments

Comments
 (0)