@rstest/core 0.5.0 → 0.5.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.
@@ -1,4 +1,5 @@
1
1
  import type { assert as assert_2 } from 'chai';
2
+ import { LoadConfigOptions } from '@rsbuild/core';
2
3
  import type { RsbuildConfig } from '@rsbuild/core';
3
4
  import type { RsbuildPlugin } from '@rsbuild/core';
4
5
 
@@ -304,6 +305,35 @@ declare interface Colors {
304
305
  };
305
306
  }
306
307
 
308
+ declare type CommonOptions = {
309
+ root?: string;
310
+ config?: string;
311
+ configLoader?: LoadConfigOptions['loader'];
312
+ globals?: boolean;
313
+ isolate?: boolean;
314
+ include?: string[];
315
+ exclude?: string[];
316
+ reporter?: string[];
317
+ project?: string[];
318
+ coverage?: boolean;
319
+ passWithNoTests?: boolean;
320
+ printConsoleTrace?: boolean;
321
+ disableConsoleIntercept?: boolean;
322
+ update?: boolean;
323
+ testNamePattern?: RegExp | string;
324
+ testTimeout?: number;
325
+ hookTimeout?: number;
326
+ testEnvironment?: string;
327
+ clearMocks?: boolean;
328
+ resetMocks?: boolean;
329
+ restoreMocks?: boolean;
330
+ unstubGlobals?: boolean;
331
+ unstubEnvs?: boolean;
332
+ retry?: number;
333
+ maxConcurrency?: number;
334
+ slowTestThreshold?: number;
335
+ };
336
+
307
337
  declare type CompareKeys = ((a: string, b: string) => number) | null | undefined;
308
338
 
