@ctrl/sentry-tests 0.0.0-stage → 0.1.1
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.
- package/LICENSE +21 -0
- package/NOTICE +338 -0
- package/README.md +271 -2
- package/dist/_chunks/channels-CLjUmyuI.js +6 -0
- package/dist/_chunks/collector-DiJaNK5H.js +271 -0
- package/dist/_chunks/delivery-DSYzskz8.js +61 -0
- package/dist/_chunks/jest-collector-B4DJnUVQ.js +135 -0
- package/dist/_chunks/report-CgKIcCfQ.js +545 -0
- package/dist/index.js +4 -0
- package/dist/jest-runner.js +27 -0
- package/dist/jest.js +104 -0
- package/dist/types/channels.d.ts +36 -0
- package/dist/types/collector.d.ts +80 -0
- package/dist/types/delivery.d.ts +10 -0
- package/dist/types/envelope.d.ts +68 -0
- package/dist/types/html.d.ts +2 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/jest-collector.d.ts +43 -0
- package/dist/types/jest-runner.d.ts +11 -0
- package/dist/types/jest.d.ts +42 -0
- package/dist/types/options.d.ts +46 -0
- package/dist/types/report.d.ts +60 -0
- package/dist/types/vitest-runner.d.ts +13 -0
- package/dist/types/vitest.d.ts +21 -0
- package/dist/vitest-runner.js +167 -0
- package/dist/vitest.js +119 -0
- package/package.json +77 -4
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** What a span's tracing channel events carry (the shape @ctrl/dom's trace.ts publishes). */
|
|
2
|
+
export interface SpanContext {
|
|
3
|
+
/** Sentry-style operation: "db.query", "testing-library.query", ... */
|
|
4
|
+
op: string;
|
|
5
|
+
/** What ran, as the test wrote it. */
|
|
6
|
+
description: string;
|
|
7
|
+
/** Filled in before the span ends; becomes the span's data. */
|
|
8
|
+
data: Record<string, string | number | boolean>;
|
|
9
|
+
/** True for promise-returning work: the span ends at asyncEnd rather than end. */
|
|
10
|
+
async: boolean;
|
|
11
|
+
/** Set by the tracing channel when the traced function throws or rejects. */
|
|
12
|
+
error?: unknown;
|
|
13
|
+
}
|
|
14
|
+
export interface Attachment {
|
|
15
|
+
filename: string;
|
|
16
|
+
contentType: string;
|
|
17
|
+
data: string;
|
|
18
|
+
}
|
|
19
|
+
/** Published on "sentry-tests:failure" when a test fails. */
|
|
20
|
+
export interface FailureMessage {
|
|
21
|
+
/** The test's full name: describe blocks and the test, joined with " > ". */
|
|
22
|
+
test: string;
|
|
23
|
+
/** What the test threw. */
|
|
24
|
+
error: unknown;
|
|
25
|
+
/** The environment's document, when there is one (jsdom, happy-dom, @ctrl/dom). */
|
|
26
|
+
document: unknown;
|
|
27
|
+
/** Files for the failure's event. The first one with a given filename wins. */
|
|
28
|
+
attachments: Attachment[];
|
|
29
|
+
/** Contexts for the failure's event: each shows as a section on the issue. */
|
|
30
|
+
contexts: Record<string, Record<string, unknown>>;
|
|
31
|
+
/** Hold the test (before its afterEach hooks) until `promise` settles, for at most 5 s in all. */
|
|
32
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
33
|
+
}
|
|
34
|
+
/** Tracing channels whose spans are recorded. */
|
|
35
|
+
export declare const SPAN_CHANNELS: readonly ["sentry-tests:span", "ctrl-dom:span"];
|
|
36
|
+
export declare const FAILURE_CHANNEL = "sentry-tests:failure";
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { type Attachment } from "./channels.js";
|
|
2
|
+
/** A finished span, in Sentry's span shape minus the trace id (report.ts adds it). */
|
|
3
|
+
export interface CollectedSpan {
|
|
4
|
+
span_id: string;
|
|
5
|
+
/** Undefined for a span directly under the file. */
|
|
6
|
+
parent_span_id: string | undefined;
|
|
7
|
+
op: string;
|
|
8
|
+
description: string;
|
|
9
|
+
start_timestamp: number;
|
|
10
|
+
timestamp: number;
|
|
11
|
+
status: "ok" | "internal_error";
|
|
12
|
+
data: Record<string, string | number | boolean>;
|
|
13
|
+
}
|
|
14
|
+
/** What a failed test (attempt) left behind, captured before its afterEach hooks ran. */
|
|
15
|
+
export interface FailureContext {
|
|
16
|
+
error?: {
|
|
17
|
+
name?: string;
|
|
18
|
+
message: string;
|
|
19
|
+
stack?: string;
|
|
20
|
+
};
|
|
21
|
+
attachments: Attachment[];
|
|
22
|
+
contexts: Record<string, Record<string, unknown>>;
|
|
23
|
+
}
|
|
24
|
+
export interface CollectedTest {
|
|
25
|
+
/** Its "test.case" spans, one per attempt under Jest (Vitest's one span holds every attempt). */
|
|
26
|
+
spanIds: string[];
|
|
27
|
+
/** Library spans past the per-test limit. */
|
|
28
|
+
droppedSpans: number;
|
|
29
|
+
/** One per failed attempt: a flaky test has failures and a pass. */
|
|
30
|
+
failures: FailureContext[];
|
|
31
|
+
}
|
|
32
|
+
/** One test file: what the worker hands the reporter. */
|
|
33
|
+
export interface CollectedFile {
|
|
34
|
+
/** Epoch seconds. */
|
|
35
|
+
startedAt: number;
|
|
36
|
+
endedAt: number;
|
|
37
|
+
/** In end order; report.ts sorts them. */
|
|
38
|
+
spans: CollectedSpan[];
|
|
39
|
+
/** By the runner's key for the test: Vitest's test id, Jest's full test name. */
|
|
40
|
+
tests: Record<string, CollectedTest>;
|
|
41
|
+
/** Library spans outside any test past the file's limit. */
|
|
42
|
+
droppedSpans: number;
|
|
43
|
+
}
|
|
44
|
+
/** A span that is still running. */
|
|
45
|
+
export interface OpenSpan {
|
|
46
|
+
readonly span: CollectedSpan;
|
|
47
|
+
/** The test it belongs to, or the file: what its library spans count against. */
|
|
48
|
+
readonly owner: Owner;
|
|
49
|
+
}
|
|
50
|
+
/** Where library spans count: a test (at most 300), or the file outside any test (at most 1000). */
|
|
51
|
+
interface Owner {
|
|
52
|
+
kept: number;
|
|
53
|
+
readonly limit: number;
|
|
54
|
+
readonly counts: {
|
|
55
|
+
droppedSpans: number;
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** Epoch seconds from the real clock. */
|
|
59
|
+
export declare const realNow: () => number;
|
|
60
|
+
export interface Collector {
|
|
61
|
+
/** Start a structure span under the current one; with `test`, a span of that test. */
|
|
62
|
+
open(op: string, description: string, data?: Record<string, string | number | boolean>, test?: CollectedTest): OpenSpan;
|
|
63
|
+
close(open: OpenSpan, failed?: boolean): void;
|
|
64
|
+
/** Run `fn` with `open` as the current span. */
|
|
65
|
+
run<T>(open: OpenSpan, fn: () => T): T;
|
|
66
|
+
/** The span later spans would nest under. */
|
|
67
|
+
current(): OpenSpan | null;
|
|
68
|
+
/** The parent for spans that no async context places (event-driven runners). */
|
|
69
|
+
enter(open: OpenSpan | null): void;
|
|
70
|
+
/** The test with this key, created on first use. */
|
|
71
|
+
test(key: string): CollectedTest;
|
|
72
|
+
/** Record a failed attempt: the error, the page and whatever "sentry-tests:failure" subscribers add. */
|
|
73
|
+
captureFailure(test: CollectedTest, name: string, error: unknown, document: unknown): Promise<void>;
|
|
74
|
+
/** Everything recorded since the last finish(), and start over. */
|
|
75
|
+
finish(): CollectedFile;
|
|
76
|
+
/** Unsubscribe from the tracing channels. */
|
|
77
|
+
stop(): void;
|
|
78
|
+
}
|
|
79
|
+
export declare function createCollector(): Collector;
|
|
80
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Envelope, Target } from "./envelope.js";
|
|
2
|
+
import type { ResolvedOptions } from "./options.js";
|
|
3
|
+
import type { BuiltReport } from "./report.js";
|
|
4
|
+
export declare class Delivery {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(options: ResolvedOptions, send: (envelope: Envelope, target: Target) => Promise<string | null>);
|
|
7
|
+
add(report: BuiltReport): void;
|
|
8
|
+
/** Wait for every envelope; a one-line summary of the run (null when nothing was reported). Starts a new run's totals. */
|
|
9
|
+
flush(): Promise<string | null>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export interface Dsn {
|
|
2
|
+
/** The DSN as given. */
|
|
3
|
+
dsn: string;
|
|
4
|
+
/** Where envelopes go, with the key in the query string. */
|
|
5
|
+
url: string;
|
|
6
|
+
host: string;
|
|
7
|
+
projectId: string;
|
|
8
|
+
}
|
|
9
|
+
/** https://develop.sentry.dev/sdk/overview/#parsing-the-dsn */
|
|
10
|
+
export declare function parseDsn(dsn: string): Dsn;
|
|
11
|
+
export declare const SDK: {
|
|
12
|
+
name: string;
|
|
13
|
+
version: string;
|
|
14
|
+
};
|
|
15
|
+
/** 32 hex characters (event and trace ids). */
|
|
16
|
+
export declare const uuid: () => string;
|
|
17
|
+
/** 16 hex characters. */
|
|
18
|
+
export declare const spanId: () => string;
|
|
19
|
+
export type EnvelopeItem = {
|
|
20
|
+
type: "event" | "transaction";
|
|
21
|
+
payload: Record<string, unknown>;
|
|
22
|
+
} | {
|
|
23
|
+
type: "attachment";
|
|
24
|
+
filename: string;
|
|
25
|
+
contentType: string;
|
|
26
|
+
payload: string;
|
|
27
|
+
};
|
|
28
|
+
export interface Envelope {
|
|
29
|
+
eventId: string;
|
|
30
|
+
items: EnvelopeItem[];
|
|
31
|
+
}
|
|
32
|
+
/** https://develop.sentry.dev/sdk/data-model/envelopes/#serialization-format */
|
|
33
|
+
export declare function serializeEnvelope(envelope: Envelope, dsn: Dsn | null): Uint8Array<ArrayBuffer>;
|
|
34
|
+
export interface Target {
|
|
35
|
+
/** "sentry.io" for a DSN's host, or "Spotlight". */
|
|
36
|
+
label: string;
|
|
37
|
+
url: string;
|
|
38
|
+
dsn: Dsn | null;
|
|
39
|
+
}
|
|
40
|
+
/** POST one envelope; resolves to an error message, or null when it was accepted. */
|
|
41
|
+
export declare function sendEnvelope(envelope: Envelope, target: Target): Promise<string | null>;
|
|
42
|
+
/** https://develop.sentry.dev/sdk/data-model/event-payloads/stacktrace/#frame-attributes */
|
|
43
|
+
export interface Frame {
|
|
44
|
+
filename: string;
|
|
45
|
+
abs_path: string;
|
|
46
|
+
function?: string;
|
|
47
|
+
lineno?: number;
|
|
48
|
+
colno?: number;
|
|
49
|
+
in_app: boolean;
|
|
50
|
+
pre_context?: string[];
|
|
51
|
+
context_line?: string;
|
|
52
|
+
post_context?: string[];
|
|
53
|
+
}
|
|
54
|
+
/** A frame as a test runner already parsed (and source-mapped) it: Vitest's `error.stacks`. */
|
|
55
|
+
export interface ParsedFrame {
|
|
56
|
+
method?: string;
|
|
57
|
+
file: string;
|
|
58
|
+
line?: number;
|
|
59
|
+
column?: number;
|
|
60
|
+
}
|
|
61
|
+
/** V8 stack lines as frames, oldest first (Sentry's order). */
|
|
62
|
+
export declare function parseStack(stack: string | undefined): ParsedFrame[];
|
|
63
|
+
/**
|
|
64
|
+
* Sentry frames from parsed ones, oldest first: paths relative to `root`, the test's own code in
|
|
65
|
+
* app (not node_modules, Node's internals or the packages next to this one), with source context
|
|
66
|
+
* read from disk (Sentry cannot fetch a local file).
|
|
67
|
+
*/
|
|
68
|
+
export declare function toSentryFrames(frames: readonly ParsedFrame[], root: string): Frame[];
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { resolveOptions, type SentryTestsOptions, type ResolvedOptions } from "./options.js";
|
|
2
|
+
export { buildReport, type BuiltReport, type ErrorInfo, type TestCaseReport, type TestFileReport } from "./report.js";
|
|
3
|
+
export type { CollectedFile, CollectedSpan, CollectedTest, FailureContext } from "./collector.js";
|
|
4
|
+
export { FAILURE_CHANNEL, SPAN_CHANNELS, type Attachment, type FailureMessage, type SpanContext } from "./channels.js";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { type CollectedFile } from "./collector.js";
|
|
2
|
+
/** The fields of jest-circus's events and its describe blocks, tests and hooks this reads. */
|
|
3
|
+
interface CircusBlock {
|
|
4
|
+
name: string;
|
|
5
|
+
parent?: CircusBlock;
|
|
6
|
+
}
|
|
7
|
+
interface CircusTest extends CircusBlock {
|
|
8
|
+
errors: unknown[];
|
|
9
|
+
}
|
|
10
|
+
interface CircusHook {
|
|
11
|
+
type: string;
|
|
12
|
+
/** Where the hook was registered. */
|
|
13
|
+
asyncError?: {
|
|
14
|
+
stack?: string;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
interface CircusEvent {
|
|
18
|
+
name: string;
|
|
19
|
+
describeBlock?: CircusBlock;
|
|
20
|
+
test?: CircusTest;
|
|
21
|
+
hook?: CircusHook;
|
|
22
|
+
error?: unknown;
|
|
23
|
+
}
|
|
24
|
+
export interface JestCollector {
|
|
25
|
+
handleTestEvent(event: CircusEvent): void | Promise<void>;
|
|
26
|
+
/** Stop collecting and write the file's data (the end of the test file). */
|
|
27
|
+
finish(): void;
|
|
28
|
+
}
|
|
29
|
+
/** What the runner writes per test file. */
|
|
30
|
+
export interface JestFileData {
|
|
31
|
+
testPath: string;
|
|
32
|
+
collected: CollectedFile;
|
|
33
|
+
}
|
|
34
|
+
/** Where a test file's data goes in the reporter's directory. */
|
|
35
|
+
export declare function jestDataFile(dir: string, testPath: string): string;
|
|
36
|
+
/** describe blocks and the test, joined with " > " (the root describe block has no name of its own). */
|
|
37
|
+
export declare function circusTestName(test: CircusBlock): string;
|
|
38
|
+
export declare function createJestCollector(options: {
|
|
39
|
+
dir: string;
|
|
40
|
+
testPath: string;
|
|
41
|
+
getDocument: () => unknown;
|
|
42
|
+
}): JestCollector;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type JestCollector } from "./jest-collector.js";
|
|
2
|
+
interface Environment {
|
|
3
|
+
global?: {
|
|
4
|
+
document?: unknown;
|
|
5
|
+
};
|
|
6
|
+
handleTestEvent?: (event: Parameters<JestCollector["handleTestEvent"]>[0], state: unknown) => void | Promise<void>;
|
|
7
|
+
}
|
|
8
|
+
/** jest-circus/runner's signature (Jest's TestFramework); the package ships no types for it. */
|
|
9
|
+
type TestFramework = (globalConfig: unknown, config: unknown, environment: Environment, runtime: unknown, testPath: string, sendMessageToJest?: unknown) => Promise<unknown>;
|
|
10
|
+
export default function sentryTestsRunner(...args: Parameters<TestFramework>): Promise<unknown>;
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type SentryTestsOptions, type ResolvedOptions } from "./options.js";
|
|
2
|
+
export type { SentryTestsOptions } from "./options.js";
|
|
3
|
+
/** The fields of Jest's reporter arguments this reads (@jest/test-result, @jest/reporters). */
|
|
4
|
+
interface JestAssertionResult {
|
|
5
|
+
ancestorTitles: string[];
|
|
6
|
+
title: string;
|
|
7
|
+
status: string;
|
|
8
|
+
duration?: number | null;
|
|
9
|
+
invocations?: number;
|
|
10
|
+
failureMessages: string[];
|
|
11
|
+
failureDetails: unknown[];
|
|
12
|
+
}
|
|
13
|
+
interface JestTestResult {
|
|
14
|
+
testFilePath: string;
|
|
15
|
+
testResults: JestAssertionResult[];
|
|
16
|
+
/** Set when the file could not run (it failed to load). */
|
|
17
|
+
testExecError?: {
|
|
18
|
+
message: string;
|
|
19
|
+
stack?: string | null;
|
|
20
|
+
} | null;
|
|
21
|
+
}
|
|
22
|
+
interface JestTest {
|
|
23
|
+
path: string;
|
|
24
|
+
context?: {
|
|
25
|
+
config?: {
|
|
26
|
+
rootDir?: string;
|
|
27
|
+
displayName?: {
|
|
28
|
+
name: string;
|
|
29
|
+
} | string;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export default class SentryTestsReporter {
|
|
34
|
+
#private;
|
|
35
|
+
readonly options: ResolvedOptions | null;
|
|
36
|
+
constructor(globalConfig?: {
|
|
37
|
+
rootDir?: string;
|
|
38
|
+
}, reporterOptions?: SentryTestsOptions);
|
|
39
|
+
onTestResult(test: JestTest, testResult: JestTestResult): void;
|
|
40
|
+
onRunComplete(): Promise<void>;
|
|
41
|
+
getLastError(): Error | undefined;
|
|
42
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { type Target } from "./envelope.js";
|
|
2
|
+
export interface SentryTestsOptions {
|
|
3
|
+
/** Project DSN. Default: `SENTRY_DSN`. */
|
|
4
|
+
dsn?: string;
|
|
5
|
+
/**
|
|
6
|
+
* Also (or only) send to Spotlight, Sentry's local debugging UI (https://spotlightjs.com):
|
|
7
|
+
* `true` for its default sidecar at http://localhost:8969/stream, or the stream URL.
|
|
8
|
+
* Default: `SENTRY_SPOTLIGHT` (`1`/`true` or a URL).
|
|
9
|
+
*/
|
|
10
|
+
spotlight?: boolean | string;
|
|
11
|
+
/** Default: `SENTRY_ENVIRONMENT`, else "ci" under CI and "local" otherwise. */
|
|
12
|
+
environment?: string;
|
|
13
|
+
/** Default: `SENTRY_RELEASE`, else `GITHUB_SHA`, else the checkout's commit. */
|
|
14
|
+
release?: string;
|
|
15
|
+
/** One trace per test file: spans for describe blocks, tests, hooks and traced calls (default true). */
|
|
16
|
+
traces?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Fraction of test files sent as traces, for suites big enough to matter for span quota.
|
|
19
|
+
* Failures and flaky tests are always sent. Default: `SENTRY_TRACES_SAMPLE_RATE`, else 1.
|
|
20
|
+
*/
|
|
21
|
+
tracesSampleRate?: number;
|
|
22
|
+
/** An issue per failing test, grouped by test (default true). */
|
|
23
|
+
failures?: boolean;
|
|
24
|
+
/** An issue per test that failed and then passed on a retry (default true). */
|
|
25
|
+
flaky?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Attach the page when a test failed (its HTML, and with @ctrl/dom its accessibility snapshot)
|
|
28
|
+
* and whatever "sentry-tests:failure" subscribers add, to failure events (default true).
|
|
29
|
+
*/
|
|
30
|
+
attachments?: boolean;
|
|
31
|
+
/** Extra tags on every event. */
|
|
32
|
+
tags?: Record<string, string>;
|
|
33
|
+
}
|
|
34
|
+
export interface ResolvedOptions {
|
|
35
|
+
targets: Target[];
|
|
36
|
+
environment: string;
|
|
37
|
+
release: string | undefined;
|
|
38
|
+
traces: boolean;
|
|
39
|
+
tracesSampleRate: number;
|
|
40
|
+
failures: boolean;
|
|
41
|
+
flaky: boolean;
|
|
42
|
+
attachments: boolean;
|
|
43
|
+
tags: Record<string, string>;
|
|
44
|
+
}
|
|
45
|
+
/** Where reports go and what they contain, or null when there is nowhere to send them. */
|
|
46
|
+
export declare function resolveOptions(options?: SentryTestsOptions, env?: NodeJS.ProcessEnv): ResolvedOptions | null;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { CollectedFile } from "./collector.js";
|
|
2
|
+
import { type Envelope, type ParsedFrame } from "./envelope.js";
|
|
3
|
+
import type { ResolvedOptions } from "./options.js";
|
|
4
|
+
export interface ErrorInfo {
|
|
5
|
+
name?: string;
|
|
6
|
+
message: string;
|
|
7
|
+
stack?: string;
|
|
8
|
+
/** Frames the runner already parsed and source-mapped (Vitest), newest first as V8 prints them. */
|
|
9
|
+
frames?: ParsedFrame[];
|
|
10
|
+
}
|
|
11
|
+
export interface TestCaseReport {
|
|
12
|
+
/** The test's key in CollectedFile.tests: Vitest's test id, Jest's full name. */
|
|
13
|
+
key: string;
|
|
14
|
+
/** Full name: describe blocks and the test, joined with " > ". */
|
|
15
|
+
name: string;
|
|
16
|
+
state: "pass" | "fail" | "skip";
|
|
17
|
+
/** Milliseconds. */
|
|
18
|
+
duration: number;
|
|
19
|
+
retries: number;
|
|
20
|
+
/** Failed, then passed on a retry. */
|
|
21
|
+
flaky: boolean;
|
|
22
|
+
errors: ErrorInfo[];
|
|
23
|
+
/** The test's console output (Vitest reports it per test), shown as breadcrumbs. */
|
|
24
|
+
console?: ConsoleEntry[];
|
|
25
|
+
}
|
|
26
|
+
export interface ConsoleEntry {
|
|
27
|
+
/** Epoch seconds. */
|
|
28
|
+
timestamp: number;
|
|
29
|
+
stream: "stdout" | "stderr";
|
|
30
|
+
message: string;
|
|
31
|
+
}
|
|
32
|
+
export interface TestFileReport {
|
|
33
|
+
runner: "vitest" | "jest";
|
|
34
|
+
/** Paths in events are relative to this. */
|
|
35
|
+
root: string;
|
|
36
|
+
/** Absolute path of the test file. */
|
|
37
|
+
file: string;
|
|
38
|
+
project?: string;
|
|
39
|
+
tests: TestCaseReport[];
|
|
40
|
+
/** What the worker's runner collected; without it there is no trace, only the issues. */
|
|
41
|
+
collected: CollectedFile | null;
|
|
42
|
+
environmentSetupMs?: number;
|
|
43
|
+
/**
|
|
44
|
+
* Failures outside any test, each an issue under its name: the file failing to load (an import
|
|
45
|
+
* or syntax error, named FILE_FAILURE), a describe block's beforeAll or afterAll throwing.
|
|
46
|
+
*/
|
|
47
|
+
outsideTests?: Array<{
|
|
48
|
+
name: string;
|
|
49
|
+
error: ErrorInfo;
|
|
50
|
+
}>;
|
|
51
|
+
}
|
|
52
|
+
export interface BuiltReport {
|
|
53
|
+
envelopes: Envelope[];
|
|
54
|
+
traces: number;
|
|
55
|
+
failures: number;
|
|
56
|
+
flaky: number;
|
|
57
|
+
}
|
|
58
|
+
/** The test name a file-level failure is reported under. */
|
|
59
|
+
export declare const FILE_FAILURE = "(file failed to load)";
|
|
60
|
+
export declare function buildReport(report: TestFileReport, options: ResolvedOptions): BuiltReport;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { TestRunner } from "vitest";
|
|
2
|
+
import { type CollectedFile } from "./collector.js";
|
|
3
|
+
declare module "vitest" {
|
|
4
|
+
interface TaskMeta {
|
|
5
|
+
/** The file's spans and failures, on the file task (@ctrl/sentry-tests). */
|
|
6
|
+
sentryTests?: CollectedFile;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
type RunnerClass = new (...args: any[]) => TestRunner;
|
|
10
|
+
/** `Base` (Vitest's TestRunner or a subclass of it), recording spans. */
|
|
11
|
+
export declare function withSentry<T extends RunnerClass>(Base: T): T;
|
|
12
|
+
declare const _default: typeof TestRunner;
|
|
13
|
+
export default _default;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Reporter, TestModule, Vitest } from "vitest/node";
|
|
2
|
+
import type { Plugin } from "vitest/config";
|
|
3
|
+
import type { UserConsoleLog } from "vitest";
|
|
4
|
+
import { type SentryTestsOptions, type ResolvedOptions } from "./options.js";
|
|
5
|
+
export type { SentryTestsOptions } from "./options.js";
|
|
6
|
+
/** The Vitest plugin: spans from the runner, events from the reporter, when there is somewhere to send them. */
|
|
7
|
+
export declare function sentryTests(options?: SentryTestsOptions): Plugin;
|
|
8
|
+
/**
|
|
9
|
+
* The reporter, which the plugin adds. With a runner built on withSentry(), add it yourself:
|
|
10
|
+
* `reporters: ["default", "@ctrl/sentry-tests/vitest"]`.
|
|
11
|
+
*/
|
|
12
|
+
export declare class SentryReporter implements Reporter {
|
|
13
|
+
#private;
|
|
14
|
+
readonly options: ResolvedOptions | null;
|
|
15
|
+
constructor(options?: SentryTestsOptions);
|
|
16
|
+
onInit(vitest: Vitest): void;
|
|
17
|
+
onUserConsoleLog(log: UserConsoleLog): void;
|
|
18
|
+
onTestModuleEnd(module: TestModule): void;
|
|
19
|
+
onTestRunEnd(): Promise<void>;
|
|
20
|
+
}
|
|
21
|
+
export default SentryReporter;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { n as realNow, t as createCollector } from "./_chunks/collector-DiJaNK5H.js";
|
|
2
|
+
import { relative } from "node:path";
|
|
3
|
+
import { TestRunner } from "vitest";
|
|
4
|
+
|
|
5
|
+
// src/vitest-runner.ts
|
|
6
|
+
if (Error.stackTraceLimit < 50) Error.stackTraceLimit = 50;
|
|
7
|
+
/**
|
|
8
|
+
* The hook phases that get a span when they run hooks. Vitest also enters "test.cleanup" and
|
|
9
|
+
* "suite.cleanup" whenever hooks ran, whether or not one returned a cleanup, so those get none.
|
|
10
|
+
*/
|
|
11
|
+
const HOOK_PHASES = {
|
|
12
|
+
"test.beforeEach": "beforeEach",
|
|
13
|
+
"test.afterEach": "afterEach",
|
|
14
|
+
"suite.beforeAll": "beforeAll",
|
|
15
|
+
"suite.afterAll": "afterAll"
|
|
16
|
+
};
|
|
17
|
+
/** `Base` (Vitest's TestRunner or a subclass of it), recording spans. */
|
|
18
|
+
function withSentry(Base) {
|
|
19
|
+
const getHooks = Base.getSuiteHooks;
|
|
20
|
+
const base = Base.prototype;
|
|
21
|
+
return class SentryTestRunner extends Base {
|
|
22
|
+
/** Subscribed for one run of files (onBeforeCollect to onAfterRunFiles). */
|
|
23
|
+
#collector;
|
|
24
|
+
#files = /* @__PURE__ */ new Map();
|
|
25
|
+
#tasks = /* @__PURE__ */ new Map();
|
|
26
|
+
/** The task behind each describe block's and test's span. */
|
|
27
|
+
#taskOf = /* @__PURE__ */ new WeakMap();
|
|
28
|
+
/** Each file's collect span: a batch of files is collected before any of them runs. */
|
|
29
|
+
#collected = /* @__PURE__ */ new Map();
|
|
30
|
+
#file;
|
|
31
|
+
#retry = 0;
|
|
32
|
+
constructor(...args) {
|
|
33
|
+
super(...args);
|
|
34
|
+
const trace = this.trace;
|
|
35
|
+
this.trace = ((name, attributes, cb) => this.#trace(trace, name, attributes, cb));
|
|
36
|
+
}
|
|
37
|
+
onBeforeCollect(paths) {
|
|
38
|
+
this.#collector = createCollector();
|
|
39
|
+
return base.onBeforeCollect?.call(this, paths);
|
|
40
|
+
}
|
|
41
|
+
onAfterRunFiles(files) {
|
|
42
|
+
this.#collector.stop();
|
|
43
|
+
return super.onAfterRunFiles(files);
|
|
44
|
+
}
|
|
45
|
+
onCollected(files) {
|
|
46
|
+
for (const file of files) {
|
|
47
|
+
this.#files.set(file.filepath, file);
|
|
48
|
+
index(file, this.#tasks);
|
|
49
|
+
}
|
|
50
|
+
return base.onCollected?.call(this, files);
|
|
51
|
+
}
|
|
52
|
+
onBeforeTryTask(test, options) {
|
|
53
|
+
this.#retry = options.retry;
|
|
54
|
+
return super.onBeforeTryTask(test, options);
|
|
55
|
+
}
|
|
56
|
+
async onTaskFinished(test) {
|
|
57
|
+
if (test.result?.state === "fail") await this.#collector.captureFailure(this.#collector.test(test.id), fullName(test), test.result.errors?.at(-1), globalThis.document);
|
|
58
|
+
await base.onTaskFinished?.call(this, test);
|
|
59
|
+
}
|
|
60
|
+
async onAfterRunSuite(suite) {
|
|
61
|
+
if (suite === this.#file) {
|
|
62
|
+
const collected = this.#collector.finish();
|
|
63
|
+
const collect = this.#collected.get(this.#file.filepath);
|
|
64
|
+
if (collect !== void 0) collected.spans.push(collect);
|
|
65
|
+
suite.meta.sentryTests = collected;
|
|
66
|
+
}
|
|
67
|
+
return super.onAfterRunSuite(suite);
|
|
68
|
+
}
|
|
69
|
+
#trace(trace, name, attributes, cb) {
|
|
70
|
+
const call = () => trace(name, attributes, cb);
|
|
71
|
+
const attrs = typeof attributes === "function" ? {} : attributes;
|
|
72
|
+
const c = this.#collector;
|
|
73
|
+
switch (name) {
|
|
74
|
+
case "collect_spec": {
|
|
75
|
+
const path = String(attrs["code.file.path"]);
|
|
76
|
+
const span = c.open("test.collect", `collect ${relative(this.config.root, path)}`);
|
|
77
|
+
return this.#settle(span, call, () => false, () => this.#collected.set(path, span.span));
|
|
78
|
+
}
|
|
79
|
+
case "run.spec":
|
|
80
|
+
this.#file = this.#files.get(String(attrs["code.file.path"]));
|
|
81
|
+
return call();
|
|
82
|
+
case "run.suite":
|
|
83
|
+
case "run.test": {
|
|
84
|
+
const task = this.#tasks.get(String(attrs[name === "run.suite" ? "vitest.suite.id" : "vitest.test.id"]));
|
|
85
|
+
if (task.mode === "skip" || task.mode === "todo") return call();
|
|
86
|
+
const line = attrs["code.line.number"];
|
|
87
|
+
const data = typeof line === "number" ? { "code.lineno": line } : {};
|
|
88
|
+
const test = name === "run.test" ? c.test(task.id) : void 0;
|
|
89
|
+
const span = c.open(name === "run.test" ? "test.case" : "test.suite", fullName(task), data, test);
|
|
90
|
+
test?.spanIds.push(span.span.span_id);
|
|
91
|
+
this.#taskOf.set(span, task);
|
|
92
|
+
return this.#settle(span, call, () => task.result?.state === "fail");
|
|
93
|
+
}
|
|
94
|
+
case "test.callback": {
|
|
95
|
+
const span = c.open("test.fn", this.#taskOf.get(c.current()).name, this.#retry > 0 ? { "test.retry": this.#retry } : {});
|
|
96
|
+
return this.#settle(span, call, () => false);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const hook = HOOK_PHASES[name];
|
|
100
|
+
if (hook === void 0) return call();
|
|
101
|
+
const hooks = this.#hookCount(hook);
|
|
102
|
+
if (hooks === 0) return call();
|
|
103
|
+
const data = { hooks };
|
|
104
|
+
if (this.#retry > 0 && (hook === "beforeEach" || hook === "afterEach")) data["test.retry"] = this.#retry;
|
|
105
|
+
return this.#settle(c.open("test.hook", hook, data), call, () => false);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* How many `hook`s the phase about to start runs. A test's beforeEach/afterEach run its describe
|
|
109
|
+
* blocks' and its file's too, walked as Vitest's callSuiteHook does: a top-level test or describe
|
|
110
|
+
* block has no `suite`, only `file`.
|
|
111
|
+
*/
|
|
112
|
+
#hookCount(hook) {
|
|
113
|
+
const current = this.#collector.current();
|
|
114
|
+
if (hook === "beforeAll" || hook === "afterAll") {
|
|
115
|
+
const suite = current === null ? this.#file : this.#taskOf.get(current);
|
|
116
|
+
return getHooks(suite)[hook].length;
|
|
117
|
+
}
|
|
118
|
+
const test = this.#taskOf.get(current);
|
|
119
|
+
let count = 0;
|
|
120
|
+
for (let suite = test.suite ?? test.file; suite !== void 0; suite = "filepath" in suite ? void 0 : suite.suite ?? suite.file) count += getHooks(suite)[hook].length;
|
|
121
|
+
return count;
|
|
122
|
+
}
|
|
123
|
+
/** Run `fn` in `span` and end it when `fn` returns or settles; failed when it throws or `failed()` says so. */
|
|
124
|
+
#settle(span, fn, failed, keep) {
|
|
125
|
+
const c = this.#collector;
|
|
126
|
+
const end = (error) => {
|
|
127
|
+
if (keep === void 0) return c.close(span, error);
|
|
128
|
+
span.span.timestamp = realNow();
|
|
129
|
+
if (error) span.span.status = "internal_error";
|
|
130
|
+
keep();
|
|
131
|
+
};
|
|
132
|
+
let result;
|
|
133
|
+
try {
|
|
134
|
+
result = c.run(span, fn);
|
|
135
|
+
} catch (error) {
|
|
136
|
+
end(true);
|
|
137
|
+
throw error;
|
|
138
|
+
}
|
|
139
|
+
if (!(result instanceof Promise)) {
|
|
140
|
+
end(failed());
|
|
141
|
+
return result;
|
|
142
|
+
}
|
|
143
|
+
return result.then((value) => {
|
|
144
|
+
end(failed());
|
|
145
|
+
return value;
|
|
146
|
+
}, (error) => {
|
|
147
|
+
end(true);
|
|
148
|
+
throw error;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function index(suite, tasks) {
|
|
154
|
+
for (const task of suite.tasks) {
|
|
155
|
+
tasks.set(task.id, task);
|
|
156
|
+
if (task.type === "suite") index(task, tasks);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** Describe blocks and the task, joined with " > " (TestCase.fullName in Vitest's reporter API). */
|
|
160
|
+
function fullName(task) {
|
|
161
|
+
const names = [task.name];
|
|
162
|
+
for (let suite = task.suite; suite !== void 0 && suite !== task.file; suite = suite.suite) names.unshift(suite.name);
|
|
163
|
+
return names.join(" > ");
|
|
164
|
+
}
|
|
165
|
+
var vitest_runner_default = withSentry(TestRunner);
|
|
166
|
+
|
|
167
|
+
export { vitest_runner_default as default, withSentry };
|