@rstest/core 0.7.3 → 0.7.5

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 "./693.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
@@ -433,9 +482,19 @@ export declare type CoverageOptions = {
433
482
  provider?: 'istanbul';
434
483
  /**
435
484
  * The reporters to use for coverage collection.
485
+ * Supports built-in istanbul reporters and custom reporters (e.g., '@canyonjs/report-html').
436
486
  * @default ['text', 'html', 'clover', 'json']
487
+ * @example
488
+ * // Built-in reporters
489
+ * reporters: ['text', 'html', ['json', { file: 'coverage.json' }]]
490
+ *
491
+ * // Custom reporters
492
+ * reporters: ['@canyonjs/report-html', ['custom-reporter', { outputDir: './reports' }]]
493
+ *
494
+ * // Mixed usage
495
+ * reporters: ['text', '@canyonjs/report-html', ['html', { subdir: 'html-report' }]]
437
496
  */
438
- reporters?: (keyof ReportOptions | ReportWithOptions)[];
497
+ reporters?: SupportedReporter[];
439
498
  /**
440
499
  * The directory to store coverage reports.
441
500
  * @default './coverage'
@@ -551,6 +610,9 @@ declare interface CustomMatcher {
551
610
  toBeOneOf: <T>(sample: Array<T>) => any;
552
611
  }
553
612
 
613
+ /** Custom reporter configuration for non-istanbul reporters */
614
+ declare type CustomReporter = string | [string, Record<string, unknown>];
615
+
554
616
  declare interface DecodedSourceMap extends SourceMapV3 {
555
617
  mappings: SourceMapSegment[][];
556
618
  }
@@ -591,12 +653,13 @@ declare class DefaultReporter implements Reporter {
591
653
  onTestCaseResult(): void;
592
654
  onUserConsoleLog(log: UserConsoleLog): void;
593
655
  onExit(): Promise<void>;
594
- onTestRunEnd({ results, testResults, duration, getSourcemap, snapshotSummary, filterRerunTestPaths, }: {
656
+ onTestRunEnd({ results, testResults, duration, getSourcemap, snapshotSummary, filterRerunTestPaths, unhandledErrors, }: {
595
657
  results: TestFileResult[];
596
658
  testResults: TestResult[];
597
659
  duration: Duration;
598
660
  snapshotSummary: SnapshotSummary;
599
661
  getSourcemap: GetSourcemap;
662
+ unhandledErrors?: Error[];
600
663
  filterRerunTestPaths?: string[];
601
664
  }): Promise<void>;
602
665
  }
@@ -848,6 +911,29 @@ declare interface FileOptions {
848
911
  file: string;
849
912
  }
850
913
 
914
+ /**
915
+ * utility for writing files under a specific directory
916
+ */
917
+ declare class FileWriter {
918
+ constructor(baseDir: string);
919
+ static startCapture(): void;
920
+ static stopCapture(): void;
921
+ static getOutput(): string;
922
+ static resetOutput(): void;
923
+ /**
924
+ * returns a FileWriter that is rooted at the supplied subdirectory
925
+ */
926
+ writeForDir(subdir: string): FileWriter;
927
+ /**
928
+ * copies a file from a source directory to a destination name
929
+ */
930
+ copyFile(source: string, dest: string, header?: string): void;
931
+ /**
932
+ * returns a content writer for writing content to the supplied file.
933
+ */
934
+ writeFile(file: string | null): ContentWriter;
935
+ }
936
+
851
937
  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
938
 
853
939
  declare type FixtureFn<T, K extends keyof T, ExtraContext> = (context: Omit<T, K> & ExtraContext, use: Use<T[K]>) => Promise<void>;
@@ -1477,6 +1563,8 @@ declare interface MatcherState_2 extends MatcherState {
1477
1563
 
1478
1564
  declare type MaybePromise<T> = T | Promise<T>;
1479
1565
 
1566
+ export declare const mergeProjectConfig: (...configs: ProjectConfig[]) => ProjectConfig;
1567
+
1480
1568
  export declare const mergeRstestConfig: (...configs: RstestConfig[]) => RstestConfig;
1481
1569
 
1482
1570
  export declare interface Mock<T extends FunctionLike = FunctionLike> extends MockInstance_2<T> {
@@ -1939,10 +2027,11 @@ declare interface Node_2 {
1939
2027
  visit(visitor: Visitor, state: any): void;
1940
2028
  }
1941
2029
 
1942
- declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'exclude'>> & Partial<Pick<RstestConfig, OptionalKeys>> & {
2030
+ declare type NormalizedConfig = Required<Omit<RstestConfig, OptionalKeys | 'pool' | 'projects' | 'coverage' | 'setupFiles' | 'globalSetup' | 'exclude'>> & Partial<Pick<RstestConfig, OptionalKeys>> & {
1943
2031
  pool: RstestPoolOptions;
1944
2032
  coverage: NormalizedCoverageOptions;
1945
2033
  setupFiles: string[];
2034
+ globalSetup: string[];
1946
2035
  exclude: {
1947
2036
  patterns: string[];
1948
2037
  override?: boolean;
@@ -1967,8 +2056,9 @@ declare type NormalizedProcedure<T extends Procedure> = (...args: Parameters<T>)
1967
2056
 
1968
2057
  declare type NormalizedProcedure_2<T extends Procedure_2> = (...args: Parameters<T>) => ReturnType<T>;
1969
2058
 
1970
- declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles'>> & Pick<NormalizedConfig, OptionalKeys> & {
2059
+ declare type NormalizedProjectConfig = Required<Omit<NormalizedConfig, OptionalKeys | 'projects' | 'reporters' | 'pool' | 'setupFiles' | 'globalSetup'>> & Pick<NormalizedConfig, OptionalKeys> & {
1971
2060
  setupFiles: string[];
2061
+ globalSetup: string[];
1972
2062
  };
1973
2063
 
1974
2064
  declare interface OldPlugin {
@@ -1978,19 +2068,11 @@ declare interface OldPlugin {
1978
2068
 
1979
2069
  export declare const onTestFailed: Rstest['onTestFailed'];
1980
2070
 
1981
- declare type OnTestFailedHandler = (params: {
1982
- task: {
1983
- result: Readonly<TestResult>;
1984
- };
1985
- }) => MaybePromise<void>;
2071
+ declare type OnTestFailedHandler = (ctx: TestContext) => MaybePromise<void>;
1986
2072
 
1987
2073
  export declare const onTestFinished: Rstest['onTestFinished'];
1988
2074
 
1989
- declare type OnTestFinishedHandler = (params: {
1990
- task: {
1991
- result: Readonly<TestResult>;
1992
- };
1993
- }) => MaybePromise<void>;
2075
+ declare type OnTestFinishedHandler = (ctx: TestContext) => MaybePromise<void>;
1994
2076
 
1995
2077
  declare type OptionalKeys = 'testNamePattern' | 'plugins' | 'source' | 'resolve' | 'output' | 'performance' | 'tools' | 'dev' | 'onConsoleLog' | 'chaiConfig' | 'resolveSnapshotPath';
1996
2078
 
@@ -2074,6 +2156,7 @@ declare type ProjectContext = {
2074
2156
  outputModule: boolean;
2075
2157
  configFilePath?: string;
2076
2158
  normalizedConfig: NormalizedProjectConfig;
2159
+ _globalSetups: boolean;
2077
2160
  };
2078
2161
 
2079
2162
  declare interface ProjectOptions {
@@ -2110,6 +2193,15 @@ declare interface RawSnapshotInfo {
2110
2193
 
2111
2194
  declare type Refs = Array<unknown>;
2112
2195
 
2196
+ declare class ReportBase {
2197
+ constructor(options?: Partial<ReportBaseOptions>);
2198
+ execute(context: Context): void;
2199
+ }
2200
+
2201
+ declare interface ReportBaseOptions {
2202
+ summarizer: Summarizers;
2203
+ }
2204
+
2113
2205
  export declare interface Reporter {
2114
2206
  /**
2115
2207
  * Called before test file run.
@@ -2144,11 +2236,12 @@ export declare interface Reporter {
2144
2236
  /**
2145
2237
  * Called after all tests have finished running.
2146
2238
  */
2147
- onTestRunEnd?: ({ results, testResults, duration, getSourcemap, snapshotSummary, }: {
2239
+ onTestRunEnd?: ({ results, testResults, duration, getSourcemap, snapshotSummary, unhandledErrors, }: {
2148
2240
  results: TestFileResult[];
2149
2241
  testResults: TestResult[];
2150
2242
  duration: Duration;
2151
2243
  getSourcemap: GetSourcemap;
2244
+ unhandledErrors?: Error[];
2152
2245
  snapshotSummary: SnapshotSummary;
2153
2246
  filterRerunTestPaths?: string[];
2154
2247
  }) => MaybePromise<void>;
@@ -2258,6 +2351,12 @@ export declare interface RstestConfig {
2258
2351
  * Path to setup files. They will be run before each test file.
2259
2352
  */
2260
2353
  setupFiles?: string[] | string;
2354
+ /**
2355
+ * Path to global setup files, relative to project root.
2356
+ * A global setup file can either export named functions `setup` and `teardown`
2357
+ * or a `default` function that returns a teardown function.
2358
+ */
2359
+ globalSetup?: string[] | string;
2261
2360
  /**
2262
2361
  * Retry the test specific number of times if it fails.
2263
2362
  * @default 0
@@ -2828,6 +2927,11 @@ declare type SuiteContext = {
2828
2927
  filepath: TestPath;
2829
2928
  };
2830
2929
 
2930
+ declare type Summarizers = "flat" | "nested" | "pkg" | "defaultSummarizer";
2931
+
2932
+ /** Union type for all supported reporter types */
2933
+ declare type SupportedReporter = keyof ReportOptions | ReportWithOptions | ReportBase | CustomReporter;
2934
+
2831
2935
  declare interface SyncExpectationResult {
2832
2936
  pass: boolean;
2833
2937
  message: () => string;
@@ -2925,6 +3029,15 @@ export declare type TestCaseInfo = {
2925
3029
  };
2926
3030
 
2927
3031
  declare type TestContext = {
3032
+ /**
3033
+ * Metadata of the current test
3034
+ */
3035
+ task: {
3036
+ /** Test name provided by user */
3037
+ name: string;
3038
+ /** Result of the current test, undefined if the test is not run yet */
3039
+ result?: TestResult;
3040
+ };
2928
3041
  expect: Expect;
2929
3042
  onTestFinished: RunnerAPI['onTestFinished'];
2930
3043
  onTestFailed: RunnerAPI['onTestFailed'];
@@ -3065,6 +3178,11 @@ declare class TraceMap implements SourceMap {
3065
3178
  constructor(map: Ro<SourceMapInput>, mapUrl?: string | null);
3066
3179
  }
3067
3180
 
3181
+ declare interface Tree<N extends Node_2 = Node_2> {
3182
+ getRoot(): N;
3183
+ visit(visitor: Partial<Visitor<N>>, state: any): void;
3184
+ }
3185
+
3068
3186
  declare interface UncheckedSnapshot {
3069
3187
  filePath: string;
3070
3188
  keys: Array<string>;
@@ -3097,10 +3215,39 @@ declare type VitestAssertion<
3097
3215
  T
3098
3216
  > = { [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
3217
 
3218
+ declare type Watermark = [number, number];
3219
+
3220
+ declare interface Watermarks {
3221
+ statements: Watermark;
3222
+ functions: Watermark;
3223
+ branches: Watermark;
3224
+ lines: Watermark;
3225
+ }
3226
+
3100
3227
  declare type WithAsymmetricMatcher<T> = T | AsymmetricMatcher<unknown>;
3101
3228
 
3102
3229
  declare type XInput = {
3103
3230
  x_google_ignoreList?: SourceMapV3['ignoreList'];
3104
3231
  };
3105
3232
 
3233
+ declare interface XmlWriter {
3234
+ indent(str: string): string;
3235
+ /**
3236
+ * writes the opening XML tag with the supplied attributes
3237
+ */
3238
+ openTag(name: string, attrs?: any): void;
3239
+ /**
3240
+ * closes an open XML tag.
3241
+ */
3242
+ closeTag(name: string): void;
3243
+ /**
3244
+ * writes a tag and its value opening and closing it at the same time
3245
+ */
3246
+ inlineTag(name: string, attrs?: any, content?: string): void;
3247
+ /**
3248
+ * closes all open tags and ends the document
3249
+ */
3250
+ closeAll(): void;
3251
+ }
3252
+
3106
3253
  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";