@rspack/test-tools 0.6.3 → 0.6.4

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.
Files changed (82) hide show
  1. package/dist/case/builtin.js +1 -1
  2. package/dist/case/compiler.d.ts +5 -0
  3. package/dist/case/compiler.js +18 -0
  4. package/dist/case/config.js +1 -1
  5. package/dist/case/defaults.js +2 -2
  6. package/dist/case/diagnostic.js +2 -7
  7. package/dist/case/error.d.ts +6 -0
  8. package/dist/case/error.js +23 -0
  9. package/dist/case/hook.d.ts +1 -0
  10. package/dist/case/hook.js +37 -0
  11. package/dist/case/hot-step.js +1 -1
  12. package/dist/case/index.d.ts +5 -1
  13. package/dist/case/index.js +5 -1
  14. package/dist/case/normal.js +2 -2
  15. package/dist/case/stats-api.d.ts +6 -0
  16. package/dist/case/stats-api.js +23 -0
  17. package/dist/case/stats-output.d.ts +1 -0
  18. package/dist/case/{stats.js → stats-output.js} +3 -3
  19. package/dist/case/treeshaking.js +1 -1
  20. package/dist/helper/directory.d.ts +7 -3
  21. package/dist/helper/directory.js +60 -31
  22. package/dist/helper/index.d.ts +1 -0
  23. package/dist/helper/index.js +1 -0
  24. package/dist/helper/jestFileSnapshot.d.ts +18 -0
  25. package/dist/helper/jestFileSnapshot.js +112 -0
  26. package/dist/helper/legacy/fakeSystem.d.ts +9 -0
  27. package/dist/helper/legacy/fakeSystem.js +124 -0
  28. package/dist/helper/legacy/supportsWorker.d.ts +2 -0
  29. package/dist/helper/legacy/supportsWorker.js +17 -0
  30. package/dist/helper/legacy/warmup-webpack.d.ts +1 -0
  31. package/dist/helper/legacy/warmup-webpack.js +26 -0
  32. package/dist/helper/setupTestFramework.d.ts +1 -0
  33. package/dist/helper/setupTestFramework.js +121 -0
  34. package/dist/helper/util/checkSourceMap.d.ts +1 -0
  35. package/dist/helper/util/checkSourceMap.js +80 -0
  36. package/dist/helper/util/currentWatchStep.d.ts +1 -0
  37. package/dist/helper/util/currentWatchStep.js +2 -0
  38. package/dist/helper/util/expectWarningFactory.d.ts +2 -0
  39. package/dist/helper/util/expectWarningFactory.js +21 -0
  40. package/dist/helper/util/filterUtil.d.ts +8 -0
  41. package/dist/helper/util/filterUtil.js +50 -0
  42. package/dist/helper/util/identifier.d.ts +76 -0
  43. package/dist/helper/util/identifier.js +345 -0
  44. package/dist/helper/util/replaceMitteDiagnostic.d.ts +2 -0
  45. package/dist/helper/util/replaceMitteDiagnostic.js +17 -0
  46. package/dist/helper/win.d.ts +2 -0
  47. package/dist/helper/win.js +15 -0
  48. package/dist/index.d.ts +1 -0
  49. package/dist/index.js +1 -0
  50. package/dist/plugin/webpack-module-placeholder-plugin.js +2 -3
  51. package/dist/processor/defaults.js +11 -23
  52. package/dist/processor/diagnostic.d.ts +0 -3
  53. package/dist/processor/diagnostic.js +17 -20
  54. package/dist/processor/error.d.ts +23 -0
  55. package/dist/processor/error.js +142 -0
  56. package/dist/processor/hook.d.ts +41 -0
  57. package/dist/processor/hook.js +189 -0
  58. package/dist/processor/hot-step.d.ts +2 -1
  59. package/dist/processor/hot-step.js +66 -14
  60. package/dist/processor/index.d.ts +2 -0
  61. package/dist/processor/index.js +2 -0
  62. package/dist/processor/normal.d.ts +0 -2
  63. package/dist/processor/normal.js +1 -10
  64. package/dist/processor/simple.js +2 -6
  65. package/dist/processor/snapshot.js +6 -6
  66. package/dist/processor/stats-api.d.ts +0 -2
  67. package/dist/processor/stats-api.js +1 -13
  68. package/dist/processor/stats.js +2 -1
  69. package/dist/processor/watch.js +4 -4
  70. package/dist/runner/hot-step.js +3 -2
  71. package/dist/runner/runner/basic.d.ts +1 -0
  72. package/dist/runner/runner/basic.js +3 -0
  73. package/dist/runner/runner/web/jsdom.d.ts +1 -0
  74. package/dist/runner/runner/web/jsdom.js +11 -5
  75. package/dist/runner/runner/web.d.ts +1 -0
  76. package/dist/runner/runner/web.js +3 -0
  77. package/dist/runner/type.d.ts +13 -0
  78. package/dist/test/simple.d.ts +5 -0
  79. package/dist/test/simple.js +36 -0
  80. package/dist/type.d.ts +1 -0
  81. package/package.json +11 -6
  82. package/dist/case/stats.d.ts +0 -1
