@fgv/ts-utils-jest 5.0.0-2 → 5.0.0-21
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/.vscode/launch.json +16 -0
- package/README.md +11 -0
- package/config/api-extractor.json +343 -0
- package/config/jest-disable-colors.js +34 -0
- package/config/rig.json +16 -0
- package/lib/helpers/fsHelpers.test.d.ts +2 -0
- package/lib/index.test.d.ts +2 -0
- package/lib/matchers/toFail/index.test.d.ts +2 -0
- package/lib/matchers/toFail/predicate.test.d.ts +2 -0
- package/lib/matchers/toFailTest/index.test.d.ts +2 -0
- package/lib/matchers/toFailTest/predicate.test.d.ts +2 -0
- package/lib/matchers/toFailTestAndMatchSnapshot/index.js +2 -1
- package/lib/matchers/toFailTestAndMatchSnapshot/index.test.d.ts +2 -0
- package/lib/matchers/toFailTestAndMatchSnapshot/predicate.test.d.ts +2 -0
- package/lib/matchers/toFailTestWith/index.test.d.ts +2 -0
- package/lib/matchers/toFailTestWith/predicate.test.d.ts +2 -0
- package/lib/matchers/toFailWith/index.test.d.ts +2 -0
- package/lib/matchers/toFailWith/predicate.test.d.ts +2 -0
- package/lib/matchers/toFailWithDetail/index.test.d.ts +2 -0
- package/lib/matchers/toFailWithDetail/predicate.test.d.ts +2 -0
- package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/index.test.d.ts +2 -0
- package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.test.d.ts +2 -0
- package/lib/matchers/toSucceed/index.test.d.ts +2 -0
- package/lib/matchers/toSucceed/predicate.test.d.ts +2 -0
- package/lib/matchers/toSucceedAndMatchInlineSnapshot/index.test.d.ts +2 -0
- package/lib/matchers/toSucceedAndMatchSnapshot/index.test.d.ts +2 -0
- package/lib/matchers/toSucceedAndSatisfy/index.test.d.ts +2 -0
- package/lib/matchers/toSucceedAndSatisfy/predicate.test.d.ts +2 -0
- package/lib/matchers/toSucceedWith/index.test.d.ts +2 -0
- package/lib/matchers/toSucceedWith/predicate.test.d.ts +2 -0
- package/lib/matchers/toSucceedWithDetail/index.test.d.ts +2 -0
- package/lib/matchers/toSucceedWithDetail/predicate.test.d.ts +2 -0
- package/lib/resolvers/resolvers.test.d.ts +2 -0
- package/lib/tsdoc-metadata.json +1 -1
- package/lib/utils/colorHelpers.d.ts +54 -0
- package/lib/utils/colorHelpers.js +84 -0
- package/lib/utils/matcherHelpers.js +9 -5
- package/package.json +8 -8
- package/src/__snapshots__/index.test.ts.snap +24 -0
- package/src/helpers/fsHelpers.ts +184 -0
- package/src/helpers/index.ts +1 -0
- package/src/index.ts +27 -0
- package/src/matchers/index.ts +29 -0
- package/src/matchers/toFail/__snapshots__/index.test.ts.snap +17 -0
- package/src/matchers/toFail/index.ts +45 -0
- package/src/matchers/toFail/predicate.ts +7 -0
- package/src/matchers/toFailTest/__snapshots__/index.test.ts.snap +15 -0
- package/src/matchers/toFailTest/index.ts +48 -0
- package/src/matchers/toFailTest/predicate.ts +8 -0
- package/src/matchers/toFailTestAndMatchSnapshot/__snapshots__/index.test.ts.snap +14 -0
- package/src/matchers/toFailTestAndMatchSnapshot/index.ts +48 -0
- package/src/matchers/toFailTestAndMatchSnapshot/predicate.ts +12 -0
- package/src/matchers/toFailTestWith/__snapshots__/index.test.ts.snap +34 -0
- package/src/matchers/toFailTestWith/index.ts +56 -0
- package/src/matchers/toFailTestWith/predicate.ts +24 -0
- package/src/matchers/toFailWith/__snapshots__/index.test.ts.snap +25 -0
- package/src/matchers/toFailWith/index.ts +52 -0
- package/src/matchers/toFailWith/predicate.ts +16 -0
- package/src/matchers/toFailWithDetail/__snapshots__/index.test.ts.snap +41 -0
- package/src/matchers/toFailWithDetail/index.ts +63 -0
- package/src/matchers/toFailWithDetail/predicate.ts +27 -0
- package/src/matchers/toHaveBeenCalledWithArgumentsMatching/__snapshots__/index.test.ts.snap +82 -0
- package/src/matchers/toHaveBeenCalledWithArgumentsMatching/index.ts +100 -0
- package/src/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.ts +18 -0
- package/src/matchers/toSucceed/__snapshots__/index.test.ts.snap +17 -0
- package/src/matchers/toSucceed/index.ts +45 -0
- package/src/matchers/toSucceed/predicate.ts +7 -0
- package/src/matchers/toSucceedAndMatchInlineSnapshot/__snapshots__/index.test.ts.snap +15 -0
- package/src/matchers/toSucceedAndMatchInlineSnapshot/index.ts +44 -0
- package/src/matchers/toSucceedAndMatchSnapshot/__snapshots__/index.test.ts.snap +27 -0
- package/src/matchers/toSucceedAndMatchSnapshot/index.ts +47 -0
- package/src/matchers/toSucceedAndSatisfy/__snapshots__/index.test.ts.snap +37 -0
- package/src/matchers/toSucceedAndSatisfy/index.ts +78 -0
- package/src/matchers/toSucceedAndSatisfy/predicate.ts +22 -0
- package/src/matchers/toSucceedWith/__snapshots__/index.test.ts.snap +25 -0
- package/src/matchers/toSucceedWith/index.ts +58 -0
- package/src/matchers/toSucceedWith/predicate.ts +14 -0
- package/src/matchers/toSucceedWithDetail/__snapshots__/index.test.ts.snap +41 -0
- package/src/matchers/toSucceedWithDetail/index.ts +62 -0
- package/src/matchers/toSucceedWithDetail/predicate.ts +23 -0
- package/src/resolvers/cli.ts +21 -0
- package/src/resolvers/ide.ts +21 -0
- package/src/ts-utils.ts +1 -0
- package/src/types/index.ts +107 -0
- package/src/utils/colorHelpers.ts +82 -0
- package/src/utils/matcherHelpers.ts +64 -0
- package/src/utils/snapshotResolver.ts +28 -0
- package/test/data/testData.json +4 -0
- package/CHANGELOG.md +0 -105
- package/lib/helpers/fsHelpers.d.ts.map +0 -1
- package/lib/helpers/fsHelpers.js.map +0 -1
- package/lib/helpers/index.d.ts.map +0 -1
- package/lib/helpers/index.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/matchers/index.d.ts.map +0 -1
- package/lib/matchers/index.js.map +0 -1
- package/lib/matchers/toFail/index.d.ts.map +0 -1
- package/lib/matchers/toFail/index.js.map +0 -1
- package/lib/matchers/toFail/predicate.d.ts.map +0 -1
- package/lib/matchers/toFail/predicate.js.map +0 -1
- package/lib/matchers/toFailTest/index.d.ts.map +0 -1
- package/lib/matchers/toFailTest/index.js.map +0 -1
- package/lib/matchers/toFailTest/predicate.d.ts.map +0 -1
- package/lib/matchers/toFailTest/predicate.js.map +0 -1
- package/lib/matchers/toFailTestAndMatchSnapshot/index.d.ts.map +0 -1
- package/lib/matchers/toFailTestAndMatchSnapshot/index.js.map +0 -1
- package/lib/matchers/toFailTestAndMatchSnapshot/predicate.d.ts.map +0 -1
- package/lib/matchers/toFailTestAndMatchSnapshot/predicate.js.map +0 -1
- package/lib/matchers/toFailTestWith/index.d.ts.map +0 -1
- package/lib/matchers/toFailTestWith/index.js.map +0 -1
- package/lib/matchers/toFailTestWith/predicate.d.ts.map +0 -1
- package/lib/matchers/toFailTestWith/predicate.js.map +0 -1
- package/lib/matchers/toFailWith/index.d.ts.map +0 -1
- package/lib/matchers/toFailWith/index.js.map +0 -1
- package/lib/matchers/toFailWith/predicate.d.ts.map +0 -1
- package/lib/matchers/toFailWith/predicate.js.map +0 -1
- package/lib/matchers/toFailWithDetail/index.d.ts.map +0 -1
- package/lib/matchers/toFailWithDetail/index.js.map +0 -1
- package/lib/matchers/toFailWithDetail/predicate.d.ts.map +0 -1
- package/lib/matchers/toFailWithDetail/predicate.js.map +0 -1
- package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/index.d.ts.map +0 -1
- package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/index.js.map +0 -1
- package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.d.ts.map +0 -1
- package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.js.map +0 -1
- package/lib/matchers/toSucceed/index.d.ts.map +0 -1
- package/lib/matchers/toSucceed/index.js.map +0 -1
- package/lib/matchers/toSucceed/predicate.d.ts.map +0 -1
- package/lib/matchers/toSucceed/predicate.js.map +0 -1
- package/lib/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts.map +0 -1
- package/lib/matchers/toSucceedAndMatchInlineSnapshot/index.js.map +0 -1
- package/lib/matchers/toSucceedAndMatchSnapshot/index.d.ts.map +0 -1
- package/lib/matchers/toSucceedAndMatchSnapshot/index.js.map +0 -1
- package/lib/matchers/toSucceedAndSatisfy/index.d.ts.map +0 -1
- package/lib/matchers/toSucceedAndSatisfy/index.js.map +0 -1
- package/lib/matchers/toSucceedAndSatisfy/predicate.d.ts.map +0 -1
- package/lib/matchers/toSucceedAndSatisfy/predicate.js.map +0 -1
- package/lib/matchers/toSucceedWith/index.d.ts.map +0 -1
- package/lib/matchers/toSucceedWith/index.js.map +0 -1
- package/lib/matchers/toSucceedWith/predicate.d.ts.map +0 -1
- package/lib/matchers/toSucceedWith/predicate.js.map +0 -1
- package/lib/matchers/toSucceedWithDetail/index.d.ts.map +0 -1
- package/lib/matchers/toSucceedWithDetail/index.js.map +0 -1
- package/lib/matchers/toSucceedWithDetail/predicate.d.ts.map +0 -1
- package/lib/matchers/toSucceedWithDetail/predicate.js.map +0 -1
- package/lib/resolvers/cli.d.ts.map +0 -1
- package/lib/resolvers/cli.js.map +0 -1
- package/lib/resolvers/ide.d.ts.map +0 -1
- package/lib/resolvers/ide.js.map +0 -1
- package/lib/ts-utils.d.ts.map +0 -1
- package/lib/ts-utils.js.map +0 -1
- package/lib/types/index.d.ts.map +0 -1
- package/lib/types/index.js.map +0 -1
- package/lib/utils/matcherHelpers.d.ts.map +0 -1
- package/lib/utils/matcherHelpers.js.map +0 -1
- package/lib/utils/snapshotResolver.d.ts.map +0 -1
- package/lib/utils/snapshotResolver.js.map +0 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Utility functions for handling ANSI color codes in test output.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.stripAnsiColors = stripAnsiColors;
|
|
9
|
+
exports.createColorStripWrapper = createColorStripWrapper;
|
|
10
|
+
/**
|
|
11
|
+
* Regular expression to match ANSI color/style escape sequences.
|
|
12
|
+
*
|
|
13
|
+
* This matches:
|
|
14
|
+
* - ESC character (ASCII 27) followed by `[`
|
|
15
|
+
* - `[0-9;]*` - Zero or more digits and semicolons (parameters)
|
|
16
|
+
* - `[a-zA-Z]` - Single letter command (m for SGR, K for erase, etc.)
|
|
17
|
+
*
|
|
18
|
+
* Examples of what this matches:
|
|
19
|
+
* - `\u001b[31m` - Red foreground
|
|
20
|
+
* - `\u001b[39m` - Default foreground
|
|
21
|
+
* - `\u001b[2m` - Dim/faint
|
|
22
|
+
* - `\u001b[22m` - Normal intensity
|
|
23
|
+
*
|
|
24
|
+
* Using a pre-compiled regex literal to satisfy ESLint security requirements.
|
|
25
|
+
* We use the unicode escape sequence \\u001b instead of \\x1b to avoid control-regex warnings.
|
|
26
|
+
*/
|
|
27
|
+
// eslint-disable-next-line @rushstack/security/no-unsafe-regexp, no-control-regex
|
|
28
|
+
const ANSI_COLOR_REGEX = /\u001b\[[0-9;]*[a-zA-Z]/g;
|
|
29
|
+
/**
|
|
30
|
+
* Strips ANSI color/style escape sequences from a string.
|
|
31
|
+
*
|
|
32
|
+
* This function removes all ANSI escape sequences commonly used for
|
|
33
|
+
* terminal colors and text styling, making the output suitable for
|
|
34
|
+
* color-agnostic snapshot testing.
|
|
35
|
+
*
|
|
36
|
+
* @param text - The text that may contain ANSI escape sequences
|
|
37
|
+
* @returns The text with all ANSI escape sequences removed
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const coloredText = '\u001b[31mError:\u001b[39m Something failed';
|
|
42
|
+
* const plainText = stripAnsiColors(coloredText);
|
|
43
|
+
* console.log(plainText); // "Error: Something failed"
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* // Common usage in Jest matchers
|
|
49
|
+
* const formattedOutput = matcherUtils.printReceived(value);
|
|
50
|
+
* const cleanOutput = stripAnsiColors(formattedOutput);
|
|
51
|
+
* expect(cleanOutput).toMatchSnapshot();
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
function stripAnsiColors(text) {
|
|
57
|
+
return text.replace(ANSI_COLOR_REGEX, '');
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Creates a wrapper function that strips ANSI colors from any string-returning function.
|
|
61
|
+
*
|
|
62
|
+
* This is useful for wrapping Jest matcher utility functions to make them
|
|
63
|
+
* color-agnostic for snapshot testing.
|
|
64
|
+
*
|
|
65
|
+
* @param fn - A function that returns a string (potentially with ANSI colors)
|
|
66
|
+
* @returns A wrapped function that returns the same string with colors stripped
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* import { printReceived } from 'jest-matcher-utils';
|
|
71
|
+
*
|
|
72
|
+
* const printReceivedClean = createColorStripWrapper(printReceived);
|
|
73
|
+
* const output = printReceivedClean(someValue); // No colors
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @public
|
|
77
|
+
*/
|
|
78
|
+
function createColorStripWrapper(fn) {
|
|
79
|
+
return ((...args) => {
|
|
80
|
+
const result = fn(...args);
|
|
81
|
+
return stripAnsiColors(result);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
//# sourceMappingURL=colorHelpers.js.map
|
|
@@ -5,8 +5,12 @@ exports.printExpectedDetailedResult = printExpectedDetailedResult;
|
|
|
5
5
|
exports.printReceivedResult = printReceivedResult;
|
|
6
6
|
exports.printReceivedDetailedResult = printReceivedDetailedResult;
|
|
7
7
|
const jest_matcher_utils_1 = require("jest-matcher-utils");
|
|
8
|
+
const colorHelpers_1 = require("./colorHelpers");
|
|
9
|
+
// Create color-stripped versions of Jest matcher utilities
|
|
10
|
+
const printExpectedClean = (0, colorHelpers_1.createColorStripWrapper)(jest_matcher_utils_1.printExpected);
|
|
11
|
+
const printReceivedClean = (0, colorHelpers_1.createColorStripWrapper)(jest_matcher_utils_1.printReceived);
|
|
8
12
|
function printExpectedValue(outcome, expected) {
|
|
9
|
-
return expected !== undefined ? ` ${outcome} with ${(
|
|
13
|
+
return expected !== undefined ? ` ${outcome} with ${printExpectedClean(expected)}` : ` ${outcome}`;
|
|
10
14
|
}
|
|
11
15
|
function printExpectedResult(expect, isNot, expected) {
|
|
12
16
|
return [
|
|
@@ -31,14 +35,14 @@ function printExpectedDetailedResult(expect, isNot, expectedMessage, expectedDet
|
|
|
31
35
|
: expect === 'success'
|
|
32
36
|
? printExpectedValue('Not success', expectedMessage)
|
|
33
37
|
: printExpectedValue('Not failure', expectedMessage),
|
|
34
|
-
` Detail: "${(
|
|
38
|
+
` Detail: "${printExpectedClean(expectedDetail)}"`
|
|
35
39
|
].join('\n');
|
|
36
40
|
}
|
|
37
41
|
function printReceivedResult(received) {
|
|
38
42
|
return [
|
|
39
43
|
'Received:',
|
|
40
44
|
received.isSuccess()
|
|
41
|
-
? ` Success with ${(
|
|
45
|
+
? ` Success with ${printReceivedClean(received.value)}`
|
|
42
46
|
: ` Failure with "${received.message}"`
|
|
43
47
|
].join('\n');
|
|
44
48
|
}
|
|
@@ -46,8 +50,8 @@ function printReceivedDetailedResult(received) {
|
|
|
46
50
|
return [
|
|
47
51
|
'Received:',
|
|
48
52
|
received.isSuccess()
|
|
49
|
-
? ` Success with "${(
|
|
50
|
-
: ` Failure with "${received.message}"\n Detail: "${(
|
|
53
|
+
? ` Success with "${printReceivedClean(received.value)}"\n Detail: "${printReceivedClean(received.detail)}"`
|
|
54
|
+
: ` Failure with "${received.message}"\n Detail: "${printReceivedClean(received.detail)}"`
|
|
51
55
|
].join('\n');
|
|
52
56
|
}
|
|
53
57
|
//# sourceMappingURL=matcherHelpers.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-utils-jest",
|
|
3
|
-
"version": "5.0.0-
|
|
3
|
+
"version": "5.0.0-21",
|
|
4
4
|
"description": "Custom matchers for ts-utils result class",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"keywords": [
|
|
@@ -32,20 +32,20 @@
|
|
|
32
32
|
"jest": "^29.7.0",
|
|
33
33
|
"prettier": "^3.6.2",
|
|
34
34
|
"rimraf": "^5.0.7",
|
|
35
|
-
"ts-jest": "^29.4.
|
|
35
|
+
"ts-jest": "^29.4.1",
|
|
36
36
|
"ts-node": "^10.9.2",
|
|
37
|
-
"typescript": "^5.
|
|
38
|
-
"@rushstack/heft": "~0.74.
|
|
37
|
+
"typescript": "^5.8.3",
|
|
38
|
+
"@rushstack/heft": "~0.74.2",
|
|
39
39
|
"@rushstack/eslint-config": "~4.4.0",
|
|
40
|
-
"@rushstack/heft-jest-plugin": "~0.16.
|
|
41
|
-
"@rushstack/heft-node-rig": "~2.9.
|
|
40
|
+
"@rushstack/heft-jest-plugin": "~0.16.11",
|
|
41
|
+
"@rushstack/heft-node-rig": "~2.9.3",
|
|
42
42
|
"@types/heft-jest": "1.0.6",
|
|
43
43
|
"eslint-plugin-n": "^16.6.2",
|
|
44
44
|
"eslint-plugin-tsdoc": "~0.4.0",
|
|
45
|
-
"@fgv/ts-utils": "5.0.0-
|
|
45
|
+
"@fgv/ts-utils": "5.0.0-21"
|
|
46
46
|
},
|
|
47
47
|
"peerDependencies": {
|
|
48
|
-
"@fgv/ts-utils": "5.0.0-
|
|
48
|
+
"@fgv/ts-utils": "5.0.0-21"
|
|
49
49
|
},
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "heft build --clean",
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`verify matchers accessibility toFailTestAndMatchSnapshot exists and works: toFailTestAndMatchSnapshot 1`] = `
|
|
4
|
+
"expect(received).toBe(expected) // Object.is equality
|
|
5
|
+
|
|
6
|
+
Expected: false
|
|
7
|
+
Received: true"
|
|
8
|
+
`;
|
|
9
|
+
|
|
10
|
+
exports[`verify matchers accessibility toSucceedAndMatchSnapshot exists and works: toSucceedAndMatchSnapshot 1`] = `
|
|
11
|
+
Object {
|
|
12
|
+
"child": Object {
|
|
13
|
+
"values": Array [
|
|
14
|
+
Object {
|
|
15
|
+
"number": 1,
|
|
16
|
+
},
|
|
17
|
+
Object {
|
|
18
|
+
"number": 2,
|
|
19
|
+
},
|
|
20
|
+
],
|
|
21
|
+
},
|
|
22
|
+
"field": "field",
|
|
23
|
+
}
|
|
24
|
+
`;
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2020 Erik Fortune
|
|
3
|
+
*
|
|
4
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
* in the Software without restriction, including without limitation the rights
|
|
7
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
* furnished to do so, subject to the following conditions:
|
|
10
|
+
*
|
|
11
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
* copies or substantial portions of the Software.
|
|
13
|
+
*
|
|
14
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
* SOFTWARE.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import fs from 'fs';
|
|
24
|
+
import * as path from 'path';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @public
|
|
28
|
+
*/
|
|
29
|
+
export interface IMockFileConfig {
|
|
30
|
+
path: string;
|
|
31
|
+
backingFile?: string;
|
|
32
|
+
payload?: string;
|
|
33
|
+
writable?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
39
|
+
export class ReadWriteSpies {
|
|
40
|
+
public readonly read: jest.SpyInstance;
|
|
41
|
+
public readonly write: jest.SpyInstance;
|
|
42
|
+
|
|
43
|
+
public constructor(read: jest.SpyInstance, write: jest.SpyInstance) {
|
|
44
|
+
this.read = read;
|
|
45
|
+
this.write = write;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public clear(): void {
|
|
49
|
+
this.read.mockClear();
|
|
50
|
+
this.write.mockClear();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
public restore(): void {
|
|
54
|
+
this.read.mockRestore();
|
|
55
|
+
this.write.mockRestore();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @public
|
|
61
|
+
*/
|
|
62
|
+
export interface IMockFileSystemOptions {
|
|
63
|
+
mockWriteOnly?: boolean;
|
|
64
|
+
allowUnknownMockWrite?: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @public
|
|
69
|
+
*/
|
|
70
|
+
// eslint-disable-next-line @rushstack/no-new-null
|
|
71
|
+
type ReadFunc = (
|
|
72
|
+
path: string | number | Buffer | URL,
|
|
73
|
+
// eslint-disable-next-line @rushstack/no-new-null
|
|
74
|
+
options?: { encoding?: null; flag?: string } | null
|
|
75
|
+
) => Buffer;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @public
|
|
79
|
+
*/
|
|
80
|
+
export class MockFileSystem {
|
|
81
|
+
protected readonly _options?: IMockFileSystemOptions;
|
|
82
|
+
protected readonly _config: Map<string, IMockFileConfig>;
|
|
83
|
+
protected readonly _data: Map<string, string>;
|
|
84
|
+
protected readonly _realReadFileSync: ReadFunc;
|
|
85
|
+
protected readonly _extraWrites: string[] = [];
|
|
86
|
+
|
|
87
|
+
public constructor(configs: Iterable<IMockFileConfig>, options?: IMockFileSystemOptions) {
|
|
88
|
+
this._config = new Map<string, IMockFileConfig>();
|
|
89
|
+
this._data = new Map<string, string>();
|
|
90
|
+
this._options = options;
|
|
91
|
+
this._realReadFileSync = fs.readFileSync;
|
|
92
|
+
|
|
93
|
+
for (const config of configs) {
|
|
94
|
+
const fullPath = path.resolve(config.path);
|
|
95
|
+
this._config.set(fullPath, config);
|
|
96
|
+
if (config.backingFile) {
|
|
97
|
+
this.readMockFileSync(fullPath);
|
|
98
|
+
} else if (config.payload) {
|
|
99
|
+
this._data.set(fullPath, config.payload);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
public readMockFileSync(wanted: string): string {
|
|
105
|
+
const fullPathWanted = path.resolve(wanted);
|
|
106
|
+
if (!this._data.has(fullPathWanted)) {
|
|
107
|
+
const config = this._config.get(fullPathWanted);
|
|
108
|
+
if (config?.backingFile === undefined) {
|
|
109
|
+
if (this._options?.mockWriteOnly !== true) {
|
|
110
|
+
throw new Error(`Mock file not found: ${wanted}`);
|
|
111
|
+
}
|
|
112
|
+
const body = this._realReadFileSync(fullPathWanted).toString();
|
|
113
|
+
this._data.set(fullPathWanted, body);
|
|
114
|
+
} else {
|
|
115
|
+
const fullBackingPath = path.resolve(config.backingFile);
|
|
116
|
+
const body = this._realReadFileSync(fullBackingPath).toString();
|
|
117
|
+
this._data.set(fullPathWanted, body);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const payload = this._data.get(fullPathWanted);
|
|
122
|
+
// not actually reproducible right now
|
|
123
|
+
/* c8 ignore next 3 */
|
|
124
|
+
if (payload === undefined) {
|
|
125
|
+
throw new Error(`Mock file ${wanted} payload is undefined.`);
|
|
126
|
+
}
|
|
127
|
+
return payload;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
public writeMockFileSync(wanted: string, body: string): void {
|
|
131
|
+
const fullPathWanted = path.resolve(wanted);
|
|
132
|
+
const config = this._config.get(fullPathWanted);
|
|
133
|
+
if (config === undefined) {
|
|
134
|
+
if (this._options?.allowUnknownMockWrite !== true) {
|
|
135
|
+
throw new Error(`Mock path not found: ${wanted}`);
|
|
136
|
+
}
|
|
137
|
+
this._extraWrites.push(fullPathWanted);
|
|
138
|
+
} else if (config.writable !== true) {
|
|
139
|
+
throw new Error(`Mock permission denied: ${wanted}`);
|
|
140
|
+
}
|
|
141
|
+
this._data.set(fullPathWanted, body);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public getExtraFilesWritten(): string[] {
|
|
145
|
+
return Array.from(this._extraWrites);
|
|
146
|
+
}
|
|
147
|
+
public tryGetPayload(want: string): string | undefined {
|
|
148
|
+
return this._data.get(path.resolve(want));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
public reset(): void {
|
|
152
|
+
const writable = Array.from(this._config.values()).filter((c) => c.writable === true);
|
|
153
|
+
for (const config of writable) {
|
|
154
|
+
this._data.delete(path.resolve(config.path));
|
|
155
|
+
if (config.backingFile) {
|
|
156
|
+
this.readMockFileSync(path.resolve(config.path));
|
|
157
|
+
} else if (config.payload) {
|
|
158
|
+
this._data.set(path.resolve(config.path), config.payload);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
for (const path of this._extraWrites) {
|
|
163
|
+
this._data.delete(path);
|
|
164
|
+
}
|
|
165
|
+
this._extraWrites.splice(0, this._extraWrites.length);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
public startSpies(): ReadWriteSpies {
|
|
169
|
+
return new ReadWriteSpies(
|
|
170
|
+
jest.spyOn(fs, 'readFileSync').mockImplementation((wanted: unknown) => {
|
|
171
|
+
if (typeof wanted !== 'string') {
|
|
172
|
+
throw new Error('readFileSync mock supports only string parameters');
|
|
173
|
+
}
|
|
174
|
+
return this.readMockFileSync(wanted);
|
|
175
|
+
}),
|
|
176
|
+
jest.spyOn(fs, 'writeFileSync').mockImplementation((wanted: unknown, payload: unknown) => {
|
|
177
|
+
if (typeof wanted !== 'string' || typeof payload !== 'string') {
|
|
178
|
+
throw new Error('writeFileSync mock supports only string parameters');
|
|
179
|
+
}
|
|
180
|
+
return this.writeMockFileSync(wanted, payload);
|
|
181
|
+
})
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './fsHelpers';
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import matchers from './matchers';
|
|
2
|
+
import './types';
|
|
3
|
+
|
|
4
|
+
import * as MockFs from './helpers/fsHelpers';
|
|
5
|
+
export { MockFs };
|
|
6
|
+
|
|
7
|
+
// eslint-disable-next-line no-undef
|
|
8
|
+
type JestGlobal = typeof global & { expect: jest.Expect };
|
|
9
|
+
|
|
10
|
+
// eslint-disable-next-line no-undef
|
|
11
|
+
function isJestGlobal(g: typeof global): g is JestGlobal {
|
|
12
|
+
return g.hasOwnProperty('expect');
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/* c8 ignore else */
|
|
16
|
+
if (isJestGlobal(global)) {
|
|
17
|
+
global.expect.extend(matchers);
|
|
18
|
+
} else {
|
|
19
|
+
/* eslint-disable no-console */
|
|
20
|
+
console.error(
|
|
21
|
+
[
|
|
22
|
+
"Unable to find Jest's global expect",
|
|
23
|
+
'Please check that you have added ts-utils-jest correctly to your jest configuration.'
|
|
24
|
+
].join('\n')
|
|
25
|
+
);
|
|
26
|
+
/* eslint-enable no-console */
|
|
27
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import ToFail from './toFail';
|
|
2
|
+
import ToFailTest from './toFailTest';
|
|
3
|
+
import ToFailTestAndMatchSnapshot from './toFailTestAndMatchSnapshot';
|
|
4
|
+
import ToFailTestWith from './toFailTestWith';
|
|
5
|
+
import ToFailWith from './toFailWith';
|
|
6
|
+
import ToFailWithDetail from './toFailWithDetail';
|
|
7
|
+
import ToHaveBeenCalledWithArgumentsMatching from './toHaveBeenCalledWithArgumentsMatching';
|
|
8
|
+
import ToSucceed from './toSucceed';
|
|
9
|
+
import ToSucceedAndMatchInlineSnapshot from './toSucceedAndMatchInlineSnapshot';
|
|
10
|
+
import ToSucceedAndMatchSnapshot from './toSucceedAndMatchSnapshot';
|
|
11
|
+
import ToSucceedAndSatisfy from './toSucceedAndSatisfy';
|
|
12
|
+
import ToSucceedWith from './toSucceedWith';
|
|
13
|
+
import toSucceedWithDetail from './toSucceedWithDetail';
|
|
14
|
+
|
|
15
|
+
export default {
|
|
16
|
+
...ToFail,
|
|
17
|
+
...ToFailTest,
|
|
18
|
+
...ToFailTestAndMatchSnapshot,
|
|
19
|
+
...ToFailTestWith,
|
|
20
|
+
...ToFailWith,
|
|
21
|
+
...ToFailWithDetail,
|
|
22
|
+
...ToHaveBeenCalledWithArgumentsMatching,
|
|
23
|
+
...ToSucceed,
|
|
24
|
+
...ToSucceedAndMatchInlineSnapshot,
|
|
25
|
+
...ToSucceedAndMatchSnapshot,
|
|
26
|
+
...ToSucceedAndSatisfy,
|
|
27
|
+
...ToSucceedWith,
|
|
28
|
+
...toSucceedWithDetail
|
|
29
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`.toFail reports details for a failed test with .not: toFailTestAndMatchSnapshot 1`] = `
|
|
4
|
+
"expect(received).not.toFail(expected)
|
|
5
|
+
Expected:
|
|
6
|
+
Not failure
|
|
7
|
+
Received:
|
|
8
|
+
Failure with \\"oops\\""
|
|
9
|
+
`;
|
|
10
|
+
|
|
11
|
+
exports[`.toFail reports details for a failed test: toFailTestAndMatchSnapshot 1`] = `
|
|
12
|
+
"expect(received).toFail(expected)
|
|
13
|
+
Expected:
|
|
14
|
+
Failure
|
|
15
|
+
Received:
|
|
16
|
+
Success with \\"hello\\""
|
|
17
|
+
`;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
|
|
4
|
+
import { Result } from '@fgv/ts-utils';
|
|
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 no-unused-vars, @typescript-eslint/no-unused-vars, @typescript-eslint/naming-convention
|
|
11
|
+
interface Matchers<R, T> {
|
|
12
|
+
/**
|
|
13
|
+
* Use .toFail to verify that a Result<T> is a failure
|
|
14
|
+
*/
|
|
15
|
+
toFail(): R;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function passMessage<T extends Result<unknown>>(received: T): () => string {
|
|
21
|
+
return () =>
|
|
22
|
+
[
|
|
23
|
+
matcherHint(`.not.${matcherName}`),
|
|
24
|
+
printExpectedResult('failure', false),
|
|
25
|
+
printReceivedResult(received)
|
|
26
|
+
].join('\n');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function failMessage<T extends Result<unknown>>(received: T): () => string {
|
|
30
|
+
return () =>
|
|
31
|
+
[matcherHint(`${matcherName}`), printExpectedResult('failure', true), printReceivedResult(received)].join(
|
|
32
|
+
'\n'
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default {
|
|
37
|
+
toFail: function <T extends Result<unknown>>(received: T): jest.CustomMatcherResult {
|
|
38
|
+
const pass = predicate(received);
|
|
39
|
+
if (pass) {
|
|
40
|
+
return { pass: true, message: passMessage(received) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return { pass: false, message: failMessage(received) };
|
|
44
|
+
}
|
|
45
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`.toFailTest logs details correctly for a failed .not result: toFailTestAndMatchSnapshot 1`] = `
|
|
4
|
+
"expect(callback).not.toFailTest(expected)
|
|
5
|
+
Expected:
|
|
6
|
+
Not failure
|
|
7
|
+
Received: Test failed"
|
|
8
|
+
`;
|
|
9
|
+
|
|
10
|
+
exports[`.toFailTest logs details correctly for a failed result: toFailTestAndMatchSnapshot 1`] = `
|
|
11
|
+
"expect(callback).toFailTest(expected)
|
|
12
|
+
Expected:
|
|
13
|
+
Failure
|
|
14
|
+
Received: Test passed"
|
|
15
|
+
`;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
|
|
4
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
5
|
+
import { printExpectedResult } from '../../utils/matcherHelpers';
|
|
6
|
+
|
|
7
|
+
declare global {
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
9
|
+
namespace jest {
|
|
10
|
+
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars, @typescript-eslint/naming-convention
|
|
11
|
+
interface Matchers<R, T> {
|
|
12
|
+
/**
|
|
13
|
+
* Use .toFailTest to test a custom matcher by
|
|
14
|
+
* verifying that a test case fails.
|
|
15
|
+
*/
|
|
16
|
+
toFailTest<T>(): R;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function passMessage<T>(): () => string {
|
|
22
|
+
return () =>
|
|
23
|
+
[
|
|
24
|
+
matcherHint(`.not.${matcherName}`, 'callback'),
|
|
25
|
+
printExpectedResult('failure', false),
|
|
26
|
+
' Received: Test failed'
|
|
27
|
+
].join('\n');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function failMessage<T>(): () => string {
|
|
31
|
+
return () =>
|
|
32
|
+
[
|
|
33
|
+
matcherHint(`${matcherName}`, 'callback'),
|
|
34
|
+
printExpectedResult('failure', true),
|
|
35
|
+
' Received: Test passed'
|
|
36
|
+
].join('\n');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default {
|
|
40
|
+
toFailTest: function <T>(cb: () => void): jest.CustomMatcherResult {
|
|
41
|
+
const pass = predicate(cb);
|
|
42
|
+
if (pass) {
|
|
43
|
+
return { pass: true, message: passMessage() };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { pass: false, message: failMessage() };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
|
|
2
|
+
import { captureResult } from '../../ts-utils';
|
|
3
|
+
export const matcherName: string = 'toFailTest';
|
|
4
|
+
|
|
5
|
+
export function predicate<T>(cb: () => void): boolean {
|
|
6
|
+
const cbResult = captureResult(() => cb());
|
|
7
|
+
return cbResult.isFailure();
|
|
8
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
2
|
+
|
|
3
|
+
exports[`.toFailTestAndMatchSnapshot logs details correctly for a failed result: toFailTestAndMatchSnapshot 1`] = `
|
|
4
|
+
"expect(callback).toFailTestAndMatchSnapshot(expected)
|
|
5
|
+
Expected: Callback to fail with an error that matches snapshot
|
|
6
|
+
Received: Callback succeeded"
|
|
7
|
+
`;
|
|
8
|
+
|
|
9
|
+
exports[`.toFailTestAndMatchSnapshot passes for a test that fails with a result matching the snapshot: toFailTestAndMatchSnapshot 1`] = `
|
|
10
|
+
"expect(received).toBe(expected) // Object.is equality
|
|
11
|
+
|
|
12
|
+
Expected: false
|
|
13
|
+
Received: true"
|
|
14
|
+
`;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
|
|
2
|
+
import { Context, toMatchSnapshot } from 'jest-snapshot';
|
|
3
|
+
import { matcherName, predicate } from './predicate';
|
|
4
|
+
|
|
5
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
6
|
+
import { stripAnsiColors } from '../../utils/colorHelpers';
|
|
7
|
+
|
|
8
|
+
declare global {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
10
|
+
namespace jest {
|
|
11
|
+
// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars, @typescript-eslint/naming-convention
|
|
12
|
+
interface Matchers<R, T> {
|
|
13
|
+
/**
|
|
14
|
+
* Use .toFailTestWith to test a custom matcher by
|
|
15
|
+
* verifying that a test case fails as expected and
|
|
16
|
+
* reports an error matching a stored snapshot.
|
|
17
|
+
*/
|
|
18
|
+
toFailTestAndMatchSnapshot<T>(): R;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default {
|
|
24
|
+
toFailTestAndMatchSnapshot: function <T>(
|
|
25
|
+
this: jest.MatcherContext,
|
|
26
|
+
cb: () => void
|
|
27
|
+
): jest.CustomMatcherResult {
|
|
28
|
+
const context = this as unknown as Context;
|
|
29
|
+
const cbResult = predicate(cb);
|
|
30
|
+
if (cbResult.isFailure()) {
|
|
31
|
+
return {
|
|
32
|
+
pass: false,
|
|
33
|
+
message: (): string => {
|
|
34
|
+
return [
|
|
35
|
+
matcherHint(`${matcherName}`, 'callback'),
|
|
36
|
+
' Expected: Callback to fail with an error that matches snapshot',
|
|
37
|
+
' Received: Callback succeeded'
|
|
38
|
+
].join('\n');
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
return toMatchSnapshot.call(
|
|
43
|
+
context,
|
|
44
|
+
stripAnsiColors(cbResult.value),
|
|
45
|
+
'toFailTestAndMatchSnapshot'
|
|
46
|
+
) as jest.CustomMatcherResult;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
|
|
2
|
+
import { Result, captureResult, fail, succeed } from '../../ts-utils';
|
|
3
|
+
|
|
4
|
+
export const matcherName: string = 'toFailTestAndMatchSnapshot';
|
|
5
|
+
|
|
6
|
+
export function predicate<T>(cb: () => void): Result<string> {
|
|
7
|
+
const cbResult = captureResult(() => cb());
|
|
8
|
+
if (cbResult.isFailure()) {
|
|
9
|
+
return succeed(cbResult.message);
|
|
10
|
+
}
|
|
11
|
+
return fail('Callback did not fail');
|
|
12
|
+
}
|