@rstest/core 0.7.3 → 0.7.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.
@@ -0,0 +1,9 @@
1
+ export declare const runGlobalTeardown: () => Promise<{
2
+ success: boolean;
3
+ error?: string;
4
+ }>;
5
+
6
+ declare function runInPool(options: any): Promise<any>;
7
+ export default runInPool;
8
+
9
+ export { }
@@ -0,0 +1,115 @@
1
+ import 'module';
2
+ /*#__PURE__*/ import.meta.url;
3
+ import { __webpack_require__ } from "./rslib-runtime.js";
4
+ import { formatTestError } from "./913.js";
5
+ import "./664.js";
6
+ const source_map_support = __webpack_require__("../../node_modules/.pnpm/source-map-support@0.5.21/node_modules/source-map-support/source-map-support.js");
7
+ const picocolors = __webpack_require__("../../node_modules/.pnpm/picocolors@1.1.1/node_modules/picocolors/picocolors.js");
8
+ var picocolors_default = /*#__PURE__*/ __webpack_require__.n(picocolors);
9
+ let teardownCallbacks = [];
10
+ let initialEnv = {};
11
+ let envChanges = {};
12
+ function trackEnvChanges() {
13
+ initialEnv = {
14
+ ...process.env
15
+ };
16
+ }
17
+ function captureEnvChanges() {
18
+ const changes = {};
19
+ for(const key in process.env)if (process.env[key] !== initialEnv[key]) changes[key] = process.env[key];
20
+ for(const key in initialEnv)if (!(key in process.env) && void 0 !== initialEnv[key]) changes[key] = void 0;
21
+ return changes;
22
+ }
23
+ const runGlobalSetup = async (data)=>{
24
+ try {
25
+ if (0 === data.entries.length) return {
26
+ success: true,
27
+ hasTeardown: false
28
+ };
29
+ (0, source_map_support.install)({
30
+ environment: 'node',
31
+ handleUncaughtExceptions: false,
32
+ retrieveSourceMap: (source)=>{
33
+ if (data.sourceMaps[source]) return {
34
+ url: source,
35
+ map: JSON.parse(data.sourceMaps[source])
36
+ };
37
+ return null;
38
+ }
39
+ });
40
+ trackEnvChanges();
41
+ for (const entry of data.entries){
42
+ const { distPath, testPath } = entry;
43
+ const setupCodeContent = data.assetFiles[distPath];
44
+ const { loadModule } = data.outputModule ? await import("./0~923.js").then((mod)=>({
45
+ EsmMode: mod.loadEsModule_EsmMode,
46
+ asModule: mod.asModule,
47
+ loadModule: mod.loadModule
48
+ })) : await import("./0~835.js").then((mod)=>({
49
+ cacheableLoadModule: mod.cacheableLoadModule,
50
+ loadModule: mod.loadModule
51
+ }));
52
+ const module = await loadModule({
53
+ codeContent: setupCodeContent,
54
+ distPath,
55
+ testPath,
56
+ rstestContext: {
57
+ global,
58
+ console: global.console,
59
+ Error
60
+ },
61
+ assetFiles: data.assetFiles,
62
+ interopDefault: data.interopDefault
63
+ });
64
+ let teardownCallback;
65
+ if (module && 'object' == typeof module) {
66
+ if (module.setup && 'function' == typeof module.setup) {
67
+ await module.setup();
68
+ if (module.teardown && 'function' == typeof module.teardown) teardownCallback = module.teardown;
69
+ } else if (module.default && 'function' == typeof module.default) teardownCallback = await module.default();
70
+ }
71
+ if (teardownCallback) teardownCallbacks.push(teardownCallback);
72
+ }
73
+ envChanges = captureEnvChanges();
74
+ return {
75
+ success: true,
76
+ hasTeardown: teardownCallbacks.length > 0,
77
+ teardownCount: teardownCallbacks.length,
78
+ envChanges: envChanges
79
+ };
80
+ } catch (error) {
81
+ return {
82
+ success: false,
83
+ hasTeardown: false,
84
+ errors: formatTestError(error)
85
+ };
86
+ }
87
+ };
88
+ async function runInPool(options) {
89
+ switch(options.type){
90
+ case 'setup':
91
+ return runGlobalSetup(options);
92
+ case 'teardown':
93
+ return runGlobalTeardown();
94
+ }
95
+ }
96
+ const runGlobalTeardown = async ()=>{
97
+ try {
98
+ const callbacks = [
99
+ ...teardownCallbacks
100
+ ];
101
+ teardownCallbacks = [];
102
+ for (const teardown of callbacks.reverse())await teardown();
103
+ return {
104
+ success: true
105
+ };
106
+ } catch (error) {
107
+ const message = error instanceof Error && error.stack ? error.stack : String(error);
108
+ console.error(picocolors_default().red(`Error during global teardown: ${message}`));
109
+ return {
110
+ success: false
111
+ };
112
+ }
113
+ };
114
+ export default runInPool;
115
+ export { runGlobalTeardown };
package/dist/index.d.ts CHANGED
@@ -20,11 +20,7 @@ declare type AfterAllListener = (ctx: SuiteContext) => MaybePromise<void>;
20
20
 
