@fgv/ts-utils-jest 5.0.0-21 → 5.0.0-23

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.
Files changed (50) hide show
  1. package/package.json +3 -3
  2. package/src/__snapshots__/index.test.ts.snap +0 -24
  3. package/src/helpers/fsHelpers.ts +0 -184
  4. package/src/helpers/index.ts +0 -1
  5. package/src/index.ts +0 -27
  6. package/src/matchers/index.ts +0 -29
  7. package/src/matchers/toFail/__snapshots__/index.test.ts.snap +0 -17
  8. package/src/matchers/toFail/index.ts +0 -45
  9. package/src/matchers/toFail/predicate.ts +0 -7
  10. package/src/matchers/toFailTest/__snapshots__/index.test.ts.snap +0 -15
  11. package/src/matchers/toFailTest/index.ts +0 -48
  12. package/src/matchers/toFailTest/predicate.ts +0 -8
  13. package/src/matchers/toFailTestAndMatchSnapshot/__snapshots__/index.test.ts.snap +0 -14
  14. package/src/matchers/toFailTestAndMatchSnapshot/index.ts +0 -48
  15. package/src/matchers/toFailTestAndMatchSnapshot/predicate.ts +0 -12
  16. package/src/matchers/toFailTestWith/__snapshots__/index.test.ts.snap +0 -34
  17. package/src/matchers/toFailTestWith/index.ts +0 -56
  18. package/src/matchers/toFailTestWith/predicate.ts +0 -24
  19. package/src/matchers/toFailWith/__snapshots__/index.test.ts.snap +0 -25
  20. package/src/matchers/toFailWith/index.ts +0 -52
  21. package/src/matchers/toFailWith/predicate.ts +0 -16
  22. package/src/matchers/toFailWithDetail/__snapshots__/index.test.ts.snap +0 -41
  23. package/src/matchers/toFailWithDetail/index.ts +0 -63
  24. package/src/matchers/toFailWithDetail/predicate.ts +0 -27
  25. package/src/matchers/toHaveBeenCalledWithArgumentsMatching/__snapshots__/index.test.ts.snap +0 -82
  26. package/src/matchers/toHaveBeenCalledWithArgumentsMatching/index.ts +0 -100
  27. package/src/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.ts +0 -18
  28. package/src/matchers/toSucceed/__snapshots__/index.test.ts.snap +0 -17
  29. package/src/matchers/toSucceed/index.ts +0 -45
  30. package/src/matchers/toSucceed/predicate.ts +0 -7
  31. package/src/matchers/toSucceedAndMatchInlineSnapshot/__snapshots__/index.test.ts.snap +0 -15
  32. package/src/matchers/toSucceedAndMatchInlineSnapshot/index.ts +0 -44
  33. package/src/matchers/toSucceedAndMatchSnapshot/__snapshots__/index.test.ts.snap +0 -27
  34. package/src/matchers/toSucceedAndMatchSnapshot/index.ts +0 -47
  35. package/src/matchers/toSucceedAndSatisfy/__snapshots__/index.test.ts.snap +0 -37
  36. package/src/matchers/toSucceedAndSatisfy/index.ts +0 -78
  37. package/src/matchers/toSucceedAndSatisfy/predicate.ts +0 -22
  38. package/src/matchers/toSucceedWith/__snapshots__/index.test.ts.snap +0 -25
  39. package/src/matchers/toSucceedWith/index.ts +0 -58
  40. package/src/matchers/toSucceedWith/predicate.ts +0 -14
  41. package/src/matchers/toSucceedWithDetail/__snapshots__/index.test.ts.snap +0 -41
  42. package/src/matchers/toSucceedWithDetail/index.ts +0 -62
  43. package/src/matchers/toSucceedWithDetail/predicate.ts +0 -23
  44. package/src/resolvers/cli.ts +0 -21
  45. package/src/resolvers/ide.ts +0 -21
  46. package/src/ts-utils.ts +0 -1
  47. package/src/types/index.ts +0 -107
  48. package/src/utils/colorHelpers.ts +0 -82
  49. package/src/utils/matcherHelpers.ts +0 -64
  50. package/src/utils/snapshotResolver.ts +0 -28