@@ -15,7 +15,7 @@ const FILTERS = {
15
15
  };
16
16
  const creator = new creator_1.BasicCaseCreator({
17
17
  clean: true,
18
- describe: true,
18
+ describe: false,
19
19
  description(name) {
20
20
  return `${name} should match snapshot`;
21
21
  },
@@ -0,0 +1,5 @@
1
+ import { ISimpleProcessorOptions } from "../processor";
2
+ export type TCompilerCaseConfig = Omit<ISimpleProcessorOptions, "name" | "compilerType"> & {
3
+ description: string;
4
+ };
5
+ export declare function createCompilerCase(name: string, src: string, dist: string, testConfig: string): void;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCompilerCase = void 0;
4
+ const type_1 = require("../type");
5
+ const processor_1 = require("../processor");
6
+ const simple_1 = require("../test/simple");
7
+ function createCompilerCase(name, src, dist, testConfig) {
8
+ const caseConfig = require(testConfig);
9
+ const runner = (0, simple_1.getSimpleProcessorRunner)(src, dist);
10
+ it(caseConfig.description, async () => {
11
+ await runner(name, new processor_1.SimpleTaskProcessor({
12
+ name: name,
13
+ compilerType: type_1.ECompilerType.Rspack,
14
+ ...caseConfig
15
+ }));
16
+ });
17
+ }
18
+ exports.createCompilerCase = createCompilerCase;
@@ -6,7 +6,7 @@ const runner_1 = require("../runner");
6
6
  const creator_1 = require("../test/creator");
7
7
  const creator = new creator_1.BasicCaseCreator({
8
8
  clean: true,
9
- describe: true,
9
+ describe: false,
10
10
  steps: ({ name }) => [
11
11
  new config_1.RspackConfigProcessor({
12
12
  name,
@@ -7,8 +7,8 @@ exports.createDefaultsCase = void 0;
7
7
  const context_1 = require("../test/context");
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const processor_1 = require("../processor");
10
- const srcDir = path_1.default.resolve(__dirname, "../../../rspack/tests/fixtures");
11
- const distDir = path_1.default.resolve(__dirname, "../../../rspack/tests/js/compiler");
10
+ const srcDir = path_1.default.resolve(__dirname, "../../tests/fixtures");
11
+ const distDir = path_1.default.resolve(__dirname, "../../tests/js/defaults");
12
12
  const context = new context_1.TestContext({
13
13
  src: srcDir,
14
14
  dist: distDir
@@ -1,19 +1,14 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.createDiagnosticCase = void 0;
7
- const path_1 = __importDefault(require("path"));
8
4
  const processor_1 = require("../processor");
9
5
  const creator_1 = require("../test/creator");
10
6
  const creator = new creator_1.BasicCaseCreator({
11
7
  clean: true,
12
- describe: true,
8
+ describe: false,
13
9
  steps: ({ name }) => [
14
10
  new processor_1.RspackDiagnosticProcessor({
15
- name,
16
- root: path_1.default.resolve(__dirname, "../../../rspack")
11
+ name
17
12
  })
18
13
  ]
19
14
  });
@@ -0,0 +1,6 @@
1
+ import { ECompilerType } from "../type";
2
+ import { IErrorTaskProcessorOptions } from "../processor";
3
+ export type TErrorCaseConfig = Omit<IErrorTaskProcessorOptions<ECompilerType.Rspack>, "name" | "compilerType"> & {
4
+ description: string;
5
+ };
6
+ export declare function createErrorCase(name: string, src: string, dist: string, testConfig: string): void;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createErrorCase = void 0;
4
+ const type_1 = require("../type");
5
+ const simple_1 = require("../test/simple");
6
+ const processor_1 = require("../processor");
7
+ let addedSerializer = false;
8
+ function createErrorCase(name, src, dist, testConfig) {
9
+ if (!addedSerializer) {
10
+ processor_1.ErrorTaskProcessor.addSnapshotSerializer();
11
+ addedSerializer = true;
12
+ }
13
+ const caseConfig = require(testConfig);
14
+ const runner = (0, simple_1.getSimpleProcessorRunner)(src, dist);
15
+ it(caseConfig.description, async () => {
16
+ await runner(name, new processor_1.ErrorTaskProcessor({
17
+ name: name,
18
+ compilerType: type_1.ECompilerType.Rspack,
19
+ ...caseConfig
20
+ }));
21
+ });
22
+ }
23
+ exports.createErrorCase = createErrorCase;
@@ -0,0 +1 @@
1
+ export declare function createHookCase(name: string, src: string, dist: string, source: string): void;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createHookCase = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const simple_1 = require("../test/simple");
9
+ const processor_1 = require("../processor");
10
+ const runner_1 = require("../runner");
11
+ const type_1 = require("../type");
12
+ const createLazyTestEnv_1 = __importDefault(require("../helper/legacy/createLazyTestEnv"));
13
+ function createHookCase(name, src, dist, source) {
14
+ const caseConfig = require(path_1.default.join(src, "test.js"));
15
+ const testName = path_1.default.basename(name.slice(0, name.indexOf(path_1.default.extname(name))));
16
+ const runner = (0, simple_1.getSimpleProcessorRunner)(source, dist, {
17
+ env: () => env,
18
+ context: () => new processor_1.HookCasesContext(src, testName, {
19
+ src: source,
20
+ dist: dist,
21
+ runnerFactory: runner_1.BasicRunnerFactory
22
+ })
23
+ });
24
+ it(caseConfig.description, async () => {
25
+ await runner(name, new processor_1.HookTaskProcessor({
26
+ name,
27
+ compilerType: type_1.ECompilerType.Rspack,
28
+ findBundle: function (i, options) {
29
+ return ["main.js"];
30
+ },
31
+ snapshot: path_1.default.join(src, "output.snap.txt"),
32
+ ...caseConfig
33
+ }));
34
+ });
35
+ const env = (0, createLazyTestEnv_1.default)(10000);
36
+ }
37
+ exports.createHookCase = createHookCase;
@@ -9,7 +9,7 @@ function getCreator(target) {
9
9
  if (!creators.has(target)) {
10
10
  creators.set(target, new creator_1.BasicCaseCreator({
11
11
  clean: true,
12
- describe: true,
12
+ describe: false,
13
13
  target,
14
14
  steps: ({ name, target }) => [
15
15
  new hot_step_1.RspackHotStepProcessor({
@@ -4,9 +4,13 @@ export * from "./diff";
4
4
  export * from "./hash";
5
5
  export * from "./hot";
6
6
  export * from "./normal";
7
- export * from "./stats";
7
+ export * from "./stats-output";
8
8
  export * from "./watch";
9
9
  export * from "./treeshaking";
10
10
  export * from "./defaults";
11
11
  export * from "./builtin";
12
12
  export * from "./hot-step";
13
+ export * from "./compiler";
14
+ export * from "./stats-api";
15
+ export * from "./error";
16
+ export * from "./hook";
@@ -20,9 +20,13 @@ __exportStar(require("./diff"), exports);
20
20
  __exportStar(require("./hash"), exports);
21
21
  __exportStar(require("./hot"), exports);
22
22
  __exportStar(require("./normal"), exports);
23
- __exportStar(require("./stats"), exports);
23
+ __exportStar(require("./stats-output"), exports);
24
24
  __exportStar(require("./watch"), exports);
25
25
  __exportStar(require("./treeshaking"), exports);
26
26
  __exportStar(require("./defaults"), exports);
27
27
  __exportStar(require("./builtin"), exports);
28
28
  __exportStar(require("./hot-step"), exports);
29
+ __exportStar(require("./compiler"), exports);
30
+ __exportStar(require("./stats-api"), exports);
31
+ __exportStar(require("./error"), exports);
32
+ __exportStar(require("./hook"), exports);
@@ -10,11 +10,11 @@ const creator_1 = require("../test/creator");
10
10
  const runner_1 = require("../runner");
11
11
  const creator = new creator_1.BasicCaseCreator({
12
12
  clean: true,
13
- describe: true,
13
+ describe: false,
14
14
  steps: ({ name }) => [
15
15
  new normal_1.RspackNormalProcessor({
16
16
  name,
17
- root: path_1.default.resolve(__dirname, "../../../rspack/tests/cases"),
17
+ root: path_1.default.resolve(__dirname, "../../tests/normalCases"),
18
18
  compilerOptions: {},
19
19
  runable: true
20
20
  })
@@ -0,0 +1,6 @@
1
+ import { ECompilerType } from "../type";
2
+ import { IStatsAPITaskProcessorOptions } from "../processor";
3
+ export type TStatsAPICaseConfig = Omit<IStatsAPITaskProcessorOptions<ECompilerType.Rspack>, "name" | "compilerType"> & {
4
+ description: string;
5
+ };
6
+ export declare function createStatsAPICase(name: string, src: string, dist: string, testConfig: string): void;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createStatsAPICase = void 0;
4
+ const type_1 = require("../type");
5
+ const simple_1 = require("../test/simple");
6
+ const processor_1 = require("../processor");
7
+ let addedSerializer = false;
8
+ function createStatsAPICase(name, src, dist, testConfig) {
9
+ if (!addedSerializer) {
10
+ processor_1.StatsAPITaskProcessor.addSnapshotSerializer();
11
+ addedSerializer = true;
12
+ }
13
+ const caseConfig = require(testConfig);
14
+ const runner = (0, simple_1.getSimpleProcessorRunner)(src, dist);
15
+ it(caseConfig.description, async () => {
16
+ await runner(name, new processor_1.StatsAPITaskProcessor({
17
+ name: name,
18
+ compilerType: type_1.ECompilerType.Rspack,
19
+ ...caseConfig
20
+ }));
21
+ });
22
+ }
23
+ exports.createStatsAPICase = createStatsAPICase;
@@ -0,0 +1 @@
1
+ export declare function createStatsOutputCase(name: string, src: string, dist: string): void;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createStatsCase = void 0;
3
+ exports.createStatsOutputCase = void 0;
4
4
  const stats_1 = require("../processor/stats");
5
5
  const creator_1 = require("../test/creator");
6
6
  const creator = new creator_1.BasicCaseCreator({
@@ -13,7 +13,7 @@ const creator = new creator_1.BasicCaseCreator({
13
13
  ],
14
14
  description: name => `should print correct stats for ${name}`
15
15
  });
16
- function createStatsCase(name, src, dist) {
16
+ function createStatsOutputCase(name, src, dist) {
17
17
  creator.create(name, src, dist);
18
18
  }
19
- exports.createStatsCase = createStatsCase;
19
+ exports.createStatsOutputCase = createStatsOutputCase;
@@ -5,7 +5,7 @@ const treeshaking_1 = require("../processor/treeshaking");
5
5
  const creator_1 = require("../test/creator");
6
6
  const creator = new creator_1.BasicCaseCreator({
7
7
  clean: true,
8
- describe: true,
8
+ describe: false,
9
9
  description(name, step) {
10
10
  if (step === 0) {
11
11
  return `${name} with builtin.treeShaking should match snapshot`;
@@ -1,6 +1,10 @@
1
1
  export declare const isDirectory: (p: string) => boolean;
2
+ export declare const isFile: (p: string) => boolean;
2
3
  export declare const isValidCaseDirectory: (name: string) => boolean;
3
- export declare function describeByWalk(name: string, sourceBase: string, distBase: string, createCase: (name: string, src: string, dist: string) => void, whitelist?: {
4
- cat?: RegExp;
5
- case?: RegExp;
4
+ export declare function describeByWalk(testFile: string, createCase: (name: string, src: string, dist: string) => void, options?: {
5
+ type?: "file" | "directory";
6
+ level?: number;
7
+ source?: string;
8
+ dist?: string;
9
+ absoluteDist?: boolean;
6
10
  }): void;
@@ -3,42 +3,71 @@ 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.describeByWalk = exports.isValidCaseDirectory = exports.isDirectory = void 0;
6
+ exports.describeByWalk = exports.isValidCaseDirectory = exports.isFile = exports.isDirectory = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
+ const _1 = require(".");
9
10
  const isDirectory = (p) => fs_1.default.lstatSync(p).isDirectory();
10
11
  exports.isDirectory = isDirectory;
11
- const isValidCaseDirectory = (name) => !name.startsWith("_") && !name.startsWith(".");
12
+ const isFile = (p) => fs_1.default.lstatSync(p).isFile();
13
+ exports.isFile = isFile;
14
+ const isValidCaseDirectory = (name) => !name.startsWith("_") && !name.startsWith(".") && name !== "node_modules";
12
15
  exports.isValidCaseDirectory = isValidCaseDirectory;
13
- function describeByWalk(name, sourceBase, distBase, createCase, whitelist = {}) {
14
- const categories = fs_1.default
15
- .readdirSync(sourceBase)
16
- .filter(exports.isValidCaseDirectory)
17
- .filter(folder => (0, exports.isDirectory)(path_1.default.join(sourceBase, folder)))
18
- .filter(i => ((whitelist === null || whitelist === void 0 ? void 0 : whitelist.cat) ? whitelist.cat.test(i) : true))
19
- .map(cat => {
20
- return {
21
- name: cat,
22
- tests: fs_1.default
23
- .readdirSync(path_1.default.join(sourceBase, cat))
24
- .filter(exports.isValidCaseDirectory)
25
- .filter(folder => (0, exports.isDirectory)(path_1.default.join(sourceBase, cat, folder)))
26
- .filter(i => ((whitelist === null || whitelist === void 0 ? void 0 : whitelist.case) ? whitelist.case.test(i) : true))
27
- .sort()
28
- };
29
- });
30
- describe(name, () => {
31
- for (let { name: catName, tests } of categories) {
32
- if (tests.length === 0)
33
- continue;
34
- describe(catName, () => {
35
- for (const testName of tests) {
36
- const src = path_1.default.join(sourceBase, catName, testName);
37
- const dist = path_1.default.join(distBase, catName, testName);
38
- createCase(testName, src, dist);
39
- }
40
- });
41
- }
16
+ function describeByWalk(testFile, createCase, options = {}) {
17
+ var _a;
18
+ const testBasename = path_1.default
19
+ .basename(testFile)
20
+ .replace(/\.(diff|hot)?test\.js/, "");
21
+ const testId = testBasename.charAt(0).toLowerCase() + testBasename.slice(1);
22
+ const sourceBase = options.source || path_1.default.join(path_1.default.dirname(testFile), `${testId}Cases`);
23
+ const distBase = options.dist || path_1.default.join(path_1.default.dirname(testFile), "js", testId);
24
+ const level = options.level || 2;
25
+ const type = options.type || "directory";
26
+ const absoluteDist = (_a = options.absoluteDist) !== null && _a !== void 0 ? _a : true;
27
+ function describeDirectory(dirname, currentLevel) {
28
+ fs_1.default.readdirSync(path_1.default.join(sourceBase, dirname))
29
+ .filter(exports.isValidCaseDirectory)
30
+ .filter(folder => {
31
+ const p = path_1.default.join(sourceBase, dirname, folder);
32
+ if (type === "file" && currentLevel === 1) {
33
+ return (0, exports.isFile)(p);
34
+ }
35
+ else if (type === "directory" || currentLevel > 1) {
36
+ return (0, exports.isDirectory)(p);
37
+ }
38
+ else {
39
+ return false;
40
+ }
41
+ })
42
+ .map(folder => {
43
+ const caseName = path_1.default.join(dirname, folder);
44
+ if (currentLevel > 1) {
45
+ describeDirectory(caseName, currentLevel - 1);
46
+ }
47
+ else {
48
+ const name = (0, _1.escapeSep)(path_1.default.join(testId, caseName).split(".").shift());
49
+ describe(name, () => {
50
+ let source = path_1.default.join(sourceBase, caseName);
51
+ let dist = "";
52
+ if (absoluteDist) {
53
+ dist = path_1.default.join(distBase, caseName);
54
+ }
55
+ else {
56
+ const relativeDist = options.dist || "dist";
57
+ if (path_1.default.isAbsolute(relativeDist)) {
58
+ dist = path_1.default.join(relativeDist, caseName);
59
+ }
60
+ else {
61
+ dist = path_1.default.join(sourceBase, caseName, relativeDist);
62
+ }
63
+ }
64
+ createCase(folder, source, dist);
65
+ });
66
+ }
67
+ });
68
+ }
69
+ describe(testId, () => {
70
+ describeDirectory("", level);
42
71
  });
43
72
  }
44
73
  exports.describeByWalk = describeByWalk;
@@ -1,3 +1,4 @@
1
1
  export * from "./read-config-file";
2
2
  export * from "./parse-modules";
3
3
  export * from "./directory";
4
+ export * from "./win";
@@ -17,3 +17,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./read-config-file"), exports);
18
18
  __exportStar(require("./parse-modules"), exports);
19
19
  __exportStar(require("./directory"), exports);
20
+ __exportStar(require("./win"), exports);
@@ -0,0 +1,18 @@
1
+ export function toMatchFileSnapshot(this: {
2
+ testPath: string;
3
+ currentTestName: string;
4
+ assertionCalls: number;
5
+ isNot: boolean;
6
+ snapshotState: {
7
+ added: number;
8
+ updated: number;
9
+ unmatched: number;
10
+ _updateSnapshot: 'none' | 'new' | 'all';
11
+ };
12
+ }, content: string | Buffer, filepath?: string | undefined, options?: {
13
+ diff?: import('jest-diff').DiffOptions;
14
+ }): {
15
+ pass: boolean;
16
+ message: () => string;
17
+ };
18
+ import { diff } from "jest-diff";
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ // @ts-nocheck
3
+ /* istanbul ignore file */
4
+ const fs = require("fs");
5
+ const path = require("path");
6
+ const chalk = require("chalk");
7
+ const { diff } = require("jest-diff");
8
+ const mkdirp = require("mkdirp");
9
+ const filenamify = require("filenamify");
10
+ /**
11
+ * Check if 2 strings or buffer are equal
12
+ * @param {string | Buffer} a
13
+ * @param {string | Buffer} b
14
+ */
15
+ const isEqual = (a, b) => {
16
+ // @ts-ignore: TypeScript gives error if we pass string to buffer.equals
17
+ return Buffer.isBuffer(a) ? a.equals(b) : a === b;
18
+ };
19
+ /**
20
+ * Match given content against content of the specified file.
21
+ *
22
+ * @param {string | Buffer} content Output content to match
23
+ * @param {string} [filepath] Path to the file to match against
24
+ * @param {{ diff?: import('jest-diff').DiffOptions }} options Additional options for matching
25
+ * @this {{ testPath: string, currentTestName: string, assertionCalls: number, isNot: boolean, snapshotState: { added: number, updated: number, unmatched: number, _updateSnapshot: 'none' | 'new' | 'all' } }}
26
+ */
27
+ exports.toMatchFileSnapshot = function toMatchFileSnapshot(content, filepath, options = {}) {
28
+ const { isNot, snapshotState } = this;
29
+ const filename = filepath === undefined
30
+ ? // If file name is not specified, generate one from the test title
31
+ path.join(path.dirname(this.testPath), "__file_snapshots__", `${filenamify(this.currentTestName, {
32
+ replacement: "-"
33
+ }).replace(/\s/g, "-")}-${this.assertionCalls}`)
34
+ : filepath;
35
+ options = {
36
+ // Options for jest-diff
37
+ diff: Object.assign({
38
+ expand: false,
39
+ contextLines: 5,
40
+ aAnnotation: "Snapshot"
41
+ }, options.diff || {})
42
+ };
43
+ if (snapshotState._updateSnapshot === "none" && !fs.existsSync(filename)) {
44
+ // We're probably running in CI environment
45
+ snapshotState.unmatched++;
46
+ return {
47
+ pass: isNot,
48
+ message: () => `New output file ${chalk.blue(path.basename(filename))} was ${chalk.bold.red("not written")}.\n\n` +
49
+ "The update flag must be explicitly passed to write a new snapshot.\n\n" +
50
+ `This is likely because this test is run in a ${chalk.blue("continuous integration (CI) environment")} in which snapshots are not written by default.\n\n`
51
+ };
52
+ }
53
+ if (fs.existsSync(filename)) {
54
+ const output = fs
55
+ .readFileSync(filename, Buffer.isBuffer(content) ? null : "utf8")
56
+ .replace(/\r\n/g, "\n");
57
+ if (isNot) {
58
+ // The matcher is being used with `.not`
59
+ if (!isEqual(content, output)) {
60
+ // The value of `pass` is reversed when used with `.not`
61
+ return { pass: false, message: () => "" };
62
+ }
63
+ else {
64
+ snapshotState.unmatched++;
65
+ return {
66
+ pass: true,
67
+ message: () => `Expected received content ${chalk.red("to not match")} the file ${chalk.blue(path.basename(filename))}.`
68
+ };
69
+ }
70
+ }
71
+ else {
72
+ if (isEqual(content, output)) {
73
+ return { pass: true, message: () => "" };
74
+ }
75
+ else {
76
+ if (snapshotState._updateSnapshot === "all") {
77
+ mkdirp.sync(path.dirname(filename));
78
+ fs.writeFileSync(filename, content);
79
+ snapshotState.updated++;
80
+ return { pass: true, message: () => "" };
81
+ }
82
+ else {
83
+ snapshotState.unmatched++;
84
+ const difference = Buffer.isBuffer(content) || Buffer.isBuffer(output)
85
+ ? ""
86
+ : `\n\n${diff(output, content, options.diff)}`;
87
+ return {
88
+ pass: false,
89
+ message: () => `Received content ${chalk.red("doesn't match")} the file ${chalk.blue(path.basename(filename))}.${difference}`
90
+ };
91
+ }
92
+ }
93
+ }
94
+ }
95
+ else {
96
+ if (!isNot &&
97
+ (snapshotState._updateSnapshot === "new" ||
98
+ snapshotState._updateSnapshot === "all")) {
99
+ mkdirp.sync(path.dirname(filename));
100
+ fs.writeFileSync(filename, content);
101
+ snapshotState.added++;
102
+ return { pass: true, message: () => "" };
103
+ }
104
+ else {
105
+ snapshotState.unmatched++;
106
+ return {
107
+ pass: true,
108
+ message: () => `The output file ${chalk.blue(path.basename(filename))} ${chalk.bold.red("doesn't exist")}.`
109
+ };
110
+ }
111
+ }
112
+ };
@@ -0,0 +1,9 @@
1
+ export function register(name: any, deps: any, fn: any): void;
2
+ export function set(name: any, exports: any): void;
3
+ export const registry: undefined;
4
+ export const _require: undefined;
5
+ export const _nextName: string;
6
+ export function setRequire(req: any): void;
7
+ export function init(modules: any): void;
8
+ export function execute(name: any): any;
9
+ export function ensureExecuted(name: any): any;