@provino/dialect-jest 0.0.0-stage → 0.1.0-rc.0

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,125 @@
1
+ import { fileURLToPath } from "node:url";
2
+
3
+ // src/results/failure-message.ts
4
+ /**
5
+ * The directory of Provino's own packages when this code runs from a workspace checkout
6
+ * (`<repo>/packages/`, from the built umbrella's `packages/provino/dist` as well as a package's
7
+ * `src`); `undefined` from an install. Decoded, as stack frames are: `import.meta.url` is
8
+ * percent-encoded.
9
+ */
10
+ const PROVINO_PACKAGES = (() => {
11
+ try {
12
+ const path = fileURLToPath(import.meta.url).replaceAll("\\", "/");
13
+ return /^(.*\/packages\/)[^/]+\/(?:src|dist)\//.exec(path)?.[1];
14
+ } catch {
15
+ return;
16
+ }
17
+ })();
18
+ /**
19
+ * Whether a frame is in one of Provino's own packages: an installed `node_modules/provino` or
20
+ * `node_modules/@provino/<name>` (`jest-message-util` drops only `jest*` frames, so an install's
21
+ * would stay), or `<packages>/<name>/src|dist/` of this checkout (a test, a fixture or a project
22
+ * that sits under `packages/` is the user's). `file://` frames keep their percent-encoding, so both
23
+ * the line and its decoded form are looked at.
24
+ * @internal
25
+ */
26
+ function isProvinoFrame(frame, packages) {
27
+ let decoded = frame;
28
+ try {
29
+ decoded = decodeURI(frame);
30
+ } catch {}
31
+ for (const text of new Set([frame, decoded].map((line) => line.replaceAll("\\", "/")))) {
32
+ if (/\/node_modules\/(?:provino|@provino\/[^/]+)\//.test(text)) return true;
33
+ if (packages === void 0) continue;
34
+ const at = text.indexOf(packages);
35
+ if (at !== -1 && /^[^/]+\/(?:src|dist)\//.test(text.slice(at + packages.length))) return true;
36
+ }
37
+ return false;
38
+ }
39
+ /**
40
+ * `stack` without the frames of Provino's own packages (D104: Jest hides its own internals the
41
+ * same way, in `removeInternalStackEntries`), so the text under a failure is the user's code.
42
+ * @internal
43
+ */
44
+ function hideProvinoFrames(stack, packages = PROVINO_PACKAGES) {
45
+ return stack.split("\n").filter((line) => !/^\s+at /.test(line) || !isProvinoFrame(line, packages)).join("\n");
46
+ }
47
+ let loaded;
48
+ function messageUtil() {
49
+ loaded ??= import("jest-message-util");
50
+ return loaded;
51
+ }
52
+ /**
53
+ * What `formatResultsErrors` should format for one failure. Jest hands it the thrown `Error`
54
+ * itself (`failureDetailsToErrorOrStack`, jest-message-util 30.5.2 index.ts L640-L658), and formats
55
+ * its `stack` (`formatErrorStackWithSeen` L564-L568); our `failureDetails[i]` is a plain object, so
56
+ * the package formats `failureMessages[i]` instead, which is `flattenErrorStack`'s message, then the
57
+ * stack, when the stack lacks the message. The two differ when the message changed after the stack
58
+ * was taken (dom-testing-library's `waitFor` appends the DOM to a timed-out error's message), so
59
+ * then the stack is formatted, as Jest formats it. A failure that also carries a `cause` or
60
+ * `errors` (more text after the two) is left as it is.
61
+ */
62
+ function formattedSource(content, detail) {
63
+ if (typeof detail !== "object" || detail === null) return content;
64
+ const { message, stack } = detail;
65
+ if (typeof message !== "string" || typeof stack !== "string" || stack === "") return content;
66
+ if (message === "" || stack.includes(message)) return content;
67
+ const enumerable = (key) => Object.prototype.propertyIsEnumerable.call(detail, key);
68
+ if (enumerable("message") && enumerable("stack")) return content;
69
+ return content === message + stack.replace(/^Error:?\s*\n/, "\n") ? stack : content;
70
+ }
71
+ /**
72
+ * The text Jest prints for the failures of one file's tests (`formatResultsErrors`), or `null`
73
+ * when none failed.
74
+ * @public
75
+ */
76
+ async function formatTestFailures(assertionResults, config, options, testPath) {
77
+ if (assertionResults.every((result) => result.failureMessages.length === 0)) return null;
78
+ const util = await messageUtil();
79
+ const shown = assertionResults.map((result) => ({
80
+ ...result,
81
+ failureMessages: result.failureMessages.map((message, index) => hideProvinoFrames(formattedSource(message, result.failureDetails[index])))
82
+ }));
83
+ return util.formatResultsErrors(shown, {
84
+ rootDir: config.rootDir,
85
+ testMatch: [...config.testMatch]
86
+ }, options, testPath);
87
+ }
88
+ /** A stack line that is a frame. */
89
+ const FRAME_LINE = /^\s*at .*:\d+:\d+\)?$/m;
90
+ /**
91
+ * A file error's stack without Provino's frames. `separateMessageFromStack` (jest-message-util
92
+ * 30.5.1, index.ts L573-L589) takes the message up to the first frame, which in Jest is one of its
93
+ * own: when every frame was Provino's, a frame that `removeInternalStackEntries` always drops
94
+ * (`ANONYMOUS_FN_IGNORE`) stands in for them, so a message over several lines is read whole
95
+ * (`snapshot/resolver-missing-member`).
96
+ */
97
+ function withoutProvinoFrames(stack) {
98
+ const hidden = hideProvinoFrames(stack);
99
+ return FRAME_LINE.test(stack) && !FRAME_LINE.test(hidden) ? `${hidden}\n at <anonymous>:1:1` : hidden;
100
+ }
101
+ /**
102
+ * The text Jest prints for an error that ended a file (`formatExecError`).
103
+ * @public
104
+ */
105
+ async function formatFileError(error, config, options, testPath) {
106
+ return (await messageUtil()).formatExecError(typeof error.stack === "string" ? {
107
+ ...error,
108
+ stack: withoutProvinoFrames(error.stack)
109
+ } : error, {
110
+ rootDir: config.rootDir,
111
+ testMatch: [...config.testMatch]
112
+ }, options, testPath);
113
+ }
114
+ /**
115
+ * One retried attempt's error as Jest prints it (`formatErrorStack`, what `retryMessages` holds).
116
+ * @public
117
+ */
118
+ async function formatRetryError(stack, config, options, testPath) {
119
+ return (await messageUtil()).formatErrorStack(hideProvinoFrames(stack), {
120
+ rootDir: config.rootDir,
121
+ testMatch: [...config.testMatch]
122
+ }, options, testPath);
123
+ }
124
+
125
+ export { hideProvinoFrames as i, formatRetryError as n, formatTestFailures as r, formatFileError as t };
@@ -0,0 +1,11 @@
1
+ import { GlobalSetupFailure } from "@provino/types";
2
+ /**
3
+ * Reports a Jest global hook that threw (`orchestrator.globalSetup.failure`), as `runGlobalHook`
4
+ * does (jest-core 30.5.2): an `Error` whose message can be rewritten is rewritten in place to
5
+ * `Jest: Got error running <hook> - <path>, reason: <message>` and kept, so its class and frames
6
+ * stay (`cli/global-setup-throws`, `-typeerror`). Any other value, or an error whose message is
7
+ * fixed, becomes a new `Error` with the same text and the value printed by pretty-format three
8
+ * levels deep (`-object`, `-string`); its stack is the header alone, so no frame of ours shows.
9
+ * @public
10
+ */
11
+ export declare const globalSetupFailure: GlobalSetupFailure;
@@ -0,0 +1,40 @@
1
+ import { format } from "pretty-format";
2
+
3
+ // src/global-setup/index.ts
4
+ /**
5
+ * Whether an error's `message` can be rewritten: Jest rewrites it in place to say which hook
6
+ * failed and keeps the error (its class and stack). It is writable as its own property, or as the
7
+ * prototype's, which an assignment then shadows.
8
+ */
9
+ function hasWritableMessage(error) {
10
+ return Object.getOwnPropertyDescriptor(error, "message")?.writable === true || Object.getOwnPropertyDescriptor(Object.getPrototypeOf(error), "message")?.writable === true;
11
+ }
12
+ /** A value as pretty-format prints it three levels deep, or as text when pretty-format throws. */
13
+ function printed(value) {
14
+ try {
15
+ return format(value, { maxDepth: 3 });
16
+ } catch {
17
+ return String(value);
18
+ }
19
+ }
20
+ /**
21
+ * Reports a Jest global hook that threw (`orchestrator.globalSetup.failure`), as `runGlobalHook`
22
+ * does (jest-core 30.5.2): an `Error` whose message can be rewritten is rewritten in place to
23
+ * `Jest: Got error running <hook> - <path>, reason: <message>` and kept, so its class and frames
24
+ * stay (`cli/global-setup-throws`, `-typeerror`). Any other value, or an error whose message is
25
+ * fixed, becomes a new `Error` with the same text and the value printed by pretty-format three
26
+ * levels deep (`-object`, `-string`); its stack is the header alone, so no frame of ours shows.
27
+ * @public
28
+ */
29
+ const globalSetupFailure = (thrown, { hook, path }) => {
30
+ const text = (reason) => `Jest: Got error running ${hook} - ${path}, reason: ${reason}`;
31
+ if (thrown instanceof Error && hasWritableMessage(thrown)) {
32
+ thrown.message = text(thrown.message);
33
+ return thrown;
34
+ }
35
+ const error = new Error(text(printed(thrown)));
36
+ error.stack = `Error: ${error.message}`;
37
+ return error;
38
+ };
39
+
40
+ export { globalSetupFailure };
@@ -0,0 +1,41 @@
1
+ import { Dialect, ExpectPolicy, HoistPolicy, MockPolicy, PolicyOverrides } from "@provino/types";
2
+ /**
3
+ * Jest 30.5's policy: every key the research gives a Jest value for, layered over the neutral
4
+ * policy by `resolvePolicy`. This is `jestDialect.policy`, typed as the overrides it is (a
5
+ * `Dialect`'s own `policy` is the overrides or a factory).
6
+ * @public
7
+ */
8
+ export declare const jestPolicy: PolicyOverrides;
9
+ /**
10
+ * What a project config may put under `jest`: nothing yet, so only an empty object. Jest's own
11
+ * options arrive with the config reader (T11.x, B14).
12
+ * @public
13
+ */
14
+ type JestDialectOptions = Record<string, never>;
15
+ declare module "@provino/types" {
16
+ interface DialectRegistry {
17
+ jest: JestDialectOptions;
18
+ }
19
+ }
20
+ /**
21
+ * The Jest 30.5 dialect: its policy values and the globals it injects. The config reader, the CLI
22
+ * flags and the `@jest/globals` facade come with T11.x and T11.10 (B14, B17).
23
+ * @public
24
+ */
25
+ export declare const jestDialect: Dialect<"jest">;
26
+ /**
27
+ * Jest 30.5's `expect` policy.
28
+ * @public
29
+ */
30
+ export declare const jestExpectPolicy: ExpectPolicy;
31
+ /**
32
+ * Jest 30.5's mock-hoisting policy.
33
+ * @public
34
+ */
35
+ export declare const jestHoistPolicy: HoistPolicy<"mock" | "unmock" | "deepUnmock" | "enableAutomock" | "disableAutomock", never>;
36
+ /**
37
+ * Jest 30.5's mock policy.
38
+ * @public
39
+ */
40
+ export declare const jestMockPolicy: MockPolicy;
41
+ export type { JestDialectOptions };