@@ -1,78 +0,0 @@
1
- import { Result, ResultValueType } from '@fgv/ts-utils';
2
- import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
3
- import { matcherName, predicate } from './predicate';
4
-
5
- import { matcherHint } from 'jest-matcher-utils';
6
-
7
- declare global {
8
- // eslint-disable-next-line @typescript-eslint/no-namespace
9
- namespace jest {
10
- // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars, @typescript-eslint/naming-convention
11
- interface Matchers<R, T> {
12
- /**
13
- * Use .toSucceedAndSatisfy to verify that a Result<T> is a success
14
- * and that the supplied test function returns true (or void)
15
- * for the resulting value
16
- * @param test -
17
- */
18
- toSucceedAndSatisfy(test: (value: ResultValueType<T>) => boolean | void): R;
19
- }
20
- }
21
- }
22
-
23
- function passMessage<T>(received: Result<T>): () => string {
24
- const expected = 'successful callback';
25
- const got = [printReceivedResult(received)];
26
- got.push(' Callback returned true');
27
-
28
- return () =>
29
- [
30
- matcherHint(`.not.${matcherName}`, 'result', 'callback'),
31
- printExpectedResult('success', false, expected),
32
- ...got
33
- ].join('\n');
34
- }
35
-
36
- function failMessage<T>(received: Result<T>, cbResult: Result<boolean | void>): () => string {
37
- const expected = 'successful callback';
38
- const got = [printReceivedResult(received)];
39
-
40
- /* c8 ignore else */
41
- if (cbResult.isFailure()) {
42
- got.push(cbResult.message);
43
- } else if (cbResult.value === false) {
44
- got.push(' Callback returned false');
45
- } else if (cbResult.value === undefined) {
46
- got.push(' Callback was not invoked');
47
- } else {
48
- throw new Error('Internal error: toSucceedAndSatisfy.failMessage passed success with true');
49
- }
50
-
51
- return () =>
52
- [
53
- matcherHint(`${matcherName}`, 'result', 'callback'),
54
- printExpectedResult('success', true, expected),
55
- ...got
56
- ].join('\n');
57
- }
58
-
59
- export default {
60
- toSucceedAndSatisfy: function <T>(
61
- this: jest.MatcherContext,
62
- received: Result<T>,
63
- test: (value: T) => boolean | void
64
- ): jest.CustomMatcherResult {
65
- // For the normal (not '.not') case, we do not want to capture exceptions
66
- // so that the IDE can display exactly the line on which the failure case.
67
- // For the .not case, we want to swallow exceptions or expect failures since
68
- // we're just testing failure and not the reason.
69
- const capture = this.isNot;
70
- const cbResult = predicate(received, test, !!capture);
71
- const pass = cbResult.isSuccess() && cbResult.value === true;
72
- if (pass) {
73
- return { pass: true, message: passMessage(received) };
74
- }
75
-
76
- return { pass: false, message: failMessage(received, cbResult) };
77
- }
78
- };
@@ -1,22 +0,0 @@
1
- import { Result, captureResult, fail, succeed } from '../../ts-utils';
2
-
3
- export const matcherName: string = 'toSucceedAndSatisfy';
4
-
5
- // If the result is successful and callback does not throw, returns Success with the callback return value, or
6
- // true if the callback returns undefined
7
- // If the result is successful but the callback throws, returns Failure with the thrown error
8
- // If the result is failure, returns Success with undefined
9
- export function predicate<T>(
10
- received: Result<T>,
11
- cb: (value: T) => boolean | void,
12
- capture: boolean
13
- ): Result<boolean | undefined> {
14
- if (received.isSuccess()) {
15
- const cbResult = capture ? captureResult(() => cb(received.value)) : succeed(cb(received.value));
16
- if (cbResult.isSuccess()) {
17
- return succeed(cbResult.value === true || cbResult.value === undefined);
18
- }
19
- return fail(` Callback failed with:\n ${cbResult.message}`);
20
- }
21
- return succeed(undefined);
22
- }
@@ -1,25 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`.toSucceedWith reports details when it fails due to a failure result: toFailTestAndMatchSnapshot 1`] = `
4
- "expect(received).toSucceedWith(expected)
5
- Expected:
6
- Success with \\"oops\\"
7
- Received:
8
- Failure with \\"oops\\""
9
- `;
10
-
11
- exports[`.toSucceedWith reports details when it fails due to a success result with .not: toFailTestAndMatchSnapshot 1`] = `
12
- "expect(received).not.toSucceedWith(expected)
13
- Expected:
14
- Not success with \\"hello\\"
15
- Received:
16
- Success with \\"hello\\""
17
- `;
18
-
19
- exports[`.toSucceedWith reports details when it fails due to success with a non-matching value: toFailTestAndMatchSnapshot 1`] = `
20
- "expect(received).toSucceedWith(expected)
21
- Expected:
22
- Success with \\"goodbye\\"
23
- Received:
24
- Success with \\"hello\\""
25
- `;
@@ -1,58 +0,0 @@
1
- import { Result, ResultValueType } from '@fgv/ts-utils';
2
- import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
3
- import { matcherName, predicate } from './predicate';
4
-
5
- import { matcherHint } from 'jest-matcher-utils';
6
-
7
- declare global {
8
- // eslint-disable-next-line @typescript-eslint/no-namespace
9
- namespace jest {
10
- // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars, @typescript-eslint/naming-convention
11
- interface Matchers<R, T> {
12
- /**
13
- * Use .toSucceedWith to verify that a Result\<T\> is a success
14
- * and that the result value matches the supplied value
15
- * @param expected -
16
- */
17
- toSucceedWith(expected: ResultValueType<T> | RegExp): R;
18
- }
19
- }
20
- }
21
-
22
- function passMessage<T extends Result<unknown>>(
23
- received: T,
24
- expected: ResultValueType<T> | RegExp
25
- ): () => string {
26
- return () =>
27
- [
28
- matcherHint(`.not.${matcherName}`),
29
- printExpectedResult('success', false, expected),
30
- printReceivedResult(received)
31
- ].join('\n');
32
- }
33
-
34
- function failMessage<T extends Result<unknown>>(
35
- received: T,
36
- expected: ResultValueType<T> | RegExp
37
- ): () => string {
38
- return () =>
39
- [
40
- matcherHint(`${matcherName}`),
41
- printExpectedResult('success', true, expected),
42
- printReceivedResult(received)
43
- ].join('\n');
44
- }
45
-
46
- export default {
47
- toSucceedWith: function <T extends Result<unknown>>(
48
- received: T,
49
- expected: ResultValueType<T> | RegExp
50
- ): jest.CustomMatcherResult {
51
- const pass = predicate(received, expected);
52
- if (pass) {
53
- return { pass: true, message: passMessage(received, expected) };
54
- }
55
-
56
- return { pass: false, message: failMessage(received, expected) };
57
- }
58
- };
@@ -1,14 +0,0 @@
1
- import { equals } from '@jest/expect-utils';
2
- import { Result } from '../../ts-utils';
3
-
4
- export const matcherName: string = 'toSucceedWith';
5
-
6
- export function predicate<T>(received: Result<T>, expected: unknown): boolean {
7
- if (received.isSuccess()) {
8
- if (typeof received.value === 'string' && expected instanceof RegExp) {
9
- return expected.test(received.value);
10
- }
11
- return equals(received.value, expected);
12
- }
13
- return false;
14
- }
@@ -1,41 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`.toSucceedWithDetail reports details when it fails due to a failure result: toFailTestAndMatchSnapshot 1`] = `
4
- "expect(received).toSucceedWithDetail(expected)
5
- Expected:
6
- Success with \\"oops\\"
7
- Detail: \\"\\"detail\\"\\"
8
- Received:
9
- Failure with \\"oops\\"
10
- Detail: \\"\\"detail\\"\\""
11
- `;
12
-
13
- exports[`.toSucceedWithDetail reports details when it fails due to a success result with .not: toFailTestAndMatchSnapshot 1`] = `
14
- "expect(received).not.toSucceedWithDetail(expected)
15
- Expected:
16
- Not success with \\"hello\\"
17
- Detail: \\"\\"detail\\"\\"
18
- Received:
19
- Success with \\"\\"hello\\"\\"
20
- Detail: \\"\\"detail\\"\\""
21
- `;
22
-
23
- exports[`.toSucceedWithDetail reports details when it fails due to success with a non-matching detail: toFailTestAndMatchSnapshot 1`] = `
24
- "expect(received).toSucceedWithDetail(expected)
25
- Expected:
26
- Success with \\"hello\\"
27
- Detail: \\"\\"other detail\\"\\"
28
- Received:
29
- Success with \\"\\"hello\\"\\"
30
- Detail: \\"\\"detail\\"\\""
31
- `;
32
-
33
- exports[`.toSucceedWithDetail reports details when it fails due to success with a non-matching value: toFailTestAndMatchSnapshot 1`] = `
34
- "expect(received).toSucceedWithDetail(expected)
35
- Expected:
36
- Success with \\"goodbye\\"
37
- Detail: \\"\\"detail\\"\\"
38
- Received:
39
- Success with \\"\\"hello\\"\\"
40
- Detail: \\"\\"detail\\"\\""
41
- `;
@@ -1,62 +0,0 @@
1
- import { DetailedResult, ResultDetailType, ResultValueType } from '@fgv/ts-utils';
2
- import { printExpectedDetailedResult, printReceivedDetailedResult } from '../../utils/matcherHelpers';
3
- import { matcherName, predicate } from './predicate';
4
-
5
- import { matcherHint } from 'jest-matcher-utils';
6
-
7
- // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
8
- declare global {
9
- // eslint-disable-next-line @typescript-eslint/no-namespace
10
- namespace jest {
11
- // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars, @typescript-eslint/naming-convention
12
- interface Matchers<R, T> {
13
- /**
14
- * Use .toSucceedWithDetail to verify that a DetailedResult\<T, TD\> is
15
- * a success and that the result value and detail matches the supplied
16
- * values
17
- */
18
- toSucceedWithDetail(expected: ResultValueType<T> | RegExp, detail: ResultDetailType<T> | undefined): R;
19
- }
20
- }
21
- }
22
-
23
- function passMessage<T extends DetailedResult<unknown, unknown>>(
24
- received: T,
25
- expected: ResultValueType<T> | RegExp,
26
- expectedDetail: ResultDetailType<T> | undefined
27
- ): () => string {
28
- return () =>
29
- [
30
- matcherHint(`.not.${matcherName}`),
31
- printExpectedDetailedResult('success', false, expected, expectedDetail),
32
- printReceivedDetailedResult(received)
33
- ].join('\n');
34
- }
35
-
36
- function failMessage<T extends DetailedResult<unknown, unknown>>(
37
- received: T,
38
- expected: ResultValueType<T> | RegExp,
39
- expectedDetail: ResultDetailType<T> | undefined
40
- ): () => string {
41
- return () =>
42
- [
43
- matcherHint(`${matcherName}`),
44
- printExpectedDetailedResult('success', true, expected, expectedDetail),
45
- printReceivedDetailedResult(received)
46
- ].join('\n');
47
- }
48
-
49
- export default {
50
- toSucceedWithDetail: function <T extends DetailedResult<unknown, unknown>>(
51
- received: T,
52
- expected: ResultValueType<T> | RegExp,
53
- detail: ResultDetailType<T> | undefined
54
- ): jest.CustomMatcherResult {
55
- const pass = predicate(received, expected, detail);
56
- if (pass) {
57
- return { pass: true, message: passMessage(received, expected, detail) };
58
- }
59
-
60
- return { pass: false, message: failMessage(received, expected, detail) };
61
- }
62
- };
@@ -1,23 +0,0 @@
1
- import { equals } from '@jest/expect-utils';
2
- import { DetailedResult } from '../../ts-utils';
3
-
4
- export const matcherName: string = 'toSucceedWithDetail';
5
-
6
- export function predicate<T, TD>(
7
- received: DetailedResult<T, TD>,
8
- expected: unknown,
9
- detail: TD | undefined
10
- ): boolean {
11
- if (received.isSuccess()) {
12
- let pass = false;
13
- if (typeof received.value === 'string' && expected instanceof RegExp) {
14
- pass = expected.test(received.value);
15
- } else {
16
- pass = equals(received.value, expected);
17
- }
18
-
19
- pass = pass && equals(received.detail, detail);
20
- return pass;
21
- }
22
- return false;
23
- }
@@ -1,21 +0,0 @@
1
- import * as Resolver from '../utils/snapshotResolver';
2
-
3
- const CLI_SNAPSHOT_FOLDER: string = '__cli__';
4
-
5
- function resolveSnapshotPath(testPath: string, ext: string): string {
6
- return Resolver.resolveSnapshotPath(testPath, ext, CLI_SNAPSHOT_FOLDER);
7
- }
8
-
9
- function resolveTestPath(snapshotPath: string, ext: string): string {
10
- return Resolver.resolveTestPath(snapshotPath, ext);
11
- }
12
-
13
- const testPathForConsistencyCheck: string = Resolver.testPathForConsistencyCheck;
14
-
15
- module.exports = {
16
- resolveSnapshotPath,
17
- resolveTestPath,
18
- testPathForConsistencyCheck
19
- };
20
-
21
- export default module.exports;
@@ -1,21 +0,0 @@
1
- import * as Resolver from '../utils/snapshotResolver';
2
-
3
- const CLI_SNAPSHOT_FOLDER: string = '__ide__';
4
-
5
- function resolveSnapshotPath(testPath: string, ext: string): string {
6
- return Resolver.resolveSnapshotPath(testPath, ext, CLI_SNAPSHOT_FOLDER);
7
- }
8
-
9
- function resolveTestPath(snapshotPath: string, ext: string): string {
10
- return Resolver.resolveTestPath(snapshotPath, ext);
11
- }
12
-
13
- const testPathForConsistencyCheck: string = Resolver.testPathForConsistencyCheck;
14
-
15
- module.exports = {
16
- resolveSnapshotPath,
17
- resolveTestPath,
18
- testPathForConsistencyCheck
19
- };
20
-
21
- export default module.exports;
package/src/ts-utils.ts DELETED
@@ -1 +0,0 @@
1
- export * from '@fgv/ts-utils';
@@ -1,107 +0,0 @@
1
- /// <reference types="jest"/>
2
-
3
- // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
4
- export type Function = (...args: any[]) => any;
5
-
6
- // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
7
- import { ResultDetailType, ResultValueType } from '@fgv/ts-utils';
8
-
9
- /* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
10
- declare global {
11
- // eslint-disable-next-line @typescript-eslint/no-namespace
12
- namespace jest {
13
- // eslint-disable-next-line @typescript-eslint/naming-convention
14
- interface Matchers<R, T> {
15
- /**
16
- * Use .toSucceed to verify that a Result\<T\> is a success
17
- */
18
- toSucceed(): R;
19
-
20
- /**
21
- * Use .toSucceedWith to verify that a Result\<T\> is a success
22
- * and that the result value matches the supplied value
23
- * @param expected -
24
- */
25
- toSucceedWith(expected: ResultValueType<T> | RegExp): R;
26
-
27
- /**
28
- * Use .toSucceedWithDetail to verify that a DetailedResult\<T, TD\> is
29
- * a success and that the result value and detail matches the supplied
30
- * values
31
- * @param expected -
32
- * @param detail -
33
- */
34
- toSucceedWithDetail(expected: ResultValueType<T>, detail: ResultDetailType<T> | undefined): R;
35
-
36
- /**
37
- * Use .toSucceedAndSatisfy to verify that a Result\<T\> is a success
38
- * and that the supplied test function returns true (or void)
39
- * for the resulting value
40
- * @param test -
41
- */
42
- toSucceedAndSatisfy(test: (value: ResultValueType<T>) => boolean | void): R;
43
-
44
- /**
45
- * Use .toSucceedAndMatchInlineSnapshot to verify that a Result\<T\> is a success
46
- * and that the result value matches an inline snapshot
47
- */
48
- toSucceedAndMatchInlineSnapshot(snapshot: string | undefined): R;
49
-
50
- /**
51
- * Use .toSucceedAndMatchSnapshot to verify that a Result\<T\> is a success
52
- * and that the result value matches a stored snapshot
53
- */
54
- toSucceedAndMatchSnapshot(): R;
55
-
56
- /**
57
- * Use .toFail to verify that a Result\<T\> is a failure
58
- */
59
- toFail(): R;
60
-
61
- /**
62
- * Use .toFailWith to verify that a Result\<T\> is a failure
63
- * that matches a supplied string, RegExp or undefined value
64
- * @param message -
65
- */
66
- toFailWith(expected: string | RegExp | undefined): R;
67
-
68
- /**
69
- * Use .toFailWithDetail to verify that a DetailedResult\<T\> is
70
- * a failure that matches both a supplied expected failure value
71
- * (string, RegExp or undefined) and a supplied failure detail.
72
- * @param message -
73
- * @param detail -
74
- */
75
- toFailWithDetail<TDetail>(message: string | RegExp, detail: TDetail): R;
76
- toFailWithDetail(message: string | RegExp | undefined, detail: ResultDetailType<T>): R;
77
-
78
- /**
79
- * Use .toFailTest to test a custom matcher by
80
- * verifying that a test case fails.
81
- */
82
- toFailTest(): R;
83
-
84
- /**
85
- * Use .toFailTestWith to test a custom matcher by
86
- * verifying that a test case fails as expected and
87
- * reports an error matching a stored snapshot.
88
- */
89
- toFailTestAndMatchSnapshot(): R;
90
-
91
- /**
92
- * Use .toFailTestWith to test a custom matcher by
93
- * verifying that a test case fails as expected and
94
- * reports an error matching a supplied value.
95
- * @param expected -
96
- */
97
- toFailTestWith(expected: string | string[] | RegExp): R;
98
-
99
- /**
100
- * Use .toSucceedWith to verify that a Result\<T\> is a success
101
- * and that the result value matches the supplied value
102
- * @param expected -
103
- */
104
- toHaveBeenCalledWithArgumentsMatching(expected: unknown): R;
105
- }
106
- }
107
- }
@@ -1,82 +0,0 @@
1
- /**
2
- * Utility functions for handling ANSI color codes in test output.
3
- *
4
- * @packageDocumentation
5
- */
6
-
7
- /**
8
- * Regular expression to match ANSI color/style escape sequences.
9
- *
10
- * This matches:
11
- * - ESC character (ASCII 27) followed by `[`
12
- * - `[0-9;]*` - Zero or more digits and semicolons (parameters)
13
- * - `[a-zA-Z]` - Single letter command (m for SGR, K for erase, etc.)
14
- *
15
- * Examples of what this matches:
16
- * - `\u001b[31m` - Red foreground
17
- * - `\u001b[39m` - Default foreground
18
- * - `\u001b[2m` - Dim/faint
19
- * - `\u001b[22m` - Normal intensity
20
- *
21
- * Using a pre-compiled regex literal to satisfy ESLint security requirements.
22
- * We use the unicode escape sequence \\u001b instead of \\x1b to avoid control-regex warnings.
23
- */
24
- // eslint-disable-next-line @rushstack/security/no-unsafe-regexp, no-control-regex
25
- const ANSI_COLOR_REGEX: RegExp = /\u001b\[[0-9;]*[a-zA-Z]/g;
26
-
27
- /**
28
- * Strips ANSI color/style escape sequences from a string.
29
- *
30
- * This function removes all ANSI escape sequences commonly used for
31
- * terminal colors and text styling, making the output suitable for
32
- * color-agnostic snapshot testing.
33
- *
34
- * @param text - The text that may contain ANSI escape sequences
35
- * @returns The text with all ANSI escape sequences removed
36
- *
37
- * @example
38
- * ```typescript
39
- * const coloredText = '\u001b[31mError:\u001b[39m Something failed';
40
- * const plainText = stripAnsiColors(coloredText);
41
- * console.log(plainText); // "Error: Something failed"
42
- * ```
43
- *
44
- * @example
45
- * ```typescript
46
- * // Common usage in Jest matchers
47
- * const formattedOutput = matcherUtils.printReceived(value);
48
- * const cleanOutput = stripAnsiColors(formattedOutput);
49
- * expect(cleanOutput).toMatchSnapshot();
50
- * ```
51
- *
52
- * @public
53
- */
54
- export function stripAnsiColors(text: string): string {
55
- return text.replace(ANSI_COLOR_REGEX, '');
56
- }
57
-
58
- /**
59
- * Creates a wrapper function that strips ANSI colors from any string-returning function.
60
- *
61
- * This is useful for wrapping Jest matcher utility functions to make them
62
- * color-agnostic for snapshot testing.
63
- *
64
- * @param fn - A function that returns a string (potentially with ANSI colors)
65
- * @returns A wrapped function that returns the same string with colors stripped
66
- *
67
- * @example
68
- * ```typescript
69
- * import { printReceived } from 'jest-matcher-utils';
70
- *
71
- * const printReceivedClean = createColorStripWrapper(printReceived);
72
- * const output = printReceivedClean(someValue); // No colors
73
- * ```
74
- *
75
- * @public
76
- */
77
- export function createColorStripWrapper<T extends (...args: unknown[]) => string>(fn: T): T {
78
- return ((...args: Parameters<T>) => {
79
- const result = fn(...args);
80
- return stripAnsiColors(result);
81
- }) as T;
82
- }
@@ -1,64 +0,0 @@
1
- import { printExpected, printReceived } from 'jest-matcher-utils';
2
- import { DetailedResult, Result } from '../ts-utils';
3
- import { createColorStripWrapper } from './colorHelpers';
4
-
5
- // Create color-stripped versions of Jest matcher utilities
6
- const printExpectedClean: typeof printExpected = createColorStripWrapper(printExpected);
7
- const printReceivedClean: typeof printReceived = createColorStripWrapper(printReceived);
8
-
9
- function printExpectedValue<T>(outcome: string, expected?: T): string {
10
- return expected !== undefined ? ` ${outcome} with ${printExpectedClean(expected)}` : ` ${outcome}`;
11
- }
12
-
13
- export function printExpectedResult<T>(expect: 'success' | 'failure', isNot: boolean, expected?: T): string {
14
- return [
15
- 'Expected:',
16
- isNot
17
- ? expect === 'success'
18
- ? printExpectedValue('Success', expected)
19
- : printExpectedValue('Failure', expected)
20
- : expect === 'success'
21
- ? printExpectedValue('Not success', expected)
22
- : printExpectedValue('Not failure', expected)
23
- ].join('\n');
24
- }
25
-
26
- export function printExpectedDetailedResult<T, TD>(
27
- expect: 'success' | 'failure',
28
- isNot: boolean,
29
- expectedMessage?: T,
30
- expectedDetail?: TD
31
- ): string {
32
- /* c8 ignore next */
33
- return [
34
- 'Expected:',
35
- isNot
36
- ? expect === 'success'
37
- ? printExpectedValue('Success', expectedMessage)
38
- : printExpectedValue('Failure', expectedMessage)
39
- : expect === 'success'
40
- ? printExpectedValue('Not success', expectedMessage)
41
- : printExpectedValue('Not failure', expectedMessage),
42
- ` Detail: "${printExpectedClean(expectedDetail)}"`
43
- ].join('\n');
44
- }
45
-
46
- export function printReceivedResult<T>(received: Result<T>): string {
47
- return [
48
- 'Received:',
49
- received.isSuccess()
50
- ? ` Success with ${printReceivedClean(received.value)}`
51
- : ` Failure with "${received.message}"`
52
- ].join('\n');
53
- }
54
-
55
- export function printReceivedDetailedResult<T, TD>(received: DetailedResult<T, TD>): string {
56
- return [
57
- 'Received:',
58
- received.isSuccess()
59
- ? ` Success with "${printReceivedClean(received.value)}"\n Detail: "${printReceivedClean(
60
- received.detail
61
- )}"`
62
- : ` Failure with "${received.message}"\n Detail: "${printReceivedClean(received.detail)}"`
63
- ].join('\n');
64
- }
@@ -1,28 +0,0 @@
1
- import path from 'path';
2
-
3
- export function resolveSnapshotPath(
4
- testPath: string,
5
- snapshotExtension: string,
6
- snapshotFolderName: string
7
- ): string {
8
- const snapshotPath = path.join(
9
- path.join(path.dirname(testPath), '__snapshots__', snapshotFolderName),
10
- path.basename(testPath) + snapshotExtension
11
- );
12
- return snapshotPath;
13
- }
14
-
15
- export function resolveTestPath(snapshotFilePath: string, snapshotExtension: string): string {
16
- const testPath = path.join(
17
- path.dirname(path.dirname(path.dirname(snapshotFilePath))),
18
- path.basename(snapshotFilePath, snapshotExtension)
19
- );
20
- return testPath;
21
- }
22
-
23
- export const testPathForConsistencyCheck: string = path.posix.join(
24
- 'consistency_check',
25
- '__tests__',
26
- 'subdir',
27
- 'example.test.js'
28
- );