@rspack-canary/test-tools 1.5.6-canary-e598f284-20250921173624 → 1.5.8-canary-e350b761-20250924173613
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/case/builtin.d.ts +2 -0
- package/dist/case/builtin.js +173 -13
- package/dist/case/cache.d.ts +1 -1
- package/dist/case/cache.js +210 -11
- package/dist/case/common.d.ts +10 -0
- package/dist/case/common.js +237 -0
- package/dist/case/compiler.d.ts +7 -4
- package/dist/case/compiler.js +144 -109
- package/dist/case/config.d.ts +5 -1
- package/dist/case/config.js +102 -16
- package/dist/case/defaults.d.ts +12 -4
- package/dist/case/defaults.js +67 -12
- package/dist/case/diagnostic.d.ts +6 -0
- package/dist/case/diagnostic.js +123 -14
- package/dist/case/diff.d.ts +17 -1
- package/dist/case/diff.js +170 -22
- package/dist/case/error.d.ts +14 -4
- package/dist/case/error.js +117 -10
- package/dist/case/hash.d.ts +1 -1
- package/dist/case/hash.js +70 -9
- package/dist/case/hook.d.ts +35 -4
- package/dist/case/hook.js +210 -21
- package/dist/case/hot-step.js +307 -9
- package/dist/case/hot.d.ts +8 -1
- package/dist/case/hot.js +235 -8
- package/dist/case/incremental.d.ts +1 -1
- package/dist/case/incremental.js +39 -41
- package/dist/case/index.d.ts +20 -20
- package/dist/case/index.js +44 -34
- package/dist/case/native-watcher.js +10 -23
- package/dist/case/normal.js +174 -12
- package/dist/case/runner.d.ts +18 -0
- package/dist/case/runner.js +108 -0
- package/dist/case/serial.d.ts +1 -1
- package/dist/case/serial.js +8 -12
- package/dist/case/stats-api.d.ts +0 -5
- package/dist/case/stats-api.js +71 -10
- package/dist/case/stats-output.js +197 -11
- package/dist/case/treeshaking.js +34 -7
- package/dist/case/watch.d.ts +27 -0
- package/dist/case/watch.js +321 -21
- package/dist/helper/plugins/hot-update.d.ts +2 -2
- package/dist/index.d.ts +0 -2
- package/dist/index.js +0 -2
- package/dist/runner/index.d.ts +2 -7
- package/dist/runner/index.js +2 -7
- package/dist/runner/{runner/node → node}/index.d.ts +1 -2
- package/dist/runner/{runner/node → node}/index.js +2 -2
- package/dist/runner/{runner/web → web}/fake.d.ts +1 -2
- package/dist/runner/{runner/web → web}/fake.js +7 -7
- package/dist/runner/{runner/web → web}/index.d.ts +2 -2
- package/dist/runner/{runner/web → web}/index.js +1 -1
- package/dist/runner/{runner/web → web}/jsdom.d.ts +1 -2
- package/dist/runner/{runner/web → web}/jsdom.js +4 -4
- package/dist/test/context.d.ts +3 -5
- package/dist/test/context.js +22 -12
- package/dist/test/creator.d.ts +13 -12
- package/dist/test/creator.js +52 -43
- package/dist/test/tester.js +4 -1
- package/dist/type.d.ts +42 -11
- package/dist/type.js +7 -1
- package/package.json +5 -5
- package/dist/processor/basic.d.ts +0 -24
- package/dist/processor/basic.js +0 -147
- package/dist/processor/builtin.d.ts +0 -9
- package/dist/processor/builtin.js +0 -171
- package/dist/processor/cache.d.ts +0 -20
- package/dist/processor/cache.js +0 -131
- package/dist/processor/config.d.ts +0 -11
- package/dist/processor/config.js +0 -88
- package/dist/processor/defaults.d.ts +0 -30
- package/dist/processor/defaults.js +0 -72
- package/dist/processor/diagnostic.d.ts +0 -15
- package/dist/processor/diagnostic.js +0 -104
- package/dist/processor/diff.d.ts +0 -30
- package/dist/processor/diff.js +0 -140
- package/dist/processor/error.d.ts +0 -23
- package/dist/processor/error.js +0 -95
- package/dist/processor/hash.d.ts +0 -10
- package/dist/processor/hash.js +0 -65
- package/dist/processor/hook.d.ts +0 -44
- package/dist/processor/hook.js +0 -206
- package/dist/processor/hot-incremental.d.ts +0 -14
- package/dist/processor/hot-incremental.js +0 -43
- package/dist/processor/hot-step.d.ts +0 -18
- package/dist/processor/hot-step.js +0 -307
- package/dist/processor/hot.d.ts +0 -17
- package/dist/processor/hot.js +0 -147
- package/dist/processor/index.d.ts +0 -20
- package/dist/processor/index.js +0 -36
- package/dist/processor/multi.d.ts +0 -17
- package/dist/processor/multi.js +0 -73
- package/dist/processor/normal.d.ts +0 -12
- package/dist/processor/normal.js +0 -170
- package/dist/processor/simple.d.ts +0 -24
- package/dist/processor/simple.js +0 -51
- package/dist/processor/snapshot.d.ts +0 -12
- package/dist/processor/snapshot.js +0 -66
- package/dist/processor/stats-api.d.ts +0 -18
- package/dist/processor/stats-api.js +0 -48
- package/dist/processor/stats.d.ts +0 -18
- package/dist/processor/stats.js +0 -206
- package/dist/processor/treeshaking.d.ts +0 -10
- package/dist/processor/treeshaking.js +0 -33
- package/dist/processor/watch.d.ts +0 -30
- package/dist/processor/watch.js +0 -252
- package/dist/runner/basic.d.ts +0 -10
- package/dist/runner/basic.js +0 -64
- package/dist/runner/cache.d.ts +0 -5
- package/dist/runner/cache.js +0 -92
- package/dist/runner/hot.d.ts +0 -5
- package/dist/runner/hot.js +0 -91
- package/dist/runner/multiple.d.ts +0 -11
- package/dist/runner/multiple.js +0 -52
- package/dist/runner/runner/index.d.ts +0 -2
- package/dist/runner/runner/index.js +0 -18
- package/dist/runner/type.d.ts +0 -42
- package/dist/runner/type.js +0 -9
- package/dist/runner/watch.d.ts +0 -7
- package/dist/runner/watch.js +0 -71
- package/dist/test/simple.d.ts +0 -5
- package/dist/test/simple.js +0 -43
package/dist/case/error.js
CHANGED
|
@@ -1,21 +1,128 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.createErrorCase = createErrorCase;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
+
const webpack_merge_1 = __importDefault(require("webpack-merge"));
|
|
9
|
+
const creator_1 = require("../test/creator");
|
|
10
|
+
const common_1 = require("./common");
|
|
7
11
|
let addedSerializer = false;
|
|
12
|
+
const creator = new creator_1.BasicCaseCreator({
|
|
13
|
+
clean: true,
|
|
14
|
+
describe: true,
|
|
15
|
+
steps: ({ name, caseConfig }) => {
|
|
16
|
+
const config = caseConfig;
|
|
17
|
+
return [
|
|
18
|
+
{
|
|
19
|
+
config: async (context) => {
|
|
20
|
+
const compiler = (0, common_1.getCompiler)(context, name);
|
|
21
|
+
compiler.setOptions(options(context, config.options));
|
|
22
|
+
},
|
|
23
|
+
compiler: async (context) => {
|
|
24
|
+
const compilerManager = (0, common_1.getCompiler)(context, name);
|
|
25
|
+
compilerManager.createCompiler();
|
|
26
|
+
compiler(context, compilerManager.getCompiler(), config.compiler);
|
|
27
|
+
},
|
|
28
|
+
build: async (context) => {
|
|
29
|
+
const compiler = (0, common_1.getCompiler)(context, name);
|
|
30
|
+
if (typeof config.build === "function") {
|
|
31
|
+
await config.build(context, compiler.getCompiler());
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
await compiler.build();
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
run: async (env, context) => {
|
|
38
|
+
// no need to run, just check the snapshot of diagnostics
|
|
39
|
+
},
|
|
40
|
+
check: async (env, context) => {
|
|
41
|
+
await check(env, context, name, config.check);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
];
|
|
45
|
+
},
|
|
46
|
+
concurrent: true
|
|
47
|
+
});
|
|
8
48
|
function createErrorCase(name, src, dist, testConfig) {
|
|
9
49
|
if (!addedSerializer) {
|
|
10
50
|
addedSerializer = true;
|
|
11
51
|
}
|
|
12
52
|
const caseConfig = require(testConfig);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
name: name,
|
|
17
|
-
compilerType: type_1.ECompilerType.Rspack,
|
|
18
|
-
...caseConfig
|
|
19
|
-
}));
|
|
53
|
+
creator.create(name, src, dist, undefined, {
|
|
54
|
+
caseConfig,
|
|
55
|
+
description: () => caseConfig.description
|
|
20
56
|
});
|
|
21
57
|
}
|
|
58
|
+
function options(context, custom) {
|
|
59
|
+
let options = {
|
|
60
|
+
context: node_path_1.default.resolve(__dirname, "../../../../tests/rspack-test/fixtures/errors"),
|
|
61
|
+
mode: "none",
|
|
62
|
+
devtool: false,
|
|
63
|
+
optimization: {
|
|
64
|
+
minimize: false,
|
|
65
|
+
moduleIds: "named",
|
|
66
|
+
chunkIds: "named"
|
|
67
|
+
},
|
|
68
|
+
experiments: {
|
|
69
|
+
css: true,
|
|
70
|
+
rspackFuture: {
|
|
71
|
+
bundlerInfo: {
|
|
72
|
+
force: false
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
if (typeof custom === "function") {
|
|
78
|
+
options = (0, webpack_merge_1.default)(options, custom(context, options));
|
|
79
|
+
}
|
|
80
|
+
if (options.mode === "production") {
|
|
81
|
+
if (options.optimization)
|
|
82
|
+
options.optimization.minimize = true;
|
|
83
|
+
else
|
|
84
|
+
options.optimization = { minimize: true };
|
|
85
|
+
}
|
|
86
|
+
return options;
|
|
87
|
+
}
|
|
88
|
+
async function compiler(context, compiler, custom) {
|
|
89
|
+
if (compiler) {
|
|
90
|
+
compiler.outputFileSystem = {
|
|
91
|
+
// CHANGE: rspack outputFileSystem `mkdirp` uses option `{ recursive: true }`, webpack's second parameter is alway a callback
|
|
92
|
+
mkdir(dir, maybeOptionOrCallback, maybeCallback) {
|
|
93
|
+
if (typeof maybeOptionOrCallback === "function") {
|
|
94
|
+
maybeOptionOrCallback();
|
|
95
|
+
}
|
|
96
|
+
else if (typeof maybeCallback === "function") {
|
|
97
|
+
maybeCallback();
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
writeFile(file, content, callback) {
|
|
101
|
+
callback();
|
|
102
|
+
},
|
|
103
|
+
stat(file, callback) {
|
|
104
|
+
callback(new Error("ENOENT"));
|
|
105
|
+
},
|
|
106
|
+
mkdirSync() { },
|
|
107
|
+
writeFileSync() { }
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
await custom?.(context, compiler);
|
|
111
|
+
}
|
|
112
|
+
class RspackStatsDiagnostics {
|
|
113
|
+
constructor(errors, warnings) {
|
|
114
|
+
this.errors = errors;
|
|
115
|
+
this.warnings = warnings;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async function check(env, context, name, check) {
|
|
119
|
+
const compiler = (0, common_1.getCompiler)(context, name);
|
|
120
|
+
const stats = compiler.getStats();
|
|
121
|
+
env.expect(typeof stats).toBe("object");
|
|
122
|
+
const statsResult = stats.toJson({ errorDetails: false });
|
|
123
|
+
env.expect(typeof statsResult).toBe("object");
|
|
124
|
+
const { errors, warnings } = statsResult;
|
|
125
|
+
env.expect(Array.isArray(errors)).toBe(true);
|
|
126
|
+
env.expect(Array.isArray(warnings)).toBe(true);
|
|
127
|
+
await check?.(new RspackStatsDiagnostics(errors, warnings));
|
|
128
|
+
}
|
package/dist/case/hash.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { ECompilerType,
|
|
1
|
+
import type { ECompilerType, TTestConfig } from "../type";
|
|
2
2
|
export type THashCaseConfig = Pick<TTestConfig<ECompilerType.Rspack>, "validate">;
|
|
3
3
|
export declare function createHashCase(name: string, src: string, dist: string): void;
|
package/dist/case/hash.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createHashCase = createHashCase;
|
|
4
|
-
const processor_1 = require("../processor");
|
|
5
4
|
const creator_1 = require("../test/creator");
|
|
6
|
-
const
|
|
5
|
+
const common_1 = require("./common");
|
|
6
|
+
const REG_ERROR_CASE = /error$/;
|
|
7
7
|
class HashCaseCreator extends creator_1.BasicCaseCreator {
|
|
8
|
-
describe(name, tester, testConfig) {
|
|
8
|
+
describe(name, tester, testConfig, options) {
|
|
9
9
|
it(`should print correct hash for ${name}`, async () => {
|
|
10
10
|
await tester.prepare();
|
|
11
11
|
await tester.compile();
|
|
12
|
-
await tester.check(this.createEnv(testConfig));
|
|
12
|
+
await tester.check(this.createEnv(testConfig, options));
|
|
13
13
|
await tester.resume();
|
|
14
14
|
}, 30000);
|
|
15
15
|
}
|
|
@@ -18,13 +18,74 @@ const creator = new HashCaseCreator({
|
|
|
18
18
|
clean: true,
|
|
19
19
|
describe: false,
|
|
20
20
|
steps: ({ name }) => [
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
{
|
|
22
|
+
config: async (context) => {
|
|
23
|
+
(0, common_1.configMultiCompiler)(context, name, ["rspack.config.js", "webpack.config.js"], defaultOptions, overrideOptions);
|
|
24
|
+
},
|
|
25
|
+
compiler: async (context) => {
|
|
26
|
+
await (0, common_1.compiler)(context, name);
|
|
27
|
+
},
|
|
28
|
+
build: async (context) => {
|
|
29
|
+
await (0, common_1.build)(context, name);
|
|
30
|
+
},
|
|
31
|
+
run: async (env, context) => {
|
|
32
|
+
// no need to run, just check snapshot
|
|
33
|
+
},
|
|
34
|
+
check: async (env, context) => {
|
|
35
|
+
await check(env, context, name);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
26
38
|
]
|
|
27
39
|
});
|
|
28
40
|
function createHashCase(name, src, dist) {
|
|
29
41
|
creator.create(name, src, dist);
|
|
30
42
|
}
|
|
43
|
+
function defaultOptions(index, context) {
|
|
44
|
+
return {
|
|
45
|
+
context: context.getSource(),
|
|
46
|
+
output: {
|
|
47
|
+
path: context.getDist()
|
|
48
|
+
},
|
|
49
|
+
experiments: {
|
|
50
|
+
css: true,
|
|
51
|
+
rspackFuture: {
|
|
52
|
+
bundlerInfo: {
|
|
53
|
+
force: false
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
inlineConst: true,
|
|
57
|
+
lazyBarrel: true
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function overrideOptions(index, context, options) {
|
|
62
|
+
if (!options.entry) {
|
|
63
|
+
options.entry = "./index.js";
|
|
64
|
+
}
|
|
65
|
+
if (!global.printLogger) {
|
|
66
|
+
options.infrastructureLogging = {
|
|
67
|
+
level: "error"
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
async function check(env, context, name) {
|
|
72
|
+
const compiler = (0, common_1.getCompiler)(context, name);
|
|
73
|
+
const stats = compiler.getStats();
|
|
74
|
+
const testConfig = context.getTestConfig();
|
|
75
|
+
if (!stats) {
|
|
76
|
+
env.expect(false);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (REG_ERROR_CASE.test(name)) {
|
|
80
|
+
env.expect(stats.hasErrors());
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
env.expect(!stats.hasErrors());
|
|
84
|
+
}
|
|
85
|
+
if (typeof testConfig.validate === "function") {
|
|
86
|
+
testConfig.validate(stats);
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
throw new Error("HashTestCases should have test.config.js and a validate method");
|
|
90
|
+
}
|
|
91
|
+
}
|
package/dist/case/hook.d.ts
CHANGED
|
@@ -1,6 +1,37 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
import { ECompilerType } from "../type";
|
|
3
|
-
export
|
|
1
|
+
import { TestContext, type TTestContextOptions } from "../test/context";
|
|
2
|
+
import type { ECompilerType, ITestContext, ITestEnv, TCompiler, TCompilerOptions } from "../type";
|
|
3
|
+
export declare function createHookCase(name: string, src: string, dist: string, source: string): void;
|
|
4
|
+
export declare class HookCasesContext extends TestContext {
|
|
5
|
+
protected src: string;
|
|
6
|
+
protected testName: string;
|
|
7
|
+
protected options: TTestContextOptions;
|
|
8
|
+
protected promises: Promise<void>[];
|
|
9
|
+
protected count: number;
|
|
10
|
+
protected snapshots: Record<string | number, Array<[string | Buffer, string]>>;
|
|
11
|
+
protected snapshotsList: Array<string | number>;
|
|
12
|
+
constructor(src: string, testName: string, options: TTestContextOptions);
|
|
13
|
+
/**
|
|
14
|
+
* Snapshot function arguments and return value.
|
|
15
|
+
* Generated snapshot is located in the same directory with the test source.
|
|
16
|
+
* @example
|
|
17
|
+
* compiler.hooks.compilation("name", context.snapped((...args) => { ... }))
|
|
18
|
+
*/
|
|
19
|
+
snapped(cb: (...args: unknown[]) => Promise<unknown>, prefix?: string): (this: any, ...args: unknown[]) => Promise<unknown>;
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
_addSnapshot(content: unknown, name: string, group: string | number): void;
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
collectSnapshots(env: ITestEnv, options?: {
|
|
28
|
+
diff: {};
|
|
29
|
+
}): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
export type THookCaseConfig = {
|
|
32
|
+
options?: (context: ITestContext) => TCompilerOptions<ECompilerType.Rspack>;
|
|
33
|
+
compiler?: (context: ITestContext, compiler: TCompiler<ECompilerType.Rspack>) => Promise<void>;
|
|
34
|
+
check?: (context: ITestContext) => Promise<void>;
|
|
35
|
+
snapshotFileFilter?: (file: string) => boolean;
|
|
4
36
|
description: string;
|
|
5
37
|
};
|
|
6
|
-
export declare function createHookCase(name: string, src: string, dist: string, source: string): void;
|
package/dist/case/hook.js
CHANGED
|
@@ -3,33 +3,222 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.HookCasesContext = void 0;
|
|
6
7
|
exports.createHookCase = createHookCase;
|
|
7
8
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
9
|
+
const core_1 = require("@rspack/core");
|
|
10
|
+
const jest_snapshot_1 = require("jest-snapshot");
|
|
11
|
+
const path_serializer_1 = require("path-serializer");
|
|
12
|
+
const pretty_format_1 = require("pretty-format");
|
|
13
|
+
const webpack_merge_1 = __importDefault(require("webpack-merge"));
|
|
14
|
+
const context_1 = require("../test/context");
|
|
15
|
+
const creator_1 = require("../test/creator");
|
|
16
|
+
const common_1 = require("./common");
|
|
17
|
+
const srcDir = node_path_1.default.resolve(__dirname, "../../../../tests/rspack-test/fixtures");
|
|
18
|
+
const distDir = node_path_1.default.resolve(__dirname, "../../../../tests/rspack-test/js/hook");
|
|
19
|
+
const creator = new creator_1.BasicCaseCreator({
|
|
20
|
+
clean: true,
|
|
21
|
+
describe: true,
|
|
22
|
+
createContext: (config) => new HookCasesContext(config.src, config.name, config),
|
|
23
|
+
steps: ({ name, caseConfig: _caseConfig, src }) => {
|
|
24
|
+
const caseConfig = _caseConfig;
|
|
25
|
+
return [
|
|
26
|
+
{
|
|
27
|
+
config: async (context) => {
|
|
28
|
+
const compiler = (0, common_1.getCompiler)(context, name);
|
|
29
|
+
const options = await (0, common_1.config)(context, name, ["rspack.config.js", "webpack.config.js"], defaultOptions(context, caseConfig.options));
|
|
30
|
+
if (!global.printLogger) {
|
|
31
|
+
options.infrastructureLogging = {
|
|
32
|
+
level: "error"
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
compiler.setOptions(options);
|
|
36
|
+
},
|
|
37
|
+
compiler: async (context) => {
|
|
38
|
+
const c = await (0, common_1.compiler)(context, name);
|
|
39
|
+
if (caseConfig.compiler) {
|
|
40
|
+
await caseConfig.compiler(context, c);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
build: async (context) => {
|
|
44
|
+
await (0, common_1.build)(context, name);
|
|
45
|
+
},
|
|
46
|
+
run: async (env, context) => {
|
|
47
|
+
// no need to run, just check snapshot
|
|
48
|
+
},
|
|
49
|
+
check: async (env, context) => {
|
|
50
|
+
await (0, common_1.checkSnapshot)(env, context, name, node_path_1.default.join(src, "output.snap.txt"), caseConfig.snapshotFileFilter);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
});
|
|
13
56
|
function createHookCase(name, src, dist, source) {
|
|
14
57
|
const caseConfig = require(node_path_1.default.join(src, "test.js"));
|
|
15
58
|
const testName = node_path_1.default.basename(name.slice(0, name.indexOf(node_path_1.default.extname(name))));
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
59
|
+
creator.create(name, src, dist, undefined, {
|
|
60
|
+
caseConfig,
|
|
61
|
+
description: () => caseConfig.description,
|
|
62
|
+
createContext: (config) => new HookCasesContext(src, testName, {
|
|
19
63
|
src: source,
|
|
20
|
-
dist: dist
|
|
21
|
-
runnerFactory: runner_1.BasicRunnerFactory
|
|
64
|
+
dist: dist
|
|
22
65
|
})
|
|
23
66
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
67
|
+
}
|
|
68
|
+
const sourceSerializer = {
|
|
69
|
+
test(val) {
|
|
70
|
+
return val instanceof core_1.sources.Source;
|
|
71
|
+
},
|
|
72
|
+
print(val) {
|
|
73
|
+
return val.source();
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
const internalSerializer = {
|
|
77
|
+
test(val) {
|
|
78
|
+
return val instanceof core_1.Compiler || val instanceof core_1.Compilation;
|
|
79
|
+
},
|
|
80
|
+
print(val) {
|
|
81
|
+
return JSON.stringify(`${val.constructor.name}(internal ignored)`);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
const testPathSerializer = (0, path_serializer_1.createSnapshotSerializer)({
|
|
85
|
+
replace: [
|
|
86
|
+
{
|
|
87
|
+
match: srcDir,
|
|
88
|
+
mark: "<HOOK_SRC_DIR>"
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
match: distDir,
|
|
92
|
+
mark: "<HOOK_DIST_DIR>"
|
|
93
|
+
}
|
|
94
|
+
]
|
|
95
|
+
});
|
|
96
|
+
const escapeRegex = true;
|
|
97
|
+
const printFunctionName = false;
|
|
98
|
+
const normalizeNewlines = (str) => str.replace(/\r\n|\r/g, "\n");
|
|
99
|
+
const serialize = (val, indent = 2, formatOverrides = {}) => normalizeNewlines((0, pretty_format_1.format)(val, {
|
|
100
|
+
escapeRegex,
|
|
101
|
+
indent,
|
|
102
|
+
plugins: [
|
|
103
|
+
...(0, jest_snapshot_1.getSerializers)(),
|
|
104
|
+
sourceSerializer,
|
|
105
|
+
internalSerializer,
|
|
106
|
+
testPathSerializer
|
|
107
|
+
],
|
|
108
|
+
printFunctionName,
|
|
109
|
+
...formatOverrides
|
|
110
|
+
}));
|
|
111
|
+
class HookCasesContext extends context_1.TestContext {
|
|
112
|
+
constructor(src, testName, options) {
|
|
113
|
+
super(options);
|
|
114
|
+
this.src = src;
|
|
115
|
+
this.testName = testName;
|
|
116
|
+
this.options = options;
|
|
117
|
+
this.promises = [];
|
|
118
|
+
this.count = 0;
|
|
119
|
+
this.snapshots = {};
|
|
120
|
+
this.snapshotsList = [];
|
|
121
|
+
this.snapped = this.snapped.bind(this);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Snapshot function arguments and return value.
|
|
125
|
+
* Generated snapshot is located in the same directory with the test source.
|
|
126
|
+
* @example
|
|
127
|
+
* compiler.hooks.compilation("name", context.snapped((...args) => { ... }))
|
|
128
|
+
*/
|
|
129
|
+
snapped(cb, prefix = "") {
|
|
130
|
+
// eslint-disable-next-line
|
|
131
|
+
const context = this;
|
|
132
|
+
return function SNAPPED_HOOK(...args) {
|
|
133
|
+
const group = prefix ? prefix : context.count++;
|
|
134
|
+
context._addSnapshot(args, "input", group);
|
|
135
|
+
const output = cb.apply(this, args);
|
|
136
|
+
if (output && typeof output.then === "function") {
|
|
137
|
+
let resolve;
|
|
138
|
+
context.promises.push(new Promise(r => (resolve = r)));
|
|
139
|
+
return output
|
|
140
|
+
.then((o) => {
|
|
141
|
+
context._addSnapshot(o, "output (Promise resolved)", group);
|
|
142
|
+
return o;
|
|
143
|
+
})
|
|
144
|
+
.catch((o) => {
|
|
145
|
+
context._addSnapshot(o, "output (Promise rejected)", group);
|
|
146
|
+
return o;
|
|
147
|
+
})
|
|
148
|
+
.finally(resolve);
|
|
149
|
+
}
|
|
150
|
+
context._addSnapshot(output, "output", group);
|
|
151
|
+
return output;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* @internal
|
|
156
|
+
*/
|
|
157
|
+
_addSnapshot(content, name, group) {
|
|
158
|
+
const normalizedContent = Buffer.isBuffer(content)
|
|
159
|
+
? content
|
|
160
|
+
: serialize(content, undefined, {
|
|
161
|
+
escapeString: true,
|
|
162
|
+
printBasicPrototype: true
|
|
163
|
+
}).replace(/\r\n/g, "\n");
|
|
164
|
+
(this.snapshots[group] = this.snapshots[group] || []).push([
|
|
165
|
+
normalizedContent,
|
|
166
|
+
name
|
|
167
|
+
]);
|
|
168
|
+
if (!this.snapshotsList.includes(group)) {
|
|
169
|
+
this.snapshotsList.push(group);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* @internal
|
|
174
|
+
*/
|
|
175
|
+
async collectSnapshots(env, options = {
|
|
176
|
+
diff: {}
|
|
177
|
+
}) {
|
|
178
|
+
await Promise.allSettled(this.promises);
|
|
179
|
+
if (!this.snapshotsList.length)
|
|
180
|
+
return;
|
|
181
|
+
const snapshots = this.snapshotsList.reduce((acc, group, index) => {
|
|
182
|
+
const block = this.snapshots[group || index].reduce((acc, [content, name]) => {
|
|
183
|
+
name = `## ${name || `test: ${index}`}\n\n`;
|
|
184
|
+
const block = `\`\`\`javascript\n${content}\n\`\`\`\n`;
|
|
185
|
+
return `${acc}${name + block}\n`;
|
|
186
|
+
}, "");
|
|
187
|
+
return `${acc}# ${Number.isInteger(group) ? `Group: ${index}` : group}\n\n${block}`;
|
|
188
|
+
}, "");
|
|
189
|
+
env
|
|
190
|
+
.expect(snapshots)
|
|
191
|
+
.toMatchFileSnapshot(node_path_1.default.join(this.src, "hooks.snap.txt"), options);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
exports.HookCasesContext = HookCasesContext;
|
|
195
|
+
function defaultOptions(context, custom) {
|
|
196
|
+
let defaultOptions = {
|
|
197
|
+
context: context.getSource(),
|
|
198
|
+
mode: "production",
|
|
199
|
+
target: "async-node",
|
|
200
|
+
devtool: false,
|
|
201
|
+
cache: false,
|
|
202
|
+
entry: "./hook",
|
|
203
|
+
output: {
|
|
204
|
+
path: context.getDist()
|
|
205
|
+
},
|
|
206
|
+
optimization: {
|
|
207
|
+
minimize: false
|
|
208
|
+
},
|
|
209
|
+
experiments: {
|
|
210
|
+
css: true,
|
|
211
|
+
rspackFuture: {
|
|
212
|
+
bundlerInfo: {
|
|
213
|
+
force: false
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
inlineConst: true,
|
|
217
|
+
lazyBarrel: true
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
if (custom) {
|
|
221
|
+
defaultOptions = (0, webpack_merge_1.default)(defaultOptions, custom(context));
|
|
222
|
+
}
|
|
223
|
+
return defaultOptions;
|
|
35
224
|
}
|