21
21
  export declare const afterEach: Rstest['afterEach'];
22
22
 
23
- declare type AfterEachListener = (params: {
24
- task: {
25
- result: Readonly<TestResult>;
26
- };
27
- }) => MaybePromise<void>;
23
+ declare type AfterEachListener = (ctx: TestContext) => MaybePromise<void>;
28
24
 
29
25
  export declare const assert: Rstest['assert'];
30
26
 
@@ -259,7 +255,7 @@ declare type BeforeAllListener = (ctx: SuiteContext) => MaybePromise<void | Afte
259
255
 
260
256
  export declare const beforeEach: Rstest['beforeEach'];
261
257
 
262
- declare type BeforeEachListener = () => MaybePromise<void | AfterEachListener>;
258
+ declare type BeforeEachListener = (ctx: TestContext) => MaybePromise<void | AfterEachListener>;
263
259
 
264
260
  declare interface BranchMapping {
265
261
  loc: Range_2;
@@ -364,6 +360,59 @@ declare interface Constructable {
364
360
  new (...args: any[]): any;
365
361
  }
366
362
 
363
+ /**
364
+ * Base class for writing content
365
+ */
366
+ declare class ContentWriter {
367
+ /**
368
+ * returns the colorized version of a string. Typically,
369
+ * content writers that write to files will return the
370
+ * same string and ones writing to a tty will wrap it in
371
+ * appropriate escape sequences.
372
+ */
373
+ colorize(str: string, clazz?: string): string;
374
+ /**
375
+ * writes a string appended with a newline to the destination
376
+ */
377
+ println(str: string): void;
378
+ /**
379
+ * closes this content writer. Should be called after all writes are complete.
380
+ */
381
+ close(): void;
382
+ }
383
+
384
+ declare interface Context {
385
+ data: any;
386
+ dir: string;
387
+ sourceFinder(filepath: string): string;
388
+ watermarks: Watermarks;
389
+ writer: FileWriter;
390
+ /**
391
+ * returns the coverage class given a coverage
392
+ * types and a percentage value.
393
+ */
394
+ classForPercent(type: keyof Watermarks, value: number): string;
395
+ /**
396
+ * returns the source code for the specified file path or throws if
397
+ * the source could not be found.
398
+ */
399
+ getSource(filepath: string): string;
400
+ getTree(summarizer?: Summarizers): Tree;
401
+ /**
402
+ * returns a full visitor given a partial one.
403
+ */
404
+ getVisitor<N extends Node_2 = Node_2>(visitor: Partial<Visitor<N>>): Visitor<N>;
405
+ /**
406
+ * returns a FileWriter implementation for reporting use. Also available
407
+ * as the `writer` property on the context.
408
+ */
409
+ getWriter(): FileWriter;
410
+ /**
411
+ * returns an XML writer for the supplied content writer
412
+ */
413
+ getXmlWriter(contentWriter: ContentWriter): XmlWriter;
414
+ }
415
+
367
416
  declare class CounterMap<K> extends DefaultMap<K, number> {
368
417
  constructor();
369
418
  // compat for jest-image-snapshot https://github.com/vitest-dev/vitest/issues/7322
@@ -435,7 +484,7 @@ export declare type CoverageOptions = {
435
484
  * The reporters to use for coverage collection.
436
485
  * @default ['text', 'html', 'clover', 'json']
437
486
  */
438
- reporters?: (keyof ReportOptions | ReportWithOptions)[];
487
+ reporters?: (keyof ReportOptions | ReportWithOptions | ReportBase)[];
439
488
  /**
440
489
  * The directory to store coverage reports.
441
490
  * @default './coverage'
@@ -591,12 +640,13 @@ declare class DefaultReporter implements Reporter {
591
640
  onTestCaseResult(): void;
592
641
  onUserConsoleLog(log: UserConsoleLog): void;
593
642
  onExit(): Promise<void>;
594
- onTestRunEnd({ results, testResults, duration, getSourcemap, snapshotSummary, filterRerunTestPaths, }: {
643
+ onTestRunEnd({ results, testResults, duration, getSourcemap, snapshotSummary, filterRerunTestPaths, unhandledErrors, }: {
595
644
  results: TestFileResult[];
596
645
  testResults: TestResult[];
597
646
  duration: Duration;
598
647
  snapshotSummary: SnapshotSummary;
599
648
  getSourcemap: GetSourcemap;
649
+ unhandledErrors?: Error[];
600
650
  filterRerunTestPaths?: string[];
601
651
  }): Promise<void>;
602
652
  }
@@ -848,6 +898,29 @@ declare interface FileOptions {
848
898
  file: string;
849
899
  }
850
900
 
901
+ /**
902
+ * utility for writing files under a specific directory
903
+ */
904
+ declare class FileWriter {
905
+ constructor(baseDir: string);
906
+ static startCapture(): void;
907
+ static stopCapture(): void;
908
+ static getOutput(): string;
909
+ static resetOutput(): void;
910
+ /**
911
+ * returns a FileWriter that is rooted at the supplied subdirectory
912
+ */
913
+ writeForDir(subdir: string): FileWriter;
914
+ /**
915
+ * copies a file from a source directory to a destination name
916
+ */
917
+ copyFile(source: string, dest: string, header?: string): void;
918
+ /**
919
+ * returns a content writer for writing content to the supplied file.
920
+ */
921
+ writeFile(file: string | null): ContentWriter;
922
+ }
923
+
851
924
  declare type Fixture<T, K extends keyof T, ExtraContext = object> = ((...args: any) => any) extends T[K] ? T[K] extends any ? FixtureFn<T, K, Omit<ExtraContext, Exclude<keyof T, K>>> : never : T[K] | (T[K] extends any ? FixtureFn<T, K, Omit<ExtraContext, Exclude<keyof T, K>>> : never);
852
925
 
853
926
  declare type FixtureFn<T, K extends keyof T, ExtraContext> = (context: Omit<T, K> & ExtraContext, use: Use<T[K]>) => Promise<void>;
@@ -1477,6 +1550,8 @@ declare interface MatcherState_2 extends MatcherState {
1477
1550
 
1478
1551
  declare type MaybePromise<T> = T | Promise<T>;
1479
1552
 
1553
+ export declare const mergeProjectConfig: (...configs: ProjectConfig[]) => ProjectConfig;
1554
+
1480
1555
  export declare const mergeRstestConfig: (...configs: RstestConfig[]) => RstestConfig;
1481
1556
 
1482
1557
  export declare interface Mock<T extends FunctionLike = FunctionLike> extends MockInstance_2<T> {
@@ -1939,10 +2014,11 @@ declare interface Node_2 {
1939
2014
  visit(visitor: Visitor, state: any): void;
1940
2015
  }
1941
2016
 
1942
- declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'exclude'>> & Partial<Pick<RstestConfig, OptionalKeys>> & {
2017
+ declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'globalSetup' | 'exclude'>> & Partial<Pick<RstestConfig, OptionalKeys>> & {
1943
2018
  pool: RstestPoolOptions;
1944
2019
  coverage: NormalizedCoverageOptions;
1945
2020
  setupFiles: string[];
2021
+ globalSetup: string[];
1946
2022
  exclude: {
1947
2023
  patterns: string[];
1948
2024
  override?: boolean;
@@ -1967,8 +2043,9 @@ declare type NormalizedProcedure<T extends Procedure> = (...args: Parameters<T>)
1967
2043
 
1968
2044
  declare type NormalizedProcedure_2<T extends Procedure_2> = (...args: Parameters<T>) => ReturnType<T>;
1969
2045
 
1970
- declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles'>> & Pick<NormalizedConfig, OptionalKeys> & {
2046
+ declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles' | 'globalSetup'>> & Pick<NormalizedConfig, OptionalKeys> & {
1971
2047
  setupFiles: string[];
2048
+ globalSetup: string[];
1972
2049
  };
1973
2050
 
1974
2051
  declare interface OldPlugin {
@@ -1978,19 +2055,11 @@ declare interface OldPlugin {
1978
2055
 
1979
2056
  export declare const onTestFailed: Rstest['onTestFailed'];
1980
2057
 
1981
- declare type OnTestFailedHandler = (params: {
1982
- task: {
1983
- result: Readonly<TestResult>;
1984
- };
1985
- }) => MaybePromise<void>;
2058
+ declare type OnTestFailedHandler = (ctx: TestContext) => MaybePromise<void>;
1986
2059
 
1987
2060
  export declare const onTestFinished: Rstest['onTestFinished'];
1988
2061
 
1989
- declare type OnTestFinishedHandler = (params: {
1990
- task: {
1991
- result: Readonly<TestResult>;
1992
- };
1993
- }) => MaybePromise<void>;
2062
+ declare type OnTestFinishedHandler = (ctx: TestContext) => MaybePromise<void>;
1994
2063
 
1995
2064
  declare type OptionalKeys = 'testNamePattern' | 'plugins' | 'source' | 'resolve' | 'output' | 'performance' | 'tools' | 'dev' | 'onConsoleLog' | 'chaiConfig' | 'resolveSnapshotPath';
1996
2065
 
@@ -2074,6 +2143,7 @@ declare type ProjectContext = {
2074
2143
  outputModule: boolean;
2075
2144
  configFilePath?: string;
2076
2145
  normalizedConfig: NormalizedProjectConfig;
2146
+ _globalSetups: boolean;
2077
2147
  };
2078
2148
 
2079
2149
  declare interface ProjectOptions {
@@ -2110,6 +2180,15 @@ declare interface RawSnapshotInfo {
2110
2180
 
2111
2181
  declare type Refs = Array<unknown>;
2112
2182
 
2183
+ declare class ReportBase {
2184
+ constructor(options?: Partial<ReportBaseOptions>);
2185
+ execute(context: Context): void;
2186
+ }
2187
+
2188
+ declare interface ReportBaseOptions {
2189
+ summarizer: Summarizers;
2190
+ }
2191
+
2113
2192
  export declare interface Reporter {
2114
2193
  /**
2115
2194
  * Called before test file run.
@@ -2144,11 +2223,12 @@ export declare interface Reporter {
2144
2223
  /**
2145
2224
  * Called after all tests have finished running.
2146
2225
  */
2147
- onTestRunEnd?: ({ results, testResults, duration, getSourcemap, snapshotSummary, }: {
2226
+ onTestRunEnd?: ({ results, testResults, duration, getSourcemap, snapshotSummary, unhandledErrors, }: {
2148
2227
  results: TestFileResult[];
2149
2228
  testResults: TestResult[];
2150
2229
  duration: Duration;
2151
2230
  getSourcemap: GetSourcemap;
2231
+ unhandledErrors?: Error[];
2152
2232
  snapshotSummary: SnapshotSummary;
2153
2233
  filterRerunTestPaths?: string[];
2154
2234
  }) => MaybePromise<void>;
@@ -2258,6 +2338,12 @@ export declare interface RstestConfig {
2258
2338
  * Path to setup files. They will be run before each test file.
2259
2339
  */
2260
2340
  setupFiles?: string[] | string;
2341
+ /**
2342
+ * Path to global setup files, relative to project root.
2343
+ * A global setup file can either export named functions `setup` and `teardown`
2344
+ * or a `default` function that returns a teardown function.
2345
+ */
2346
+ globalSetup?: string[] | string;
2261
2347
  /**
2262
2348
  * Retry the test specific number of times if it fails.
2263
2349
  * @default 0
@@ -2828,6 +2914,8 @@ declare type SuiteContext = {
2828
2914
  filepath: TestPath;
2829
2915
  };
2830
2916
 
2917
+ declare type Summarizers = "flat" | "nested" | "pkg" | "defaultSummarizer";
2918
+
2831
2919
  declare interface SyncExpectationResult {
2832
2920
  pass: boolean;
2833
2921
  message: () => string;
@@ -2925,6 +3013,15 @@ export declare type TestCaseInfo = {
2925
3013
  };
2926
3014
 
2927
3015
  declare type TestContext = {
3016
+ /**
3017
+ * Metadata of the current test
3018
+ */
3019
+ task: {
3020
+ /** Test name provided by user */
3021
+ name: string;
3022
+ /** Result of the current test, undefined if the test is not run yet */
3023
+ result?: TestResult;
3024
+ };
2928
3025
  expect: Expect;
2929
3026
  onTestFinished: RunnerAPI['onTestFinished'];
2930
3027
  onTestFailed: RunnerAPI['onTestFailed'];
@@ -3065,6 +3162,11 @@ declare class TraceMap implements SourceMap {
3065
3162
  constructor(map: Ro<SourceMapInput>, mapUrl?: string | null);
3066
3163
  }
3067
3164
 
3165
+ declare interface Tree<N extends Node_2 = Node_2> {
3166
+ getRoot(): N;
3167
+ visit(visitor: Partial<Visitor<N>>, state: any): void;
3168
+ }
3169
+
3068
3170
  declare interface UncheckedSnapshot {
3069
3171
  filePath: string;
3070
3172
  keys: Array<string>;
@@ -3097,10 +3199,39 @@ declare type VitestAssertion<
3097
3199
  T
3098
3200
  > = { [K in keyof A] : A[K] extends Chai.Assertion ? Assertion_2<T> : A[K] extends (...args: any[]) => any ? A[K] : VitestAssertion<A[K], T> } & ((type: string, message?: string) => Assertion_2);
3099
3201
 
3202
+ declare type Watermark = [number, number];
3203
+
3204
+ declare interface Watermarks {
3205
+ statements: Watermark;
3206
+ functions: Watermark;
3207
+ branches: Watermark;
3208
+ lines: Watermark;
3209
+ }
3210
+
3100
3211
  declare type WithAsymmetricMatcher<T> = T | AsymmetricMatcher<unknown>;
3101
3212
 
3102
3213
  declare type XInput = {
3103
3214
  x_google_ignoreList?: SourceMapV3['ignoreList'];
3104
3215
  };
3105
3216
 
3217
+ declare interface XmlWriter {
3218
+ indent(str: string): string;
3219
+ /**
3220
+ * writes the opening XML tag with the supplied attributes
3221
+ */
3222
+ openTag(name: string, attrs?: any): void;
3223
+ /**
3224
+ * closes an open XML tag.
3225
+ */
3226
+ closeTag(name: string): void;
3227
+ /**
3228
+ * writes a tag and its value opening and closing it at the same time
3229
+ */
3230
+ inlineTag(name: string, attrs?: any, content?: string): void;
3231
+ /**
3232
+ * closes all open tags and ends the document
3233
+ */
3234
+ closeAll(): void;
3235
+ }
3236
+
3106
3237
  export { }
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  import 'module';
2
2
  /*#__PURE__*/ import.meta.url;
3
- export { afterAll, afterEach, assert, beforeAll, beforeEach, createRstest, defineConfig, defineProject, describe, expect, initCli, it, loadConfig, mergeRstestConfig, onTestFailed, onTestFinished, rs, rstest, runCLI, test } from "./131.js";
3
+ export { afterAll, afterEach, assert, beforeAll, beforeEach, createRstest, defineConfig, defineProject, describe, expect, initCli, it, loadConfig, mergeProjectConfig, mergeRstestConfig, onTestFailed, onTestFinished, rs, rstest, runCLI, test } from "./131.js";