309
339
  declare interface Config {
@@ -415,6 +445,11 @@ export declare type CoverageOptions = {
415
445
  * @default undefined
416
446
  */
417
447
  thresholds?: CoverageThresholds;
448
+ /**
449
+ * Whether to report coverage when tests fail.
450
+ * @default false
451
+ */
452
+ reportOnFailure?: boolean;
418
453
  };
419
454
 
420
455
  export declare class CoverageProvider {
@@ -489,6 +524,12 @@ declare type CoverageThresholds = CoverageThreshold | (CoverageThreshold & {
489
524
  };
490
525
  });
491
526
 
527
+ export declare function createRstest({ config, projects, configFilePath, }: {
528
+ config: RstestConfig;
529
+ configFilePath?: string;
530
+ projects: Project[];
531
+ }, command: RstestCommand, fileFilters: string[]): RstestInstance;
532
+
492
533
  declare interface CustomMatcher {
493
534
  /**
494
535
  * Checks that a value satisfies a custom matcher function.
@@ -844,7 +885,7 @@ declare function getMatcherUtils(): {
844
885
  printWithType: typeof printWithType
845
886
  };
846
887
 
847
- declare type GetSourcemap = (sourcePath: string) => SourceMapInput | null;
888
+ declare type GetSourcemap = (sourcePath: string) => Promise<SourceMapInput | null>;
848
889
 
849
890
  declare class GithubActionsReporter {
850
891
  private onWritePath;
@@ -879,6 +920,12 @@ declare interface HtmlSpaOptions extends HtmlOptions {
879
920
 
880
921
  declare type Indent = (arg0: string) => string;
881
922
 
923
+ export declare function initCli(options: CommonOptions): Promise<{
924
+ config: RstestConfig;
925
+ configFilePath?: string;
926
+ projects: Project[];
927
+ }>;
928
+
882
929
  declare interface InlineSnapshotMatcher<T> {
883
930
  <U extends {
884
931
  [P in keyof T]: any;
@@ -1313,6 +1360,21 @@ declare interface LinkMapper {
1313
1360
  assetPath(node: Node_2, name: string): string;
1314
1361
  }
1315
1362
 
1363
+ declare type ListCommandOptions = {
1364
+ filesOnly?: boolean;
1365
+ json?: boolean | string;
1366
+ };
1367
+
1368
+ export declare function loadConfig({ cwd, path, envMode, configLoader, }: {
1369
+ cwd?: string;
1370
+ path?: string;
1371
+ envMode?: string;
1372
+ configLoader?: LoadConfigOptions['loader'];
1373
+ }): Promise<{
1374
+ content: RstestConfig;
1375
+ filePath: string | null;
1376
+ }>;
1377
+
1316
1378
  declare interface Location_2 {
1317
1379
  line: number;
1318
1380
  column: number;
@@ -1928,6 +1990,11 @@ declare type Procedure = (...args: any[]) => any;
1928
1990
 
1929
1991
  declare type Procedure_2 = (...args: any[]) => any;
1930
1992
 
1993
+ declare type Project = {
1994
+ config: RstestConfig;
1995
+ configFilePath?: string;
1996
+ };
1997
+
1931
1998
  export declare type ProjectConfig = Omit<RstestConfig, 'projects' | 'reporters' | 'pool' | 'isolate' | 'coverage'>;
1932
1999
 
1933
2000
  declare type ProjectContext = {
@@ -2212,6 +2279,12 @@ export declare interface RstestConfig {
2212
2279
  * Custom handler for console log in tests
2213
2280
  */
2214
2281
  onConsoleLog?: (content: string) => boolean | void;
2282
+ /** Format snapshot output */
2283
+ snapshotFormat?: SnapshotFormat;
2284
+ /**
2285
+ * Custom environment variables available on `process.env` during tests.
2286
+ */
2287
+ env?: Partial<NodeJS.ProcessEnv>;
2215
2288
  /**
2216
2289
  * Coverage options
2217
2290
  */
@@ -2262,6 +2335,12 @@ declare type RstestContext = {
2262
2335
 
2263
2336
  declare type RstestExpect = ExpectStatic;
2264
2337
 
2338
+ declare type RstestInstance = {
2339
+ context: RstestContext;
2340
+ runTests: () => Promise<void>;
2341
+ listTests: (options: ListCommandOptions) => Promise<void>;
2342
+ };
2343
+
2265
2344
  declare type RstestPoolOptions = {
2266
2345
  /** Pool used to run tests in. */
2267
2346
  type?: RstestPoolType;
@@ -2413,7 +2492,7 @@ declare type RunnerAPI = {
2413
2492
  onTestFailed: (fn: OnTestFailedHandler, timeout?: number) => void;
2414
2493
  };
2415
2494
 
2416
- declare type RuntimeConfig = Pick<RstestContext['normalizedConfig'], 'testTimeout' | 'testNamePattern' | 'globals' | 'passWithNoTests' | 'retry' | 'clearMocks' | 'resetMocks' | 'restoreMocks' | 'unstubEnvs' | 'unstubGlobals' | 'maxConcurrency' | 'printConsoleTrace' | 'disableConsoleIntercept' | 'testEnvironment' | 'isolate' | 'hookTimeout' | 'coverage'>;
2495
+ declare type RuntimeConfig = Pick<RstestContext['normalizedConfig'], 'testTimeout' | 'testNamePattern' | 'globals' | 'passWithNoTests' | 'retry' | 'clearMocks' | 'resetMocks' | 'restoreMocks' | 'unstubEnvs' | 'unstubGlobals' | 'maxConcurrency' | 'printConsoleTrace' | 'disableConsoleIntercept' | 'testEnvironment' | 'isolate' | 'hookTimeout' | 'coverage' | 'snapshotFormat' | 'env'>;
2417
2496
 
2418
2497
  declare type RuntimeOptions = Partial<Pick<RuntimeConfig, 'testTimeout' | 'hookTimeout' | 'clearMocks' | 'resetMocks' | 'restoreMocks' | 'maxConcurrency' | 'retry'>>;
2419
2498
 
@@ -2433,6 +2512,8 @@ declare interface SnapshotEnvironment {
2433
2512
  processStackTrace?: (stack: ParsedStack) => ParsedStack;
2434
2513
  }
2435
2514
 
2515
+ declare type SnapshotFormat = Omit<NonNullable<SnapshotStateOptions['snapshotFormat']>, 'plugins' | 'compareKeys'>;
2516
+
2436
2517
  declare class SnapshotManager {
2437
2518
  options: Omit<SnapshotStateOptions, "snapshotEnvironment">;
2438
2519
  summary: SnapshotSummary;
@@ -391,6 +391,11 @@ declare type CoverageOptions = {
391
391
  * @default undefined
392
392
  */
393
393
  thresholds?: CoverageThresholds;
394
+ /**
395
+ * Whether to report coverage when tests fail.
396
+ * @default false
397
+ */
398
+ reportOnFailure?: boolean;
394
399
  };
395
400
 
396
401
  declare class CoverageSummary {
@@ -725,7 +730,7 @@ declare function getMatcherUtils(): {
725
730
  printWithType: typeof printWithType
726
731
  };
727
732
 
728
- declare type GetSourcemap = (sourcePath: string) => SourceMapInput | null;
733
+ declare type GetSourcemap = (sourcePath: string) => Promise<SourceMapInput | null>;
729
734
 
730
735
  declare class GithubActionsReporter {
731
736
  private onWritePath;
@@ -1930,6 +1935,12 @@ declare interface RstestConfig {
1930
1935
  * Custom handler for console log in tests
1931
1936
  */
1932
1937
  onConsoleLog?: (content: string) => boolean | void;
1938
+ /** Format snapshot output */
1939
+ snapshotFormat?: SnapshotFormat;
1940
+ /**
1941
+ * Custom environment variables available on `process.env` during tests.
1942
+ */
1943
+ env?: Partial<NodeJS.ProcessEnv>;
1933
1944
  /**
1934
1945
  * Coverage options
1935
1946
  */
@@ -2005,12 +2016,15 @@ declare type RunnerAPI = {
2005
2016
  onTestFailed: (fn: OnTestFailedHandler, timeout?: number) => void;
2006
2017
  };
2007
2018
 
2008
- declare type RuntimeConfig = Pick<RstestContext['normalizedConfig'], 'testTimeout' | 'testNamePattern' | 'globals' | 'passWithNoTests' | 'retry' | 'clearMocks' | 'resetMocks' | 'restoreMocks' | 'unstubEnvs' | 'unstubGlobals' | 'maxConcurrency' | 'printConsoleTrace' | 'disableConsoleIntercept' | 'testEnvironment' | 'isolate' | 'hookTimeout' | 'coverage'>;
2019
+ declare type RuntimeConfig = Pick<RstestContext['normalizedConfig'], 'testTimeout' | 'testNamePattern' | 'globals' | 'passWithNoTests' | 'retry' | 'clearMocks' | 'resetMocks' | 'restoreMocks' | 'unstubEnvs' | 'unstubGlobals' | 'maxConcurrency' | 'printConsoleTrace' | 'disableConsoleIntercept' | 'testEnvironment' | 'isolate' | 'hookTimeout' | 'coverage' | 'snapshotFormat' | 'env'>;
2009
2020
 
2010
2021
  /** Runtime to Server */
2011
2022
  declare type RuntimeRPC = {
2012
2023
  onTestFileStart: (test: TestFileInfo) => Promise<void>;
2013
- onTestFileResult: (test: TestFileResult) => Promise<void>;
2024
+ getAssetsByEntry: () => Promise<{
2025
+ assetFiles: Record<string, string>;
2026
+ sourceMaps: Record<string, string>;
2027
+ }>;
2014
2028
  onTestCaseResult: (result: TestResult) => Promise<void>;
2015
2029
  onConsoleLog: (log: UserConsoleLog) => void;
2016
2030
  };
@@ -2019,11 +2033,14 @@ declare type RunWorkerOptions = {
2019
2033
  options: {
2020
2034
  entryInfo: EntryInfo;
2021
2035
  setupEntries: EntryInfo[];
2022
- assetFiles: Record<string, string>;
2023
- sourceMaps: Record<string, SourceMapInput>;
2024
2036
  context: WorkerContext;
2025
2037
  updateSnapshot: SnapshotUpdateState;
2026
2038
  type: 'run' | 'collect';
2039
+ /** assets is only defined when memory is sufficient, otherwise we should get them via rpc getAssetsByEntry method */
2040
+ assets?: {
2041
+ assetFiles: Record<string, string>;
2042
+ sourceMaps: Record<string, string>;
2043
+ };
2027
2044
  };
2028
2045
  rpcMethods: RuntimeRPC;
2029
2046
  };
@@ -2044,6 +2061,8 @@ declare interface SnapshotEnvironment {
2044
2061
  processStackTrace?: (stack: ParsedStack) => ParsedStack;
2045
2062
  }
2046
2063
 
2064
+ declare type SnapshotFormat = Omit<NonNullable<SnapshotStateOptions['snapshotFormat']>, 'plugins' | 'compareKeys'>;
2065
+
2047
2066
  declare class SnapshotManager {
2048
2067
  options: Omit<SnapshotStateOptions, "snapshotEnvironment">;
2049
2068
  summary: SnapshotSummary;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rstest/core",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "description": "The Rsbuild-based test tool.",
5
5
  "bugs": {
6
6
  "url": "https://github.com/web-infra-dev/rstest/issues"
@@ -28,6 +28,9 @@
28
28
  "types": "./dist-types/index.d.ts",
29
29
  "default": "./dist/index.js"
30
30
  },
31
+ "./package.json": {
32
+ "default": "./package.json"
33
+ },
31
34
  "./globals": {
32
35
  "types": "./globals.d.ts"
33
36
  },
@@ -68,10 +71,10 @@
68
71
  "@types/picomatch": "^4.0.2",
69
72
  "cac": "^6.7.14",
70
73
  "chokidar": "^4.0.3",
71
- "happy-dom": "^18.0.1",
74
+ "happy-dom": "^20.0.0",
72
75
  "jest-diff": "^30.1.2",
73
76
  "jsdom": "^26.1.0",
74
- "license-webpack-plugin": "^4.0.2",
77
+ "webpack-license-plugin": "^4.5.1",
75
78
  "picocolors": "^1.1.1",
76
79
  "rslog": "^1.2.11",
77
80
  "source-map-support": "^0.5.21",
package/dist/0~223.js DELETED
@@ -1,158 +0,0 @@
1
- import 'module';
2
- /*#__PURE__*/ import.meta.url;
3
- export const __webpack_id__ = "223";
4
- export const __webpack_ids__ = [
5
- "223"
6
- ];
7
- export const __webpack_modules__ = {
8
- "./src/cli/init.ts": function(__unused_webpack_module, __webpack_exports__, __webpack_require__) {
9
- __webpack_require__.d(__webpack_exports__, {
10
- initCli: ()=>initCli
11
- });
12
- var node_fs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("node:fs");
13
- var pathe__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__("../../node_modules/.pnpm/pathe@2.0.3/node_modules/pathe/dist/shared/pathe.M-eThtNZ.mjs");
14
- var tinyglobby__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__("../../node_modules/.pnpm/tinyglobby@0.2.15/node_modules/tinyglobby/dist/index.mjs");
15
- var _config__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./src/config.ts");
16
- var _utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__("./src/utils/index.ts");
17
- async function resolveConfig(options) {
18
- const { content: config, filePath: configFilePath } = await (0, _config__WEBPACK_IMPORTED_MODULE_1__.Z9)({
19
- cwd: options.root,
20
- path: options.config,
21
- configLoader: options.configLoader
22
- });
23
- const keys = [
24
- 'root',
25
- 'globals',
26
- 'isolate',
27
- 'passWithNoTests',
28
- 'update',
29
- 'testNamePattern',
30
- 'testTimeout',
31
- 'hookTimeout',
32
- 'clearMocks',
33
- 'resetMocks',
34
- 'restoreMocks',
35
- 'unstubEnvs',
36
- 'unstubGlobals',
37
- 'retry',
38
- 'slowTestThreshold',
39
- 'maxConcurrency',
40
- 'printConsoleTrace',
41
- 'disableConsoleIntercept',
42
- 'testEnvironment'
43
- ];
44
- for (const key of keys)if (void 0 !== options[key]) config[key] = options[key];
45
- if (options.reporter) config.reporters = (0, _utils__WEBPACK_IMPORTED_MODULE_2__.bg)(options.reporter);
46
- if (void 0 !== options.coverage) {
47
- config.coverage ??= {};
48
- config.coverage.enabled = options.coverage;
49
- }
50
- if (options.exclude) config.exclude = (0, _utils__WEBPACK_IMPORTED_MODULE_2__.bg)(options.exclude);
51
- if (options.include) config.include = (0, _utils__WEBPACK_IMPORTED_MODULE_2__.bg)(options.include);
52
- return {
53
- config,
54
- configFilePath: configFilePath ?? void 0
55
- };
56
- }
57
- async function resolveProjects({ config, root, options }) {
58
- if (!config.projects) return [];
59
- const getDefaultProjectName = (dir)=>{
60
- const pkgJsonPath = (0, pathe__WEBPACK_IMPORTED_MODULE_3__.r)(dir, 'package.json');
61
- const name = (0, node_fs__WEBPACK_IMPORTED_MODULE_0__.existsSync)(pkgJsonPath) ? JSON.parse((0, node_fs__WEBPACK_IMPORTED_MODULE_0__.readFileSync)(pkgJsonPath, 'utf-8')).name : '';
62
- if ('string' != typeof name || !name) return (0, pathe__WEBPACK_IMPORTED_MODULE_3__.c)(dir);
63
- return name;
64
- };
65
- const globProjects = async (patterns)=>{
66
- const globOptions = {
67
- absolute: true,
68
- dot: true,
69
- onlyFiles: false,
70
- cwd: root,
71
- expandDirectories: false,
72
- ignore: [
73
- '**/node_modules/**',
74
- '**/.DS_Store'
75
- ]
76
- };
77
- return (0, tinyglobby__WEBPACK_IMPORTED_MODULE_4__.glob)(patterns, globOptions);
78
- };
79
- const { projectPaths, projectPatterns, projectConfigs } = (config.projects || []).reduce((total, p)=>{
80
- if ('object' == typeof p) {
81
- const projectRoot = p.root ? (0, _utils__WEBPACK_IMPORTED_MODULE_2__.tS)(p.root, root) : root;
82
- total.projectConfigs.push({
83
- config: {
84
- root: projectRoot,
85
- name: p.name ? p.name : getDefaultProjectName(projectRoot),
86
- ...p
87
- },
88
- configFilePath: void 0
89
- });
90
- return total;
91
- }
92
- const projectStr = (0, _utils__WEBPACK_IMPORTED_MODULE_2__.tS)(p, root);
93
- if ((0, tinyglobby__WEBPACK_IMPORTED_MODULE_4__.ey)(projectStr)) total.projectPatterns.push(projectStr);
94
- else {
95
- const absolutePath = (0, _utils__WEBPACK_IMPORTED_MODULE_2__.FI)(root, projectStr);
96
- if (!(0, node_fs__WEBPACK_IMPORTED_MODULE_0__.existsSync)(absolutePath)) throw `Can't resolve project "${p}", please make sure "${p}" is a existing file or a directory.`;
97
- total.projectPaths.push(absolutePath);
98
- }
99
- return total;
100
- }, {
101
- projectPaths: [],
102
- projectPatterns: [],
103
- projectConfigs: []
104
- });
105
- projectPaths.push(...await globProjects(projectPatterns));
106
- const projects = await Promise.all(projectPaths.map(async (project)=>{
107
- const isDirectory = (0, node_fs__WEBPACK_IMPORTED_MODULE_0__.statSync)(project).isDirectory();
108
- const { config, configFilePath } = await resolveConfig({
109
- ...options,
110
- config: isDirectory ? void 0 : project,
111
- root: isDirectory ? project : (0, pathe__WEBPACK_IMPORTED_MODULE_3__.d)(project)
112
- });
113
- config.name ??= getDefaultProjectName(project);
114
- if (config.projects?.length && config.root !== root) _utils__WEBPACK_IMPORTED_MODULE_2__.vF.warn(`Projects cannot have nested projects, the "projects" field in project "${config.name}" will be ignored.`);
115
- return {
116
- config,
117
- configFilePath
118
- };
119
- })).then((projects)=>(0, _utils__WEBPACK_IMPORTED_MODULE_2__.zz)(projects.concat(projectConfigs), options));
120
- if (!projects.length) {
121
- let errorMsg = `No projects found, please make sure you have at least one valid project.
122
- ${_utils__WEBPACK_IMPORTED_MODULE_2__.yW.gray('projects:')} ${JSON.stringify(config.projects, null, 2)}`;
123
- if (options.project) errorMsg += `\n${_utils__WEBPACK_IMPORTED_MODULE_2__.yW.gray('projectName filter:')} ${JSON.stringify(options.project, null, 2)}`;
124
- throw errorMsg;
125
- }
126
- const names = new Set();
127
- projects.forEach((project)=>{
128
- if (names.has(project.config.name)) {
129
- const conflictProjects = projects.filter((p)=>p.config.name === project.config.name);
130
- throw `Project name "${project.config.name}" is already used. Please ensure all projects have unique names.
131
- Conflicting projects:
132
- ${conflictProjects.map((p)=>`- ${p.configFilePath || p.config.root}`).join('\n')}
133
- `;
134
- }
135
- names.add(project.config.name);
136
- });
137
- return projects;
138
- }
139
- async function initCli(options) {
140
- const cwd = process.cwd();
141
- const root = options.root ? (0, _utils__WEBPACK_IMPORTED_MODULE_2__.FI)(cwd, options.root) : cwd;
142
- const { config, configFilePath } = await resolveConfig({
143
- ...options,
144
- root
145
- });
146
- const projects = await resolveProjects({
147
- config,
148
- root,
149
- options
150
- });
151
- return {
152
- config,
153
- configFilePath,
154
- projects
155
- };
156
- }
157
- }
158
- };