@rspack/test-tools 1.0.0-beta.0 → 1.0.0-beta.2

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 (41) hide show
  1. package/dist/case/config.js +11 -0
  2. package/dist/compiler.d.ts +1 -1
  3. package/dist/helper/directory.d.ts +1 -0
  4. package/dist/helper/directory.js +10 -0
  5. package/dist/helper/expect/to-be-typeof.js +2 -12
  6. package/dist/helper/expect/to-end-with.js +2 -12
  7. package/dist/helper/expect/to-match-file-snapshot.js +1 -3
  8. package/dist/helper/legacy/FakeDocument.d.ts +2 -0
  9. package/dist/helper/legacy/FakeDocument.js +34 -11
  10. package/dist/helper/legacy/checkArrayExpectation.js +13 -12
  11. package/dist/helper/legacy/copyDiff.js +1 -1
  12. package/dist/helper/legacy/walkCssTokens.d.ts +38 -0
  13. package/dist/helper/legacy/walkCssTokens.js +761 -0
  14. package/dist/helper/setup-env.js +1 -1
  15. package/dist/helper/util/checkSourceMap.js +1 -1
  16. package/dist/helper/util/identifier.js +5 -5
  17. package/dist/plugin/webpack-module-placeholder-plugin.d.ts +0 -1
  18. package/dist/plugin/webpack-module-placeholder-plugin.js +2 -3
  19. package/dist/processor/config.d.ts +1 -1
  20. package/dist/processor/config.js +12 -2
  21. package/dist/processor/hook.js +2 -2
  22. package/dist/processor/hot.js +6 -0
  23. package/dist/processor/normal.js +1 -1
  24. package/dist/processor/stats.js +3 -5
  25. package/dist/processor/watch.js +2 -1
  26. package/dist/reporter/diff-stats.js +1 -1
  27. package/dist/runner/basic.js +1 -2
  28. package/dist/runner/hot-step.js +4 -4
  29. package/dist/runner/hot.js +4 -4
  30. package/dist/runner/runner/basic.js +1 -1
  31. package/dist/runner/runner/cjs.js +1 -1
  32. package/dist/runner/runner/esm.js +2 -2
  33. package/dist/runner/runner/watch.d.ts +4 -4
  34. package/dist/runner/runner/watch.js +5 -9
  35. package/dist/runner/runner/web/fake.js +1 -0
  36. package/dist/runner/runner/web/jsdom.js +10 -0
  37. package/dist/runner/watch.js +8 -2
  38. package/dist/test/creator.d.ts +1 -0
  39. package/dist/test/creator.js +3 -0
  40. package/dist/type.d.ts +2 -2
  41. package/package.json +3 -3
