Skip to content

Commit 8b3ccab

Browse files
authored
Migrate to node test runner (#1028)
* migrate to node test runner * add unit test for serialize
1 parent 7cba007 commit 8b3ccab

File tree

11 files changed

+353
-2444
lines changed

11 files changed

+353
-2444
lines changed

package.json

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@
1818
"webpack": ">=5.61.0"
1919
},
2020
"devDependencies": {
21-
"@ava/babel": "^1.0.1",
2221
"@babel/cli": "^7.23.0",
2322
"@babel/core": "^7.23.3",
2423
"@babel/eslint-parser": "^7.23.3",
2524
"@babel/preset-env": "^7.23.3",
26-
"ava": "^3.13.0",
2725
"c8": "^8.0.0",
2826
"eslint": "^9.6.0",
2927
"eslint-config-prettier": "^9.1.0",
@@ -43,7 +41,7 @@
4341
"prepublish": "yarn run clean && yarn run build",
4442
"preversion": "yarn run test",
4543
"test": "yarn run lint && yarn run build --source-maps && c8 yarn run test-only",
46-
"test-only": "ava"
44+
"test-only": "node --test test/**/*.test.js"
4745
},
4846
"resolutions": {
4947
"minipass": "6.0.2"
@@ -78,18 +76,6 @@
7876
"sourceMap": false,
7977
"instrument": false
8078
},
81-
"ava": {
82-
"files": [
83-
"test/**/*.test.js",
84-
"!test/fixtures/**/*",
85-
"!test/helpers/**/*"
86-
],
87-
"babel": {
88-
"compileAsTests": [
89-
"test/helpers/**/*"
90-
]
91-
}
92-
},
9379
"lint-staged": {
9480
"scripts/*.js": [
9581
"prettier --trailing-comma es5 --write",
@@ -113,4 +99,4 @@
11399
]
114100
},
115101
"packageManager": "yarn@3.6.4"
116-
}
102+
}

test/cache.test.js

Lines changed: 70 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import test from "ava";
2-
import fs from "fs";
3-
import path from "path";
1+
import test from "node:test";
2+
import fs from "node:fs";
3+
import path from "node:path";
4+
import assert from "node:assert/strict";
45
import { webpackAsync } from "./helpers/webpackAsync.js";
56
import createTestDirectory from "./helpers/createTestDirectory.js";
7+
import { fileURLToPath } from "node:url";
8+
9+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
610

