Skip to content

Commit

Permalink
Migrate to node test runner (#1028)
Browse files Browse the repository at this point in the history
* migrate to node test runner

* add unit test for serialize
  • Loading branch information
JLHwung authored Jul 15, 2024
1 parent 7cba007 commit 8b3ccab
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 2,444 deletions.
18 changes: 2 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
"webpack": ">=5.61.0"
},
"devDependencies": {
"@ava/babel": "^1.0.1",
"@babel/cli": "^7.23.0",
"@babel/core": "^7.23.3",
"@babel/eslint-parser": "^7.23.3",
"@babel/preset-env": "^7.23.3",
"ava": "^3.13.0",
"c8": "^8.0.0",
"eslint": "^9.6.0",
"eslint-config-prettier": "^9.1.0",
Expand All @@ -43,7 +41,7 @@
"prepublish": "yarn run clean && yarn run build",
"preversion": "yarn run test",
"test": "yarn run lint && yarn run build --source-maps && c8 yarn run test-only",
"test-only": "ava"
"test-only": "node --test test/**/*.test.js"
},
"resolutions": {
"minipass": "6.0.2"
Expand Down Expand Up @@ -78,18 +76,6 @@
"sourceMap": false,
"instrument": false
},
"ava": {
"files": [
"test/**/*.test.js",
"!test/fixtures/**/*",
"!test/helpers/**/*"
],
"babel": {
"compileAsTests": [
"test/helpers/**/*"
]
}
},
"lint-staged": {
"scripts/*.js": [
"prettier --trailing-comma es5 --write",
Expand All @@ -113,4 +99,4 @@
]
},
"packageManager": "[email protected]"
}
}
134 changes: 70 additions & 64 deletions test/cache.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import test from "ava";
import fs from "fs";
import path from "path";
import test from "node:test";
import fs from "node:fs";
import path from "node:path";
import assert from "node:assert/strict";
import { webpackAsync } from "./helpers/webpackAsync.js";
import createTestDirectory from "./helpers/createTestDirectory.js";
import { fileURLToPath } from "node:url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

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

const context = { directory: undefined, cacheDirectory: undefined };