@@ -8,6 +8,17 @@ const type_1 = require("../type");
8
8
  const creator = new creator_1.BasicCaseCreator({
9
9
  clean: true,
10
10
  describe: false,
11
+ testConfig: testConfig => {
12
+ const oldModuleScope = testConfig.moduleScope;
13
+ testConfig.moduleScope = (ms, stats) => {
14
+ let res = ms;
15
+ // TODO: modify runner module scope based on stats here
16
+ if (typeof oldModuleScope === "function") {
17
+ res = oldModuleScope(ms, stats);
18
+ }
19
+ return res;
20
+ };
21
+ },
11
22
  steps: ({ name }) => [
12
23
  new config_1.ConfigProcessor({
13
24
  name,
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
2
  import EventEmitter from "node:events";
3
3
  import { ECompilerType, type ITestCompilerManager, type TCompiler, type TCompilerFactories, type TCompilerOptions, type TCompilerStats } from "./type";
4
- export declare const enum ECompilerEvent {
4
+ export declare enum ECompilerEvent {
5
5
  Build = "build",
6
6
  Option = "option",
7
7
  Create = "create",
@@ -9,4 +9,5 @@ export declare function describeByWalk(testFile: string, createCase: (name: stri
9
9
  dist?: string;
10
10
  absoluteDist?: boolean;
11
11
  describe?: jest.Describe;
12
+ exclude?: RegExp[];
12
13
  }): void;
@@ -27,6 +27,16 @@ function describeByWalk(testFile, createCase, options = {}) {
27
27
  function describeDirectory(dirname, currentLevel) {
28
28
  node_fs_1.default.readdirSync(node_path_1.default.join(sourceBase, dirname))
29
29
  .filter(exports.isValidCaseDirectory)
30
+ .filter(folder => {
31
+ if (options.exclude) {
32
+ if (options.exclude.some(exclude => {
33
+ return exclude.test(folder);
34
+ })) {
35
+ return false;
36
+ }
37
+ }
38
+ return true;
39
+ })
30
40
  .filter(folder => {
31
41
  const p = node_path_1.default.join(sourceBase, dirname, folder);
32
42
  if (type === "file" && currentLevel === 1) {
@@ -6,18 +6,8 @@ function toBeTypeOf(received, expected) {
6
6
  const objType = typeof received;
7
7
  const pass = objType === expected;
8
8
  const message = pass
9
- ? () => this.utils.matcherHint(".not.toBeTypeOf") +
10
- "\n\n" +
11
- "Expected value to not be (using typeof):\n" +
12
- ` ${this.utils.printExpected(expected)}\n` +
13
- "Received:\n" +
14
- ` ${this.utils.printReceived(objType)}`
15
- : () => this.utils.matcherHint(".toBeTypeOf") +
16
- "\n\n" +
17
- "Expected value to be (using typeof):\n" +
18
- ` ${this.utils.printExpected(expected)}\n` +
19
- "Received:\n" +
20
- ` ${this.utils.printReceived(objType)}`;
9
+ ? () => `${this.utils.matcherHint(".not.toBeTypeOf")}\n\nExpected value to not be (using typeof):\n ${this.utils.printExpected(expected)}\nReceived:\n ${this.utils.printReceived(objType)}`
10
+ : () => `${this.utils.matcherHint(".toBeTypeOf")}\n\nExpected value to be (using typeof):\n ${this.utils.printExpected(expected)}\nReceived:\n ${this.utils.printReceived(objType)}`;
21
11
  return { message, pass };
22
12
  }
23
13
  exports.toBeTypeOf = toBeTypeOf;
@@ -5,18 +5,8 @@ exports.toEndWith = void 0;
5
5
  function toEndWith(received, expected) {
6
6
  const pass = typeof received === "string" && received.endsWith(expected);
7
7
  const message = pass
8
- ? () => this.utils.matcherHint(".not.toEndWith") +
9
- "\n\n" +
10
- "Expected value to not end with:\n" +
11
- ` ${this.utils.printExpected(expected)}\n` +
12
- "Received:\n" +
13
- ` ${this.utils.printReceived(received)}`
14
- : () => this.utils.matcherHint(".toEndWith") +
15
- "\n\n" +
16
- "Expected value to end with:\n" +
17
- ` ${this.utils.printExpected(expected)}\n` +
18
- "Received:\n" +
19
- ` ${this.utils.printReceived(received)}`;
8
+ ? () => `${this.utils.matcherHint(".not.toEndWith")}\n\nExpected value to not end with:\n ${this.utils.printExpected(expected)}\nReceived:\n ${this.utils.printReceived(received)}`
9
+ : () => `${this.utils.matcherHint(".toEndWith")}\n\nExpected value to end with:\n ${this.utils.printExpected(expected)}\nReceived:\n ${this.utils.printReceived(received)}`;
20
10
  return { message, pass };
21
11
  }
22
12
  exports.toEndWith = toEndWith;
@@ -50,9 +50,7 @@ function toMatchFileSnapshot(content, filepath, options = {}) {
50
50
  snapshotState.unmatched++;
51
51
  return {
52
52
  pass: isNot,
53
- message: () => `New output file ${chalk_1.default.blue(node_path_1.default.basename(filename))} was ${chalk_1.default.bold.red("not written")}.\n\n` +
54
- "The update flag must be explicitly passed to write a new snapshot.\n\n" +
55
- `This is likely because this test is run in a ${chalk_1.default.blue("continuous integration (CI) environment")} in which snapshots are not written by default.\n\n`
53
+ message: () => `New output file ${chalk_1.default.blue(node_path_1.default.basename(filename))} was ${chalk_1.default.bold.red("not written")}.\n\nThe update flag must be explicitly passed to write a new snapshot.\n\nThis is likely because this test is run in a ${chalk_1.default.blue("continuous integration (CI) environment")} in which snapshots are not written by default.\n\n`
56
54
  };
57
55
  }
58
56
  if (node_fs_1.default.existsSync(filename)) {
@@ -43,6 +43,8 @@ export class FakeSheet {
43
43
  constructor(element: any, basePath: any);
44
44
  _element: any;
45
45
  _basePath: any;
46
+ cachedCss: string | undefined;
47
+ cachedCssRules: any[] | undefined;
46
48
  get css(): string;
47
49
  get cssRules(): any[];
48
50
  }
@@ -135,19 +135,21 @@ class FakeElement {
135
135
  if (/^\//.test(value)) {
136
136
  return `https://test.cases${value}`;
137
137
  }
138
- if (/^\.\.\//.test(value)) {
138
+ else if (/^\.\.\//.test(value)) {
139
139
  return `https://test.cases${value.slice(2)}`;
140
140
  }
141
- if (/^\.\//.test(value)) {
141
+ else if (/^\.\//.test(value)) {
142
142
  return `https://test.cases/path${value.slice(1)}`;
143
143
  }
144
- if (/^\w+:\/\//.test(value)) {
144
+ else if (/^\w+:\/\//.test(value)) {
145
145
  return value;
146
146
  }
147
- if (/^\/\//.test(value)) {
147
+ else if (/^\/\//.test(value)) {
148
148
  return `https:${value}`;
149
149
  }
150
- return `https://test.cases/path/${value}`;
150
+ else {
151
+ return `https://test.cases/path/${value}`;
152
+ }
151
153
  }
152
154
  set src(value) {
153
155
  if (this._type === "script") {
@@ -174,8 +176,13 @@ class FakeSheet {
174
176
  constructor(element, basePath) {
175
177
  this._element = element;
176
178
  this._basePath = basePath;
179
+ this.cachedCss = undefined;
180
+ this.cachedCssRules = undefined;
177
181
  }
178
182
  get css() {
183
+ if (this.cachedCss) {
184
+ return this.cachedCss;
185
+ }
179
186
  let css = fs.readFileSync(path.resolve(this._basePath, this._element.href
180
187
  .replace(/^https:\/\/test\.cases\/path\//, "")
181
188
  .replace(/^https:\/\/example\.com\//, "")), "utf-8");
@@ -188,10 +195,14 @@ class FakeSheet {
188
195
  }
189
196
  return fs.readFileSync(path.resolve(this._basePath, url.replace(/^https:\/\/test\.cases\/path\//, "")), "utf-8");
190
197
  });
198
+ this.cachedCss = css;
191
199
  return css;
192
200
  }
193
201
  get cssRules() {
194
- const walkCssTokens = require("../../lib/css/walkCssTokens");
202
+ if (this.cachedCssRules) {
203
+ return this.cachedCssRules;
204
+ }
205
+ const walkCssTokens = require("./walkCssTokens");
195
206
  const rules = [];
196
207
  let currentRule = { getPropertyValue };
197
208
  let selector = undefined;
@@ -204,9 +215,13 @@ class FakeSheet {
204
215
  currentRule[property] = value;
205
216
  }
206
217
  };
207
- let css = fs.readFileSync(path.resolve(this._basePath, this._element.href
208
- .replace(/^https:\/\/test\.cases\/path\//, "")
209
- .replace(/^https:\/\/example\.com\//, "")), "utf-8");
218
+ const filepath = /file:\/\//.test(this._element.href)
219
+ ? new URL(this._element.href)
220
+ : path.resolve(this._basePath, this._element.href
221
+ .replace(/^https:\/\/test\.cases\/path\//, "")
222
+ .replace(/^https:\/\/example\.com\/public\/path\//, "")
223
+ .replace(/^https:\/\/example\.com\//, ""));
224
+ let css = fs.readFileSync(filepath.replace(/\?hmr=\d*/, ""), "utf-8");
210
225
  css = css.replace(/@import url\("([^"]+)"\);/g, (match, url) => {
211
226
  if (!/^https:\/\/test\.cases\/path\//.test(url)) {
212
227
  return url;
@@ -215,7 +230,9 @@ class FakeSheet {
215
230
  return url;
216
231
  }
217
232
  return fs.readFileSync(path.resolve(this._basePath, url.replace(/^https:\/\/test\.cases\/path\//, "")), "utf-8");
218
- });
233
+ })
234
+ .replace(/\/\*[\s\S]*?\*\//g, '')
235
+ .replace("//", "");
219
236
  walkCssTokens(css, {
220
237
  isSelector() {
221
238
  return selector === undefined;
@@ -229,8 +246,13 @@ class FakeSheet {
229
246
  },
230
247
  rightCurlyBracket(source, start, end) {
231
248
  processDeclaration(source.slice(last, start));
249
+ rules.push({
250
+ selectorText: selector,
251
+ style: currentRule,
252
+ // hack cssText, css hmr needs this fields to detect changes
253
+ cssText: css
254
+ });
232
255
  last = end;
233
- rules.push({ selectorText: selector, style: currentRule });
234
256
  selector = undefined;
235
257
  currentRule = { getPropertyValue };
236
258
  return end;
@@ -241,6 +263,7 @@ class FakeSheet {
241
263
  return end;
242
264
  }
243
265
  });
266
+ this.cachedCssRules = rules;
244
267
  return rules;
245
268
  }
246
269
  }
@@ -81,31 +81,32 @@ module.exports = function checkArrayExpectation(testDirectory, object, kind, fil
81
81
  const expected = require(expectedFilename);
82
82
  const diff = diffItems(array, expected, kind);
83
83
  if (expected.length < array.length) {
84
- return (done(new Error(`More ${kind}s (${array.length} instead of ${expected.length}) while compiling than expected:\n\n${diff}\n\nCheck expected ${kind}s: ${expectedFilename}`)),
85
- true);
84
+ done(new Error(`More ${kind}s (${array.length} instead of ${expected.length}) while compiling than expected:\n\n${diff}\n\nCheck expected ${kind}s: ${expectedFilename}`));
85
+ return true;
86
86
  }
87
87
  if (expected.length > array.length) {
88
- return (done(new Error(`Less ${kind}s (${array.length} instead of ${expected.length}) while compiling than expected:\n\n${diff}\n\nCheck expected ${kind}s: ${expectedFilename}`)),
89
- true);
88
+ done(new Error(`Less ${kind}s (${array.length} instead of ${expected.length}) while compiling than expected:\n\n${diff}\n\nCheck expected ${kind}s: ${expectedFilename}`));
89
+ return true;
90
90
  }
91
91
  for (let i = 0; i < array.length; i++) {
92
92
  if (Array.isArray(expected[i])) {
93
93
  for (let j = 0; j < expected[i].length; j++) {
94
94
  if (!check(expected[i][j], array[i])) {
95
- return (done(new Error(`${upperCaseKind} ${i}: ${explain(array[i])} doesn't match ${explain(expected[i][j])}`)),
96
- true);
95
+ done(new Error(`${upperCaseKind} ${i}: ${explain(array[i])} doesn't match ${explain(expected[i][j])}`));
96
+ return true;
97
97
  }
98
98
  }
99
99
  }
100
- else if (!check(expected[i], array[i]))
101
- return (done(new Error(`${upperCaseKind} ${i}: ${explain(array[i])} doesn't match ${explain(expected[i])}`)),
102
- true);
100
+ else if (!check(expected[i], array[i])) {
101
+ done(new Error(`${upperCaseKind} ${i}: ${explain(array[i])} doesn't match ${explain(expected[i])}`));
102
+ return true;
103
+ }
103
104
  }
104
105
  }
105
106
  else if (array.length > 0) {
106
- return (done(new Error(`${upperCaseKind}s while compiling:\n\n${array
107
+ done(new Error(`${upperCaseKind}s while compiling:\n\n${array
107
108
  .map(explain)
108
- .join("\n\n")}`)),
109
- true);
109
+ .join("\n\n")}`));
110
+ return true;
110
111
  }
111
112
  };
@@ -14,7 +14,7 @@ module.exports = function copyDiff(src, dest, initial) {
14
14
  copyDiff(srcFile, destFile, initial);
15
15
  }
16
16
  else {
17
- var content = fs.readFileSync(srcFile);
17
+ const content = fs.readFileSync(srcFile);
18
18
  if (/^DELETE\s*$/.test(content.toString("utf-8"))) {
19
19
  fs.unlinkSync(destFile);
20
20
  }
@@ -0,0 +1,38 @@
1
+ declare function _exports(input: string, callbacks: CssTokenCallbacks): void;
2
+ declare namespace _exports {
3
+ export { isIdentStartCodePoint };
4
+ export function eatComments(input: string, pos: number): number;
5
+ export function eatWhitespace(input: string, pos: number): number;
6
+ export function eatWhitespaceAndComments(input: string, pos: number): number;
7
+ export function eatWhiteLine(input: string, pos: number): number;
8
+ export { CssTokenCallbacks, CharHandler };
9
+ }
10
+ export = _exports;
11
+ export type CssTokenCallbacks = {
12
+ isSelector?: ((arg0: string, arg1: number) => boolean) | undefined;
13
+ url?: ((arg0: string, arg1: number, arg2: number, arg3: number, arg4: number) => number) | undefined;
14
+ string?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
15
+ leftParenthesis?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
16
+ rightParenthesis?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
17
+ pseudoFunction?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
18
+ function?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
19
+ pseudoClass?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
20
+ atKeyword?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
21
+ class?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
22
+ identifier?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
23
+ id?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
24
+ leftCurlyBracket?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
25
+ rightCurlyBracket?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
26
+ semicolon?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
27
+ comma?: ((arg0: string, arg1: number, arg2: number) => number) | undefined;
28
+ };
29
+ export type CharHandler = (arg0: string, arg1: number, arg2: CssTokenCallbacks) => number;
30
+ /**
31
+ * ident-start code point
32
+ *
33
+ * A letter, a non-ASCII code point, or U+005F LOW LINE (_).
34
+ *
35
+ * @param {number} cc char code
36
+ * @returns {boolean} true, if cc is a start code point of an identifier
37
+ */
38
+ declare function isIdentStartCodePoint(cc: number): boolean;