711
const defaultCacheDir = path.join(
812
__dirname,
@@ -33,24 +37,26 @@ const CACHE_FILE_REGEX = /^[0-9a-f]{32}(?:[0-9a-f]{32})?\.json\.gz$/;
3337
// Create a separate directory for each test so that the tests
3438
// can run in parallel
3539

40+
const context = { directory: undefined, cacheDirectory: undefined };
41+
3642
test.beforeEach(async t => {
37-
const directory = await createTestDirectory(outputDir, t.title);
38-
t.context.directory = directory;
39-
const cacheDirectory = await createTestDirectory(cacheDir, t.title);
40-
t.context.cacheDirectory = cacheDirectory;
43+
const directory = await createTestDirectory(outputDir, t.name);
44+
context.directory = directory;
45+
const cacheDirectory = await createTestDirectory(cacheDir, t.name);
46+
context.cacheDirectory = cacheDirectory;
4147
});
4248
test.beforeEach(() =>
4349
fs.rmSync(defaultCacheDir, { recursive: true, force: true }),
4450
);
45-
test.afterEach(t => {
46-
fs.rmSync(t.context.directory, { recursive: true, force: true });
47-
fs.rmSync(t.context.cacheDirectory, { recursive: true, force: true });
51+
test.afterEach(() => {
52+
fs.rmSync(context.directory, { recursive: true, force: true });
53+
fs.rmSync(context.cacheDirectory, { recursive: true, force: true });
4854
});
4955

50-
test("should output files to cache directory", async t => {
56+
test("should output files to cache directory", async () => {
5157
const config = Object.assign({}, globalConfig, {
5258
output: {
53-
path: t.context.directory,
59+
path: context.directory,
5460
},
5561
module: {
5662
rules: [
@@ -59,7 +65,7 @@ test("should output files to cache directory", async t => {
5965
loader: babelLoader,
6066
exclude: /node_modules/,
6167
options: {
62-
cacheDirectory: t.context.cacheDirectory,
68+
cacheDirectory: context.cacheDirectory,
6369
presets: ["@babel/preset-env"],
6470
},
6571
},
@@ -68,17 +74,17 @@ test("should output files to cache directory", async t => {
6874
});
6975

7076
const stats = await webpackAsync(config);
71-
t.deepEqual(stats.compilation.errors, []);
72-
t.deepEqual(stats.compilation.warnings, []);
77+
assert.deepEqual(stats.compilation.errors, []);
78+
assert.deepEqual(stats.compilation.warnings, []);
7379

74-
const files = fs.readdirSync(t.context.cacheDirectory);
75-
t.true(files.length > 0);
80+
const files = fs.readdirSync(context.cacheDirectory);
81+
assert.ok(files.length > 0);
7682
});
7783

78-
test("should output json.gz files to standard cache dir by default", async t => {
84+
test("should output json.gz files to standard cache dir by default", async () => {
7985
const config = Object.assign({}, globalConfig, {
8086
output: {
81-
path: t.context.directory,
87+
path: context.directory,
8288
},
8389
module: {
8490
rules: [
@@ -96,18 +102,18 @@ test("should output json.gz files to standard cache dir by default", async t =>
96102
});
97103

98104
const stats = await webpackAsync(config);
99-
t.deepEqual(stats.compilation.errors, []);
100-
t.deepEqual(stats.compilation.warnings, []);
105+
assert.deepEqual(stats.compilation.errors, []);
106+
assert.deepEqual(stats.compilation.warnings, []);
101107

102108
let files = fs.readdirSync(defaultCacheDir);
103109
files = files.filter(file => CACHE_FILE_REGEX.test(file));
104-
t.true(files.length > 0);
110+
assert.ok(files.length > 0);
105111
});
106112

107-
test("should output non-compressed files to standard cache dir when cacheCompression is set to false", async t => {
113+
test("should output non-compressed files to standard cache dir when cacheCompression is set to false", async () => {
108114
const config = Object.assign({}, globalConfig, {
109115
output: {
110-
path: t.context.directory,
116+
path: context.directory,
111117
},
112118
module: {
113119
rules: [
@@ -128,13 +134,13 @@ test("should output non-compressed files to standard cache dir when cacheCompres
128134
await webpackAsync(config);
129135
let files = fs.readdirSync(defaultCacheDir);
130136
files = files.filter(file => UNCOMPRESSED_CACHE_FILE_REGEX.test(file));
131-
t.true(files.length > 0);
137+
assert.ok(files.length > 0);
132138
});
133139

134-
test("should output files to standard cache dir if set to true in query", async t => {
140+
test("should output files to standard cache dir if set to true in query", async () => {
135141
const config = Object.assign({}, globalConfig, {
136142
output: {
137-
path: t.context.directory,
143+
path: context.directory,
138144
},
139145
module: {
140146
rules: [
@@ -152,18 +158,18 @@ test("should output files to standard cache dir if set to true in query", async
152158
});
153159

154160
const stats = await webpackAsync(config);
155-
t.deepEqual(stats.compilation.errors, []);
156-
t.deepEqual(stats.compilation.warnings, []);
161+
assert.deepEqual(stats.compilation.errors, []);
162+
assert.deepEqual(stats.compilation.warnings, []);
157163

158164
let files = fs.readdirSync(defaultCacheDir);
159165
files = files.filter(file => CACHE_FILE_REGEX.test(file));
160-
t.true(files.length > 0);
166+
assert.ok(files.length > 0);
161167
});
162168

163-
test("should read from cache directory if cached file exists", async t => {
169+
test("should read from cache directory if cached file exists", async () => {
164170
const config = Object.assign({}, globalConfig, {
165171
output: {
166-
path: t.context.directory,
172+
path: context.directory,
167173
},
168174
module: {
169175
rules: [
@@ -172,7 +178,7 @@ test("should read from cache directory if cached file exists", async t => {
172178
loader: babelLoader,
173179
exclude: /node_modules/,
174180
options: {
175-
cacheDirectory: t.context.cacheDirectory,
181+
cacheDirectory: context.cacheDirectory,
176182
presets: ["@babel/preset-env"],
177183
},
178184
},
@@ -183,18 +189,18 @@ test("should read from cache directory if cached file exists", async t => {
183189
// @TODO Find a way to know if the file as correctly read without relying on
184190
// Istanbul for coverage.
185191
const stats = await webpackAsync(config);
186-
t.deepEqual(stats.compilation.errors, []);
187-
t.deepEqual(stats.compilation.warnings, []);
192+
assert.deepEqual(stats.compilation.errors, []);
193+
assert.deepEqual(stats.compilation.warnings, []);
188194

189195
await webpackAsync(config);
190-
const files = fs.readdirSync(t.context.cacheDirectory);
191-
t.true(files.length > 0);
196+
const files = fs.readdirSync(context.cacheDirectory);
197+
assert.ok(files.length > 0);
192198
});
193199

194-
test("should have one file per module", async t => {
200+
test("should have one file per module", async () => {
195201
const config = Object.assign({}, globalConfig, {
196202
output: {
197-
path: t.context.directory,
203+
path: context.directory,
198204
},
199205
module: {
200206
rules: [
@@ -203,7 +209,7 @@ test("should have one file per module", async t => {
203209
loader: babelLoader,
204210
exclude: /node_modules/,
205211
options: {
206-
cacheDirectory: t.context.cacheDirectory,
212+
cacheDirectory: context.cacheDirectory,
207213
presets: ["@babel/preset-env"],
208214
},
209215
},
@@ -212,18 +218,18 @@ test("should have one file per module", async t => {
212218
});
213219

214220
const stats = await webpackAsync(config);
215-
t.deepEqual(stats.compilation.errors, []);
216-
t.deepEqual(stats.compilation.warnings, []);
221+
assert.deepEqual(stats.compilation.errors, []);
222+
assert.deepEqual(stats.compilation.warnings, []);
217223

218-
const files = fs.readdirSync(t.context.cacheDirectory);
219-
t.true(files.length === 3);
224+
const files = fs.readdirSync(context.cacheDirectory);
225+
assert.ok(files.length === 3);
220226
});
221227

222-
test("should generate a new file if the identifier changes", async t => {
228+
test("should generate a new file if the identifier changes", async () => {
223229
const configs = [
224230
Object.assign({}, globalConfig, {
225231
output: {
226-
path: t.context.directory,
232+
path: context.directory,
227233
},
228234
module: {
229235
rules: [
@@ -232,7 +238,7 @@ test("should generate a new file if the identifier changes", async t => {
232238
loader: babelLoader,
233239
exclude: /node_modules/,
234240
options: {
235-
cacheDirectory: t.context.cacheDirectory,
241+
cacheDirectory: context.cacheDirectory,
236242
cacheIdentifier: "a",
237243
presets: ["@babel/preset-env"],
238244
},
@@ -242,7 +248,7 @@ test("should generate a new file if the identifier changes", async t => {
242248
}),
243249
Object.assign({}, globalConfig, {
244250
output: {
245-
path: t.context.directory,
251+
path: context.directory,
246252
},
247253
module: {
248254
rules: [
@@ -251,7 +257,7 @@ test("should generate a new file if the identifier changes", async t => {
251257
loader: babelLoader,
252258
exclude: /node_modules/,
253259
options: {
254-
cacheDirectory: t.context.cacheDirectory,
260+
cacheDirectory: context.cacheDirectory,
255261
cacheIdentifier: "b",
256262
presets: ["@babel/preset-env"],
257263
},
@@ -264,21 +270,21 @@ test("should generate a new file if the identifier changes", async t => {
264270
await Promise.allSettled(
265271
configs.map(async config => {
266272
const stats = await webpackAsync(config);
267-
t.deepEqual(stats.compilation.errors, []);
268-
t.deepEqual(stats.compilation.warnings, []);
273+
assert.deepEqual(stats.compilation.errors, []);
274+
assert.deepEqual(stats.compilation.warnings, []);
269275
}),
270276
);
271277

272-
const files = fs.readdirSync(t.context.cacheDirectory);
273-
t.true(files.length === 6);
278+
const files = fs.readdirSync(context.cacheDirectory);
279+
assert.ok(files.length === 6);
274280
});
275281

276-
test("should allow to specify the .babelrc file", async t => {
282+
test("should allow to specify the .babelrc file", async () => {
277283
const config = [
278284
Object.assign({}, globalConfig, {
279285
entry: path.join(__dirname, "fixtures/constant.js"),
280286
output: {
281-
path: t.context.directory,
287+
path: context.directory,
282288
},
283289
module: {
284290
rules: [
@@ -287,7 +293,7 @@ test("should allow to specify the .babelrc file", async t => {
287293
loader: babelLoader,
288294
exclude: /node_modules/,
289295
options: {
290-
cacheDirectory: t.context.cacheDirectory,
296+
cacheDirectory: context.cacheDirectory,
291297
extends: path.join(__dirname, "fixtures/babelrc"),
292298
babelrc: false,
293299
presets: ["@babel/preset-env"],
@@ -299,7 +305,7 @@ test("should allow to specify the .babelrc file", async t => {
299305
Object.assign({}, globalConfig, {
300306
entry: path.join(__dirname, "fixtures/constant.js"),
301307
output: {
302-
path: t.context.directory,
308+
path: context.directory,
303309
},
304310
module: {
305311
rules: [
@@ -308,7 +314,7 @@ test("should allow to specify the .babelrc file", async t => {
308314
loader: babelLoader,
309315
exclude: /node_modules/,
310316
options: {
311-
cacheDirectory: t.context.cacheDirectory,
317+
cacheDirectory: context.cacheDirectory,
312318
presets: ["@babel/preset-env"],
313319
},
314320
},
@@ -317,13 +323,13 @@ test("should allow to specify the .babelrc file", async t => {
317323
}),
318324
];
319325
const multiStats = await webpackAsync(config);
320-
t.deepEqual(multiStats.stats[0].compilation.errors, []);
321-
t.deepEqual(multiStats.stats[0].compilation.warnings, []);
322-
t.deepEqual(multiStats.stats[1].compilation.errors, []);
323-
t.deepEqual(multiStats.stats[1].compilation.warnings, []);
326+
assert.deepEqual(multiStats.stats[0].compilation.errors, []);
327+
assert.deepEqual(multiStats.stats[0].compilation.warnings, []);
328+
assert.deepEqual(multiStats.stats[1].compilation.errors, []);
329+
assert.deepEqual(multiStats.stats[1].compilation.warnings, []);
324330

325-
const files = fs.readdirSync(t.context.cacheDirectory);
331+
const files = fs.readdirSync(context.cacheDirectory);
326332
// The two configs resolved to same Babel config because "fixtures/babelrc"
327333
// is { "presets": ["@babel/preset-env"] }
328-
t.true(files.length === 1);
334+
assert.ok(files.length === 1);
329335
});

test/helpers/createTestDirectory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import path from "path";
2-
import fs from "fs/promises";
1+
import path from "node:path";
2+
import fs from "node:fs/promises";
33

44
export default async function createTestDirectory(baseDirectory, testTitle) {
55
const directory = path.join(baseDirectory, escapeDirectory(testTitle));

test/helpers/webpackAsync.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import webpack from "webpack";
2-
import { promisify } from "util";
2+
import { promisify } from "node:util";
33
export const webpackAsync = promisify(webpack);

0 commit comments

Comments
 (0)