test.beforeEach(async t => {
const directory = await createTestDirectory(outputDir, t.title);
t.context.directory = directory;
const cacheDirectory = await createTestDirectory(cacheDir, t.title);
t.context.cacheDirectory = cacheDirectory;
const directory = await createTestDirectory(outputDir, t.name);
context.directory = directory;
const cacheDirectory = await createTestDirectory(cacheDir, t.name);
context.cacheDirectory = cacheDirectory;
});
test.beforeEach(() =>
fs.rmSync(defaultCacheDir, { recursive: true, force: true }),
);
test.afterEach(t => {
fs.rmSync(t.context.directory, { recursive: true, force: true });
fs.rmSync(t.context.cacheDirectory, { recursive: true, force: true });
test.afterEach(() => {
fs.rmSync(context.directory, { recursive: true, force: true });
fs.rmSync(context.cacheDirectory, { recursive: true, force: true });
});

test("should output files to cache directory", async t => {
test("should output files to cache directory", async () => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -59,7 +65,7 @@ test("should output files to cache directory", async t => {
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: t.context.cacheDirectory,
cacheDirectory: context.cacheDirectory,
presets: ["@babel/preset-env"],
},
},
Expand All @@ -68,17 +74,17 @@ test("should output files to cache directory", async t => {
});

const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
assert.deepEqual(stats.compilation.errors, []);
assert.deepEqual(stats.compilation.warnings, []);

const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length > 0);
const files = fs.readdirSync(context.cacheDirectory);
assert.ok(files.length > 0);
});

test("should output json.gz files to standard cache dir by default", async t => {
test("should output json.gz files to standard cache dir by default", async () => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -96,18 +102,18 @@ test("should output json.gz files to standard cache dir by default", async t =>
});

const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
assert.deepEqual(stats.compilation.errors, []);
assert.deepEqual(stats.compilation.warnings, []);

let files = fs.readdirSync(defaultCacheDir);
files = files.filter(file => CACHE_FILE_REGEX.test(file));
t.true(files.length > 0);
assert.ok(files.length > 0);
});

test("should output non-compressed files to standard cache dir when cacheCompression is set to false", async t => {
test("should output non-compressed files to standard cache dir when cacheCompression is set to false", async () => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -128,13 +134,13 @@ test("should output non-compressed files to standard cache dir when cacheCompres
await webpackAsync(config);
let files = fs.readdirSync(defaultCacheDir);
files = files.filter(file => UNCOMPRESSED_CACHE_FILE_REGEX.test(file));
t.true(files.length > 0);
assert.ok(files.length > 0);
});

test("should output files to standard cache dir if set to true in query", async t => {
test("should output files to standard cache dir if set to true in query", async () => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -152,18 +158,18 @@ test("should output files to standard cache dir if set to true in query", async
});

const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
assert.deepEqual(stats.compilation.errors, []);
assert.deepEqual(stats.compilation.warnings, []);

let files = fs.readdirSync(defaultCacheDir);
files = files.filter(file => CACHE_FILE_REGEX.test(file));
t.true(files.length > 0);
assert.ok(files.length > 0);
});

test("should read from cache directory if cached file exists", async t => {
test("should read from cache directory if cached file exists", async () => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -172,7 +178,7 @@ test("should read from cache directory if cached file exists", async t => {
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: t.context.cacheDirectory,
cacheDirectory: context.cacheDirectory,
presets: ["@babel/preset-env"],
},
},
Expand All @@ -183,18 +189,18 @@ test("should read from cache directory if cached file exists", async t => {
// @TODO Find a way to know if the file as correctly read without relying on
// Istanbul for coverage.
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
assert.deepEqual(stats.compilation.errors, []);
assert.deepEqual(stats.compilation.warnings, []);

await webpackAsync(config);
const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length > 0);
const files = fs.readdirSync(context.cacheDirectory);
assert.ok(files.length > 0);
});

test("should have one file per module", async t => {
test("should have one file per module", async () => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -203,7 +209,7 @@ test("should have one file per module", async t => {
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: t.context.cacheDirectory,
cacheDirectory: context.cacheDirectory,
presets: ["@babel/preset-env"],
},
},
Expand All @@ -212,18 +218,18 @@ test("should have one file per module", async t => {
});

const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
assert.deepEqual(stats.compilation.errors, []);
assert.deepEqual(stats.compilation.warnings, []);

const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length === 3);
const files = fs.readdirSync(context.cacheDirectory);
assert.ok(files.length === 3);
});

test("should generate a new file if the identifier changes", async t => {
test("should generate a new file if the identifier changes", async () => {
const configs = [
Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -232,7 +238,7 @@ test("should generate a new file if the identifier changes", async t => {
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: t.context.cacheDirectory,
cacheDirectory: context.cacheDirectory,
cacheIdentifier: "a",
presets: ["@babel/preset-env"],
},
Expand All @@ -242,7 +248,7 @@ test("should generate a new file if the identifier changes", async t => {
}),
Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -251,7 +257,7 @@ test("should generate a new file if the identifier changes", async t => {
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: t.context.cacheDirectory,
cacheDirectory: context.cacheDirectory,
cacheIdentifier: "b",
presets: ["@babel/preset-env"],
},
Expand All @@ -264,21 +270,21 @@ test("should generate a new file if the identifier changes", async t => {
await Promise.allSettled(
configs.map(async config => {
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
assert.deepEqual(stats.compilation.errors, []);
assert.deepEqual(stats.compilation.warnings, []);
}),
);

const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length === 6);
const files = fs.readdirSync(context.cacheDirectory);
assert.ok(files.length === 6);
});

test("should allow to specify the .babelrc file", async t => {
test("should allow to specify the .babelrc file", async () => {
const config = [
Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/constant.js"),
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -287,7 +293,7 @@ test("should allow to specify the .babelrc file", async t => {
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: t.context.cacheDirectory,
cacheDirectory: context.cacheDirectory,
extends: path.join(__dirname, "fixtures/babelrc"),
babelrc: false,
presets: ["@babel/preset-env"],
Expand All @@ -299,7 +305,7 @@ test("should allow to specify the .babelrc file", async t => {
Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/constant.js"),
output: {
path: t.context.directory,
path: context.directory,
},
module: {
rules: [
Expand All @@ -308,7 +314,7 @@ test("should allow to specify the .babelrc file", async t => {
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: t.context.cacheDirectory,
cacheDirectory: context.cacheDirectory,
presets: ["@babel/preset-env"],
},
},
Expand All @@ -317,13 +323,13 @@ test("should allow to specify the .babelrc file", async t => {
}),
];
const multiStats = await webpackAsync(config);
t.deepEqual(multiStats.stats[0].compilation.errors, []);
t.deepEqual(multiStats.stats[0].compilation.warnings, []);
t.deepEqual(multiStats.stats[1].compilation.errors, []);
t.deepEqual(multiStats.stats[1].compilation.warnings, []);
assert.deepEqual(multiStats.stats[0].compilation.errors, []);
assert.deepEqual(multiStats.stats[0].compilation.warnings, []);
assert.deepEqual(multiStats.stats[1].compilation.errors, []);
assert.deepEqual(multiStats.stats[1].compilation.warnings, []);

const files = fs.readdirSync(t.context.cacheDirectory);
const files = fs.readdirSync(context.cacheDirectory);
// The two configs resolved to same Babel config because "fixtures/babelrc"
// is { "presets": ["@babel/preset-env"] }
t.true(files.length === 1);
assert.ok(files.length === 1);
});
4 changes: 2 additions & 2 deletions test/helpers/createTestDirectory.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from "path";
import fs from "fs/promises";
import path from "node:path";
import fs from "node:fs/promises";

export default async function createTestDirectory(baseDirectory, testTitle) {
const directory = path.join(baseDirectory, escapeDirectory(testTitle));
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/webpackAsync.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import webpack from "webpack";
import { promisify } from "util";
import { promisify } from "node:util";
export const webpackAsync = promisify(webpack);
Loading

0 comments on commit 8b3ccab

Please sign in to comment.