Skip to content

Commit

Permalink
Use promises in test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jul 28, 2023
1 parent d975dd3 commit f1047a7
Show file tree
Hide file tree
Showing 10 changed files with 923 additions and 1,148 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -38,7 +38,7 @@
"react-intl-webpack-plugin": "^0.3.0",
"rimraf": "^5.0.1",
"semver": "7.5.2",
"webpack": "^5.74.0"
"webpack": "^5.88.2"
},
"scripts": {
"clean": "rimraf lib/",
Expand Down
302 changes: 118 additions & 184 deletions test/cache.test.js
Expand Up @@ -2,8 +2,8 @@ import test from "ava";
import fs from "fs";
import path from "path";
import { rimraf } from "rimraf";
import webpack from "webpack";
import createTestDirectory from "./helpers/createTestDirectory";
import { webpackAsync } from "./helpers/webpackAsync.js";
import createTestDirectory from "./helpers/createTestDirectory.js";

const defaultCacheDir = path.join(
__dirname,
Expand Down Expand Up @@ -34,24 +34,16 @@ 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

test.beforeEach.cb(t => {
createTestDirectory(outputDir, t.title, (err, directory) => {
if (err) return t.end(err);
t.context.directory = directory;
t.end();
});
});
test.beforeEach.cb(t => {
createTestDirectory(cacheDir, t.title, (err, directory) => {
if (err) return t.end(err);
t.context.cacheDirectory = directory;
t.end();
});
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;
});
test.beforeEach(() => rimraf(defaultCacheDir));
test.afterEach(t => rimraf([t.context.directory, t.context.cacheDirectory]));

test.cb("should output files to cache directory", t => {
test("should output files to cache directory", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
Expand All @@ -71,134 +63,101 @@ test.cb("should output files to cache directory", t => {
},
});

webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length > 0);
t.end();
});
});
const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length > 0);
});

test.serial.cb(
"should output json.gz files to standard cache dir by default",
t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
presets: ["@babel/preset-env"],
},
test("should output json.gz files to standard cache dir by default", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
presets: ["@babel/preset-env"],
},
],
},
});

webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
},
],
},
});

fs.readdir(defaultCacheDir, (err, files) => {
files = files.filter(file => CACHE_FILE_REGEX.test(file));
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

t.is(err, null);
t.true(files.length > 0);
t.end();
});
});
},
);
let files = fs.readdirSync(defaultCacheDir);
files = files.filter(file => CACHE_FILE_REGEX.test(file));
t.true(files.length > 0);
});

test.serial.cb(
"should output non-compressed files to standard cache dir when cacheCompression is set to false",
t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
cacheCompression: false,
presets: ["@babel/preset-env"],
},
// eslint-disable-next-line max-len
test("should output non-compressed files to standard cache dir when cacheCompression is set to false", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
cacheCompression: false,
presets: ["@babel/preset-env"],
},
],
},
});

webpack(config, err => {
t.is(err, null);

fs.readdir(defaultCacheDir, (err, files) => {
files = files.filter(file => UNCOMPRESSED_CACHE_FILE_REGEX.test(file));
},
],
},
});

t.is(err, null);
t.true(files.length > 0);
t.end();
});
});
},
);
await webpackAsync(config);
let files = fs.readdirSync(defaultCacheDir);
files = files.filter(file => UNCOMPRESSED_CACHE_FILE_REGEX.test(file));
t.true(files.length > 0);
});

test.serial.cb(
"should output files to standard cache dir if set to true in query",
t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
presets: ["@babel/preset-env"],
},
test("should output files to standard cache dir if set to true in query", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.jsx?/,
loader: babelLoader,
exclude: /node_modules/,
options: {
cacheDirectory: true,
presets: ["@babel/preset-env"],
},
],
},
});

webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

fs.readdir(defaultCacheDir, (err, files) => {
files = files.filter(file => CACHE_FILE_REGEX.test(file));
},
],
},
});

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

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

test.cb("should read from cache directory if cached file exists", t => {
test("should read from cache directory if cached file exists", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
Expand All @@ -220,23 +179,16 @@ test.cb("should read from cache directory if cached file exists", t => {

// @TODO Find a way to know if the file as correctly read without relying on
// Istanbul for coverage.
webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

webpack(config, err => {
t.is(err, null);
fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length > 0);
t.end();
});
});
});
await webpackAsync(config);
const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length > 0);
});

test.cb("should have one file per module", t => {
test("should have one file per module", async t => {
const config = Object.assign({}, globalConfig, {
output: {
path: t.context.directory,
Expand All @@ -256,20 +208,15 @@ test.cb("should have one file per module", t => {
},
});

webpack(config, (err, stats) => {
t.is(err, null);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);

fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length === 3);
t.end();
});
});
const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length === 3);
});

test.cb("should generate a new file if the identifier changes", t => {
test("should generate a new file if the identifier changes", async t => {
const configs = [
Object.assign({}, globalConfig, {
output: {
Expand Down Expand Up @@ -310,27 +257,20 @@ test.cb("should generate a new file if the identifier changes", t => {
},
}),
];
let counter = configs.length;

configs.forEach(config => {
webpack(config, (err, stats) => {
t.is(err, null);
await Promise.allSettled(
configs.map(async config => {
const stats = await webpackAsync(config);
t.deepEqual(stats.compilation.errors, []);
t.deepEqual(stats.compilation.warnings, []);
counter -= 1;
}),
);

if (!counter) {
fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length === 6);
t.end();
});
}
});
});
const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length === 6);
});

test.cb("should allow to specify the .babelrc file", t => {
test("should allow to specify the .babelrc file", async t => {
const config = [
Object.assign({}, globalConfig, {
entry: path.join(__dirname, "fixtures/constant.js"),
Expand Down Expand Up @@ -373,18 +313,12 @@ test.cb("should allow to specify the .babelrc file", t => {
},
}),
];

webpack(config, (err, multiStats) => {
t.is(err, null);
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, []);

fs.readdir(t.context.cacheDirectory, (err, files) => {
t.is(err, null);
t.true(files.length === 2);
t.end();
});
});
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, []);

const files = fs.readdirSync(t.context.cacheDirectory);
t.true(files.length === 2);
});
1 change: 1 addition & 0 deletions test/fixtures/constant.js
@@ -1 +1,2 @@
const LOADER = true;
export default LOADER;

0 comments on commit f1047a7

Please sign in to comment.