@fgv/ts-utils-jest 5.0.1-9 → 5.0.2-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.
- package/dist/helpers/fsHelpers.js +141 -0
- package/dist/helpers/index.js +2 -0
- package/dist/index.js +18 -0
- package/dist/matchers/index.js +15 -0
- package/dist/matchers/toFail/index.js +23 -0
- package/dist/matchers/toFail/predicate.js +5 -0
- package/dist/matchers/toFailTest/index.js +28 -0
- package/dist/matchers/toFailTest/predicate.js +8 -0
- package/dist/matchers/toFailTestAndMatchSnapshot/index.js +25 -0
- package/dist/matchers/toFailTestAndMatchSnapshot/predicate.js +11 -0
- package/dist/matchers/toFailTestWith/index.js +29 -0
- package/dist/matchers/toFailTestWith/predicate.js +26 -0
- package/dist/matchers/toFailWith/index.js +27 -0
- package/dist/matchers/toFailWith/predicate.js +16 -0
- package/dist/matchers/toFailWithDetail/index.js +27 -0
- package/dist/matchers/toFailWithDetail/predicate.js +22 -0
- package/dist/matchers/toHaveBeenCalledWithArgumentsMatching/index.js +69 -0
- package/dist/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.js +12 -0
- package/dist/matchers/toSucceed/index.js +23 -0
- package/dist/matchers/toSucceed/predicate.js +5 -0
- package/dist/matchers/toSucceedAndMatchInlineSnapshot/index.js +23 -0
- package/dist/matchers/toSucceedAndMatchSnapshot/index.js +23 -0
- package/dist/matchers/toSucceedAndSatisfy/index.js +51 -0
- package/dist/matchers/toSucceedAndSatisfy/predicate.js +17 -0
- package/dist/matchers/toSucceedWith/index.js +27 -0
- package/dist/matchers/toSucceedWith/predicate.js +12 -0
- package/dist/matchers/toSucceedWithDetail/index.js +27 -0
- package/dist/matchers/toSucceedWithDetail/predicate.js +17 -0
- package/dist/resolvers/cli.js +16 -0
- package/dist/resolvers/ide.js +16 -0
- package/dist/ts-utils.js +2 -0
- package/dist/types/index.js +3 -0
- package/dist/utils/colorHelpers.js +80 -0
- package/dist/utils/matcherHelpers.js +51 -0
- package/dist/utils/snapshotResolver.js +11 -0
- package/lib/tsdoc-metadata.json +1 -1
- package/package.json +19 -3
|
@@ -0,0 +1,141 @@
|
|
|
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
|
+
import fs from 'fs';
|
|
23
|
+
import path from 'path';
|
|
24
|
+
/**
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export class ReadWriteSpies {
|
|
28
|
+
constructor(read, write) {
|
|
29
|
+
this.read = read;
|
|
30
|
+
this.write = write;
|
|
31
|
+
}
|
|
32
|
+
clear() {
|
|
33
|
+
this.read.mockClear();
|
|
34
|
+
this.write.mockClear();
|
|
35
|
+
}
|
|
36
|
+
restore() {
|
|
37
|
+
this.read.mockRestore();
|
|
38
|
+
this.write.mockRestore();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
export class MockFileSystem {
|
|
45
|
+
constructor(configs, options) {
|
|
46
|
+
this._extraWrites = [];
|
|
47
|
+
this._config = new Map();
|
|
48
|
+
this._data = new Map();
|
|
49
|
+
this._options = options;
|
|
50
|
+
this._realReadFileSync = fs.readFileSync;
|
|
51
|
+
for (const config of configs) {
|
|
52
|
+
const fullPath = path.resolve(config.path);
|
|
53
|
+
this._config.set(fullPath, config);
|
|
54
|
+
if (config.backingFile) {
|
|
55
|
+
this.readMockFileSync(fullPath);
|
|
56
|
+
}
|
|
57
|
+
else if (config.payload) {
|
|
58
|
+
this._data.set(fullPath, config.payload);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
readMockFileSync(wanted) {
|
|
63
|
+
var _a;
|
|
64
|
+
const fullPathWanted = path.resolve(wanted);
|
|
65
|
+
if (!this._data.has(fullPathWanted)) {
|
|
66
|
+
const config = this._config.get(fullPathWanted);
|
|
67
|
+
if ((config === null || config === void 0 ? void 0 : config.backingFile) === undefined) {
|
|
68
|
+
if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.mockWriteOnly) !== true) {
|
|
69
|
+
throw new Error(`Mock file not found: ${wanted}`);
|
|
70
|
+
}
|
|
71
|
+
const body = this._realReadFileSync(fullPathWanted).toString();
|
|
72
|
+
this._data.set(fullPathWanted, body);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const fullBackingPath = path.resolve(config.backingFile);
|
|
76
|
+
const body = this._realReadFileSync(fullBackingPath).toString();
|
|
77
|
+
this._data.set(fullPathWanted, body);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const payload = this._data.get(fullPathWanted);
|
|
81
|
+
// not actually reproducible right now
|
|
82
|
+
/* c8 ignore next 3 */
|
|
83
|
+
if (payload === undefined) {
|
|
84
|
+
throw new Error(`Mock file ${wanted} payload is undefined.`);
|
|
85
|
+
}
|
|
86
|
+
return payload;
|
|
87
|
+
}
|
|
88
|
+
writeMockFileSync(wanted, body) {
|
|
89
|
+
var _a;
|
|
90
|
+
const fullPathWanted = path.resolve(wanted);
|
|
91
|
+
const config = this._config.get(fullPathWanted);
|
|
92
|
+
if (config === undefined) {
|
|
93
|
+
if (((_a = this._options) === null || _a === void 0 ? void 0 : _a.allowUnknownMockWrite) !== true) {
|
|
94
|
+
throw new Error(`Mock path not found: ${wanted}`);
|
|
95
|
+
}
|
|
96
|
+
this._extraWrites.push(fullPathWanted);
|
|
97
|
+
}
|
|
98
|
+
else if (config.writable !== true) {
|
|
99
|
+
throw new Error(`Mock permission denied: ${wanted}`);
|
|
100
|
+
}
|
|
101
|
+
this._data.set(fullPathWanted, body);
|
|
102
|
+
}
|
|
103
|
+
getExtraFilesWritten() {
|
|
104
|
+
return Array.from(this._extraWrites);
|
|
105
|
+
}
|
|
106
|
+
tryGetPayload(want) {
|
|
107
|
+
return this._data.get(path.resolve(want));
|
|
108
|
+
}
|
|
109
|
+
reset() {
|
|
110
|
+
const writable = Array.from(this._config.values()).filter((c) => c.writable === true);
|
|
111
|
+
for (const config of writable) {
|
|
112
|
+
this._data.delete(path.resolve(config.path));
|
|
113
|
+
if (config.backingFile) {
|
|
114
|
+
this.readMockFileSync(path.resolve(config.path));
|
|
115
|
+
}
|
|
116
|
+
else if (config.payload) {
|
|
117
|
+
this._data.set(path.resolve(config.path), config.payload);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
for (const path of this._extraWrites) {
|
|
121
|
+
this._data.delete(path);
|
|
122
|
+
}
|
|
123
|
+
this._extraWrites.splice(0, this._extraWrites.length);
|
|
124
|
+
}
|
|
125
|
+
startSpies(fsModule) {
|
|
126
|
+
// Dynamic import of fs at runtime to avoid babel instrumentation timing issues
|
|
127
|
+
const fsToMock = fsModule || require('fs');
|
|
128
|
+
return new ReadWriteSpies(jest.spyOn(fsToMock, 'readFileSync').mockImplementation((wanted) => {
|
|
129
|
+
if (typeof wanted !== 'string') {
|
|
130
|
+
throw new Error('readFileSync mock supports only string parameters');
|
|
131
|
+
}
|
|
132
|
+
return this.readMockFileSync(wanted);
|
|
133
|
+
}), jest.spyOn(fsToMock, 'writeFileSync').mockImplementation((wanted, payload) => {
|
|
134
|
+
if (typeof wanted !== 'string' || typeof payload !== 'string') {
|
|
135
|
+
throw new Error('writeFileSync mock supports only string parameters');
|
|
136
|
+
}
|
|
137
|
+
return this.writeMockFileSync(wanted, payload);
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
//# sourceMappingURL=fsHelpers.js.map
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import matchers from './matchers';
|
|
2
|
+
import './types';
|
|
3
|
+
import * as MockFs from './helpers/fsHelpers';
|
|
4
|
+
export { MockFs };
|
|
5
|
+
function isJestGlobal(g) {
|
|
6
|
+
return g.hasOwnProperty('expect');
|
|
7
|
+
}
|
|
8
|
+
/* c8 ignore else */
|
|
9
|
+
if (isJestGlobal(global)) {
|
|
10
|
+
global.expect.extend(matchers);
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
console.error([
|
|
14
|
+
"Unable to find Jest's global expect",
|
|
15
|
+
'Please check that you have added ts-utils-jest correctly to your jest configuration.'
|
|
16
|
+
].join('\n'));
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
export default Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, ToFail), ToFailTest), ToFailTestAndMatchSnapshot), ToFailTestWith), ToFailWith), ToFailWithDetail), ToHaveBeenCalledWithArgumentsMatching), ToSucceed), ToSucceedAndMatchInlineSnapshot), ToSucceedAndMatchSnapshot), ToSucceedAndSatisfy), ToSucceedWith), toSucceedWithDetail);
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedResult('failure', false),
|
|
8
|
+
printReceivedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received) {
|
|
12
|
+
return () => [matcherHint(`${matcherName}`), printExpectedResult('failure', true), printReceivedResult(received)].join('\n');
|
|
13
|
+
}
|
|
14
|
+
export default {
|
|
15
|
+
toFail: function (received) {
|
|
16
|
+
const pass = predicate(received);
|
|
17
|
+
if (pass) {
|
|
18
|
+
return { pass: true, message: passMessage(received) };
|
|
19
|
+
}
|
|
20
|
+
return { pass: false, message: failMessage(received) };
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
import { printExpectedResult } from '../../utils/matcherHelpers';
|
|
5
|
+
function passMessage() {
|
|
6
|
+
return () => [
|
|
7
|
+
matcherHint(`.not.${matcherName}`, 'callback'),
|
|
8
|
+
printExpectedResult('failure', false),
|
|
9
|
+
' Received: Test failed'
|
|
10
|
+
].join('\n');
|
|
11
|
+
}
|
|
12
|
+
function failMessage() {
|
|
13
|
+
return () => [
|
|
14
|
+
matcherHint(`${matcherName}`, 'callback'),
|
|
15
|
+
printExpectedResult('failure', true),
|
|
16
|
+
' Received: Test passed'
|
|
17
|
+
].join('\n');
|
|
18
|
+
}
|
|
19
|
+
export default {
|
|
20
|
+
toFailTest: function (cb) {
|
|
21
|
+
const pass = predicate(cb);
|
|
22
|
+
if (pass) {
|
|
23
|
+
return { pass: true, message: passMessage() };
|
|
24
|
+
}
|
|
25
|
+
return { pass: false, message: failMessage() };
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { captureResult } from '../../ts-utils';
|
|
3
|
+
export const matcherName = 'toFailTest';
|
|
4
|
+
export function predicate(cb) {
|
|
5
|
+
const cbResult = captureResult(() => cb());
|
|
6
|
+
return cbResult.isFailure();
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { toMatchSnapshot } from 'jest-snapshot';
|
|
3
|
+
import { matcherName, predicate } from './predicate';
|
|
4
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
5
|
+
import { stripAnsiColors } from '../../utils/colorHelpers';
|
|
6
|
+
export default {
|
|
7
|
+
toFailTestAndMatchSnapshot: function (cb) {
|
|
8
|
+
const context = this;
|
|
9
|
+
const cbResult = predicate(cb);
|
|
10
|
+
if (cbResult.isFailure()) {
|
|
11
|
+
return {
|
|
12
|
+
pass: false,
|
|
13
|
+
message: () => {
|
|
14
|
+
return [
|
|
15
|
+
matcherHint(`${matcherName}`, 'callback'),
|
|
16
|
+
' Expected: Callback to fail with an error that matches snapshot',
|
|
17
|
+
' Received: Callback succeeded'
|
|
18
|
+
].join('\n');
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return toMatchSnapshot.call(context, stripAnsiColors(cbResult.value), 'toFailTestAndMatchSnapshot');
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { captureResult, fail, succeed } from '../../ts-utils';
|
|
3
|
+
export const matcherName = 'toFailTestAndMatchSnapshot';
|
|
4
|
+
export function predicate(cb) {
|
|
5
|
+
const cbResult = captureResult(() => cb());
|
|
6
|
+
if (cbResult.isFailure()) {
|
|
7
|
+
return succeed(cbResult.message);
|
|
8
|
+
}
|
|
9
|
+
return fail('Callback did not fail');
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { matcherName, predicate } from './predicate';
|
|
2
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
3
|
+
import { printExpectedResult } from '../../utils/matcherHelpers';
|
|
4
|
+
function passMessage(cbResult, expected) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`, 'callback', 'expectedMessage'),
|
|
7
|
+
printExpectedResult('failure', false, expected),
|
|
8
|
+
` Received: Callback failed with:\n>>>>\n${cbResult.value}\n<<<<`
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(cbResult, expected) {
|
|
12
|
+
return () => [
|
|
13
|
+
matcherHint(`${matcherName}`, 'callback', 'expectedMessage'),
|
|
14
|
+
printExpectedResult('failure', true, expected),
|
|
15
|
+
cbResult.message === ''
|
|
16
|
+
? ' Received: Callback succeeded'
|
|
17
|
+
: ` Received: Callback failed with:\n>>>>\n${cbResult.message}\n<<<<`
|
|
18
|
+
].join('\n');
|
|
19
|
+
}
|
|
20
|
+
export default {
|
|
21
|
+
toFailTestWith: function (cb, expected) {
|
|
22
|
+
const cbResult = predicate(cb, expected);
|
|
23
|
+
if (cbResult.isSuccess()) {
|
|
24
|
+
return { pass: true, message: passMessage(cbResult, expected) };
|
|
25
|
+
}
|
|
26
|
+
return { pass: false, message: failMessage(cbResult, expected) };
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import { equals } from '@jest/expect-utils';
|
|
3
|
+
import { stringify } from 'jest-matcher-utils';
|
|
4
|
+
import { captureResult, fail, succeed } from '../../ts-utils';
|
|
5
|
+
export const matcherName = 'toFailTestWith';
|
|
6
|
+
export function predicate(cb, expected) {
|
|
7
|
+
const cbResult = captureResult(() => cb());
|
|
8
|
+
if (cbResult.isFailure()) {
|
|
9
|
+
let success = false;
|
|
10
|
+
if (expected instanceof RegExp) {
|
|
11
|
+
success = cbResult.message.match(expected) !== null;
|
|
12
|
+
}
|
|
13
|
+
else if (typeof expected === 'string') {
|
|
14
|
+
success = cbResult.message === expected;
|
|
15
|
+
}
|
|
16
|
+
else if (Array.isArray(expected)) {
|
|
17
|
+
success = equals(cbResult.message.split('\n'), expected);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
return fail(`Unsupported expected value "${stringify(expected)}" for toFailTestWith`);
|
|
21
|
+
}
|
|
22
|
+
return success ? succeed(cbResult.message) : fail(cbResult.message);
|
|
23
|
+
}
|
|
24
|
+
return fail('');
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received, expected) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedResult('failure', false, expected),
|
|
8
|
+
printReceivedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received, expected) {
|
|
12
|
+
return () => [
|
|
13
|
+
matcherHint(`${matcherName}`),
|
|
14
|
+
printExpectedResult('failure', true, expected),
|
|
15
|
+
printReceivedResult(received)
|
|
16
|
+
].join('\n');
|
|
17
|
+
}
|
|
18
|
+
export default {
|
|
19
|
+
toFailWith: function (received, expected) {
|
|
20
|
+
const pass = predicate(received, expected);
|
|
21
|
+
if (pass) {
|
|
22
|
+
return { pass: true, message: passMessage(received, expected) };
|
|
23
|
+
}
|
|
24
|
+
return { pass: false, message: failMessage(received, expected) };
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const matcherName = 'toFailWith';
|
|
2
|
+
export function predicate(received, expected) {
|
|
3
|
+
if (received.isFailure()) {
|
|
4
|
+
if (expected === undefined) {
|
|
5
|
+
return received.message === undefined;
|
|
6
|
+
}
|
|
7
|
+
else if (expected instanceof RegExp) {
|
|
8
|
+
return received.message.match(expected) !== null;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
return received.message === expected;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { printExpectedDetailedResult, printReceivedDetailedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received, expectedMessage, expectedDetail) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedDetailedResult('failure', false, expectedMessage, expectedDetail),
|
|
8
|
+
printReceivedDetailedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received, expectedMessage, expectedDetail) {
|
|
12
|
+
return () => [
|
|
13
|
+
matcherHint(`${matcherName}`),
|
|
14
|
+
printExpectedDetailedResult('failure', true, expectedMessage, expectedDetail),
|
|
15
|
+
printReceivedDetailedResult(received)
|
|
16
|
+
].join('\n');
|
|
17
|
+
}
|
|
18
|
+
export default {
|
|
19
|
+
toFailWithDetail: function (received, expectedMessage, expectedDetail) {
|
|
20
|
+
const pass = predicate(received, expectedMessage, expectedDetail);
|
|
21
|
+
if (pass) {
|
|
22
|
+
return { pass: true, message: passMessage(received, expectedMessage, expectedDetail) };
|
|
23
|
+
}
|
|
24
|
+
return { pass: false, message: failMessage(received, expectedMessage, expectedDetail) };
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { equals } from '@jest/expect-utils';
|
|
2
|
+
export const matcherName = 'toFailWith';
|
|
3
|
+
export function predicate(received, expectedResult, expectedDetail) {
|
|
4
|
+
if (received.isFailure()) {
|
|
5
|
+
if (expectedResult === undefined) {
|
|
6
|
+
if (received.message !== undefined) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
else if (expectedResult instanceof RegExp) {
|
|
11
|
+
if (received.message.match(expectedResult) === null) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
else if (received.message !== expectedResult) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
return equals(received.detail, expectedDetail);
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { getTypeOfProperty, getValueOfPropertyOrDefault } from '@fgv/ts-utils';
|
|
2
|
+
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
|
|
3
|
+
import { matcherName, predicate } from './predicate';
|
|
4
|
+
function getRange(length, cursor) {
|
|
5
|
+
// less than 3 return everything
|
|
6
|
+
if (length < 3) {
|
|
7
|
+
return { start: 0, end: length };
|
|
8
|
+
}
|
|
9
|
+
if (cursor === 0) {
|
|
10
|
+
return { start: 0, end: 3 };
|
|
11
|
+
}
|
|
12
|
+
else if (cursor >= length - 1) {
|
|
13
|
+
return { start: length - 3, end: length };
|
|
14
|
+
}
|
|
15
|
+
return { start: cursor - 1, end: cursor + 2 };
|
|
16
|
+
}
|
|
17
|
+
function formatOneCall(index, received, cursor) {
|
|
18
|
+
const indexString = index.toLocaleString([], { maximumFractionDigits: 0, minimumIntegerDigits: 3 });
|
|
19
|
+
const cursorString = cursor ? '*' : ' ';
|
|
20
|
+
return `${cursorString}${indexString}: ${printReceived(received)}`;
|
|
21
|
+
}
|
|
22
|
+
function formatArgsMessage(received, cursor) {
|
|
23
|
+
const calls = received.mock.calls;
|
|
24
|
+
if (calls.length > 0) {
|
|
25
|
+
// if there's no cursor, show the last 3
|
|
26
|
+
const { start, end } = getRange(calls.length, cursor !== undefined ? cursor : calls.length - 1);
|
|
27
|
+
const callsToShow = calls.slice(start, end);
|
|
28
|
+
return callsToShow.map((c, i) => formatOneCall(start + i, c, start + i === cursor));
|
|
29
|
+
}
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
function passMessage(received, expected, matched) {
|
|
33
|
+
return () => [
|
|
34
|
+
matcherHint(`.not.${matcherName}`),
|
|
35
|
+
'Expected no call with arguments matching:',
|
|
36
|
+
` ${printExpected(expected)}`,
|
|
37
|
+
`Received (${received.mock.calls.length} total):`,
|
|
38
|
+
...formatArgsMessage(received, matched.index)
|
|
39
|
+
].join('\n');
|
|
40
|
+
}
|
|
41
|
+
function failMessage(received, expected) {
|
|
42
|
+
return () => [
|
|
43
|
+
matcherHint(`${matcherName}`),
|
|
44
|
+
'Expected call with arguments matching:',
|
|
45
|
+
` ${printExpected(expected)}`,
|
|
46
|
+
`Received (${received.mock.calls.length} total):`,
|
|
47
|
+
...formatArgsMessage(received)
|
|
48
|
+
].join('\n');
|
|
49
|
+
}
|
|
50
|
+
function isMock(received) {
|
|
51
|
+
if (received !== null && !Array.isArray(received)) {
|
|
52
|
+
return (getValueOfPropertyOrDefault('_isMockFunction', received) === true &&
|
|
53
|
+
getTypeOfProperty('mock', received) === 'object');
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
export default {
|
|
58
|
+
toHaveBeenCalledWithArgumentsMatching: function (received, expected) {
|
|
59
|
+
if (!isMock(received)) {
|
|
60
|
+
throw new Error('Test error: toHaveBeenCalledWithArgumentsMatching called with other than jest.Mock');
|
|
61
|
+
}
|
|
62
|
+
const matched = predicate(received, expected);
|
|
63
|
+
if (matched !== undefined) {
|
|
64
|
+
return { pass: true, message: passMessage(received, expected, matched) };
|
|
65
|
+
}
|
|
66
|
+
return { pass: false, message: failMessage(received, expected) };
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { equals } from '@jest/expect-utils';
|
|
2
|
+
export const matcherName = 'toHaveBeenCalledWithArgumentsMatching';
|
|
3
|
+
export function predicate(received, expected) {
|
|
4
|
+
for (let i = 0; i < received.mock.calls.length; i++) {
|
|
5
|
+
const args = received.mock.calls[i];
|
|
6
|
+
if (equals(args, expected)) {
|
|
7
|
+
return { index: i, arguments: args };
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedResult('success', false),
|
|
8
|
+
printReceivedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received) {
|
|
12
|
+
return () => [matcherHint(`${matcherName}`), printExpectedResult('success', true), printReceivedResult(received)].join('\n');
|
|
13
|
+
}
|
|
14
|
+
export default {
|
|
15
|
+
toSucceed: function (received) {
|
|
16
|
+
const pass = predicate(received);
|
|
17
|
+
if (pass) {
|
|
18
|
+
return { pass: true, message: passMessage(received) };
|
|
19
|
+
}
|
|
20
|
+
return { pass: false, message: failMessage(received) };
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
2
|
+
import { toMatchInlineSnapshot } from 'jest-snapshot';
|
|
3
|
+
import { printReceivedResult } from '../../utils/matcherHelpers';
|
|
4
|
+
const matcherName = 'toSucceedAndMatchInlineSnapshot';
|
|
5
|
+
export default {
|
|
6
|
+
toSucceedAndMatchInlineSnapshot: function (received, snapshot) {
|
|
7
|
+
const context = this;
|
|
8
|
+
if (received.isFailure()) {
|
|
9
|
+
return {
|
|
10
|
+
pass: false,
|
|
11
|
+
message: () => {
|
|
12
|
+
return [
|
|
13
|
+
matcherHint(`${matcherName}`, 'callback'),
|
|
14
|
+
'Expected:\n Callback to succeed with a result that matches the snapshot',
|
|
15
|
+
printReceivedResult(received)
|
|
16
|
+
].join('\n');
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return toMatchInlineSnapshot.call(context, received.value, {}, snapshot);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
2
|
+
import { toMatchSnapshot } from 'jest-snapshot';
|
|
3
|
+
import { printReceivedResult } from '../../utils/matcherHelpers';
|
|
4
|
+
const matcherName = 'toSucceedAndMatchSnapshot';
|
|
5
|
+
export default {
|
|
6
|
+
toSucceedAndMatchSnapshot: function (received) {
|
|
7
|
+
const context = this;
|
|
8
|
+
if (received.isFailure()) {
|
|
9
|
+
return {
|
|
10
|
+
pass: false,
|
|
11
|
+
message: () => {
|
|
12
|
+
return [
|
|
13
|
+
matcherHint(`${matcherName}`, 'callback'),
|
|
14
|
+
'Expected:\n Callback to succeed with a result that matches the snapshot',
|
|
15
|
+
printReceivedResult(received)
|
|
16
|
+
].join('\n');
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
return toMatchSnapshot.call(context, received.value, 'toSucceedAndMatchSnapshot');
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received) {
|
|
5
|
+
const expected = 'successful callback';
|
|
6
|
+
const got = [printReceivedResult(received)];
|
|
7
|
+
got.push(' Callback returned true');
|
|
8
|
+
return () => [
|
|
9
|
+
matcherHint(`.not.${matcherName}`, 'result', 'callback'),
|
|
10
|
+
printExpectedResult('success', false, expected),
|
|
11
|
+
...got
|
|
12
|
+
].join('\n');
|
|
13
|
+
}
|
|
14
|
+
function failMessage(received, cbResult) {
|
|
15
|
+
const expected = 'successful callback';
|
|
16
|
+
const got = [printReceivedResult(received)];
|
|
17
|
+
/* c8 ignore else */
|
|
18
|
+
if (cbResult.isFailure()) {
|
|
19
|
+
got.push(cbResult.message);
|
|
20
|
+
}
|
|
21
|
+
else if (cbResult.value === false) {
|
|
22
|
+
got.push(' Callback returned false');
|
|
23
|
+
}
|
|
24
|
+
else if (cbResult.value === undefined) {
|
|
25
|
+
got.push(' Callback was not invoked');
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
throw new Error('Internal error: toSucceedAndSatisfy.failMessage passed success with true');
|
|
29
|
+
}
|
|
30
|
+
return () => [
|
|
31
|
+
matcherHint(`${matcherName}`, 'result', 'callback'),
|
|
32
|
+
printExpectedResult('success', true, expected),
|
|
33
|
+
...got
|
|
34
|
+
].join('\n');
|
|
35
|
+
}
|
|
36
|
+
export default {
|
|
37
|
+
toSucceedAndSatisfy: function (received, test) {
|
|
38
|
+
// For the normal (not '.not') case, we do not want to capture exceptions
|
|
39
|
+
// so that the IDE can display exactly the line on which the failure case.
|
|
40
|
+
// For the .not case, we want to swallow exceptions or expect failures since
|
|
41
|
+
// we're just testing failure and not the reason.
|
|
42
|
+
const capture = this.isNot;
|
|
43
|
+
const cbResult = predicate(received, test, !!capture);
|
|
44
|
+
const pass = cbResult.isSuccess() && cbResult.value === true;
|
|
45
|
+
if (pass) {
|
|
46
|
+
return { pass: true, message: passMessage(received) };
|
|
47
|
+
}
|
|
48
|
+
return { pass: false, message: failMessage(received, cbResult) };
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { captureResult, fail, succeed } from '../../ts-utils';
|
|
2
|
+
export const matcherName = 'toSucceedAndSatisfy';
|
|
3
|
+
// If the result is successful and callback does not throw, returns Success with the callback return value, or
|
|
4
|
+
// true if the callback returns undefined
|
|
5
|
+
// If the result is successful but the callback throws, returns Failure with the thrown error
|
|
6
|
+
// If the result is failure, returns Success with undefined
|
|
7
|
+
export function predicate(received, cb, capture) {
|
|
8
|
+
if (received.isSuccess()) {
|
|
9
|
+
const cbResult = capture ? captureResult(() => cb(received.value)) : succeed(cb(received.value));
|
|
10
|
+
if (cbResult.isSuccess()) {
|
|
11
|
+
return succeed(cbResult.value === true || cbResult.value === undefined);
|
|
12
|
+
}
|
|
13
|
+
return fail(` Callback failed with:\n ${cbResult.message}`);
|
|
14
|
+
}
|
|
15
|
+
return succeed(undefined);
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received, expected) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedResult('success', false, expected),
|
|
8
|
+
printReceivedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received, expected) {
|
|
12
|
+
return () => [
|
|
13
|
+
matcherHint(`${matcherName}`),
|
|
14
|
+
printExpectedResult('success', true, expected),
|
|
15
|
+
printReceivedResult(received)
|
|
16
|
+
].join('\n');
|
|
17
|
+
}
|
|
18
|
+
export default {
|
|
19
|
+
toSucceedWith: function (received, expected) {
|
|
20
|
+
const pass = predicate(received, expected);
|
|
21
|
+
if (pass) {
|
|
22
|
+
return { pass: true, message: passMessage(received, expected) };
|
|
23
|
+
}
|
|
24
|
+
return { pass: false, message: failMessage(received, expected) };
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { equals } from '@jest/expect-utils';
|
|
2
|
+
export const matcherName = 'toSucceedWith';
|
|
3
|
+
export function predicate(received, expected) {
|
|
4
|
+
if (received.isSuccess()) {
|
|
5
|
+
if (typeof received.value === 'string' && expected instanceof RegExp) {
|
|
6
|
+
return expected.test(received.value);
|
|
7
|
+
}
|
|
8
|
+
return equals(received.value, expected);
|
|
9
|
+
}
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { printExpectedDetailedResult, printReceivedDetailedResult } from '../../utils/matcherHelpers';
|
|
2
|
+
import { matcherName, predicate } from './predicate';
|
|
3
|
+
import { matcherHint } from 'jest-matcher-utils';
|
|
4
|
+
function passMessage(received, expected, expectedDetail) {
|
|
5
|
+
return () => [
|
|
6
|
+
matcherHint(`.not.${matcherName}`),
|
|
7
|
+
printExpectedDetailedResult('success', false, expected, expectedDetail),
|
|
8
|
+
printReceivedDetailedResult(received)
|
|
9
|
+
].join('\n');
|
|
10
|
+
}
|
|
11
|
+
function failMessage(received, expected, expectedDetail) {
|
|
12
|
+
return () => [
|
|
13
|
+
matcherHint(`${matcherName}`),
|
|
14
|
+
printExpectedDetailedResult('success', true, expected, expectedDetail),
|
|
15
|
+
printReceivedDetailedResult(received)
|
|
16
|
+
].join('\n');
|
|
17
|
+
}
|
|
18
|
+
export default {
|
|
19
|
+
toSucceedWithDetail: function (received, expected, detail) {
|
|
20
|
+
const pass = predicate(received, expected, detail);
|
|
21
|
+
if (pass) {
|
|
22
|
+
return { pass: true, message: passMessage(received, expected, detail) };
|
|
23
|
+
}
|
|
24
|
+
return { pass: false, message: failMessage(received, expected, detail) };
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { equals } from '@jest/expect-utils';
|
|
2
|
+
export const matcherName = 'toSucceedWithDetail';
|
|
3
|
+
export function predicate(received, expected, detail) {
|
|
4
|
+
if (received.isSuccess()) {
|
|
5
|
+
let pass = false;
|
|
6
|
+
if (typeof received.value === 'string' && expected instanceof RegExp) {
|
|
7
|
+
pass = expected.test(received.value);
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
pass = equals(received.value, expected);
|
|
11
|
+
}
|
|
12
|
+
pass = pass && equals(received.detail, detail);
|
|
13
|
+
return pass;
|
|
14
|
+
}
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=predicate.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as Resolver from '../utils/snapshotResolver';
|
|
2
|
+
const CLI_SNAPSHOT_FOLDER = '__cli__';
|
|
3
|
+
function resolveSnapshotPath(testPath, ext) {
|
|
4
|
+
return Resolver.resolveSnapshotPath(testPath, ext, CLI_SNAPSHOT_FOLDER);
|
|
5
|
+
}
|
|
6
|
+
function resolveTestPath(snapshotPath, ext) {
|
|
7
|
+
return Resolver.resolveTestPath(snapshotPath, ext);
|
|
8
|
+
}
|
|
9
|
+
const testPathForConsistencyCheck = Resolver.testPathForConsistencyCheck;
|
|
10
|
+
module.exports = {
|
|
11
|
+
resolveSnapshotPath,
|
|
12
|
+
resolveTestPath,
|
|
13
|
+
testPathForConsistencyCheck
|
|
14
|
+
};
|
|
15
|
+
export default module.exports;
|
|
16
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import * as Resolver from '../utils/snapshotResolver';
|
|
2
|
+
const CLI_SNAPSHOT_FOLDER = '__ide__';
|
|
3
|
+
function resolveSnapshotPath(testPath, ext) {
|
|
4
|
+
return Resolver.resolveSnapshotPath(testPath, ext, CLI_SNAPSHOT_FOLDER);
|
|
5
|
+
}
|
|
6
|
+
function resolveTestPath(snapshotPath, ext) {
|
|
7
|
+
return Resolver.resolveTestPath(snapshotPath, ext);
|
|
8
|
+
}
|
|
9
|
+
const testPathForConsistencyCheck = Resolver.testPathForConsistencyCheck;
|
|
10
|
+
module.exports = {
|
|
11
|
+
resolveSnapshotPath,
|
|
12
|
+
resolveTestPath,
|
|
13
|
+
testPathForConsistencyCheck
|
|
14
|
+
};
|
|
15
|
+
export default module.exports;
|
|
16
|
+
//# sourceMappingURL=ide.js.map
|
package/dist/ts-utils.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for handling ANSI color codes in test output.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Regular expression to match ANSI color/style escape sequences.
|
|
8
|
+
*
|
|
9
|
+
* This matches:
|
|
10
|
+
* - ESC character (ASCII 27) followed by `[`
|
|
11
|
+
* - `[0-9;]*` - Zero or more digits and semicolons (parameters)
|
|
12
|
+
* - `[a-zA-Z]` - Single letter command (m for SGR, K for erase, etc.)
|
|
13
|
+
*
|
|
14
|
+
* Examples of what this matches:
|
|
15
|
+
* - `\u001b[31m` - Red foreground
|
|
16
|
+
* - `\u001b[39m` - Default foreground
|
|
17
|
+
* - `\u001b[2m` - Dim/faint
|
|
18
|
+
* - `\u001b[22m` - Normal intensity
|
|
19
|
+
*
|
|
20
|
+
* Using a pre-compiled regex literal to satisfy ESLint security requirements.
|
|
21
|
+
* We use the unicode escape sequence \\u001b instead of \\x1b to avoid control-regex warnings.
|
|
22
|
+
*/
|
|
23
|
+
// eslint-disable-next-line no-control-regex
|
|
24
|
+
const ANSI_COLOR_REGEX = /\u001b\[[0-9;]*[a-zA-Z]/g;
|
|
25
|
+
/**
|
|
26
|
+
* Strips ANSI color/style escape sequences from a string.
|
|
27
|
+
*
|
|
28
|
+
* This function removes all ANSI escape sequences commonly used for
|
|
29
|
+
* terminal colors and text styling, making the output suitable for
|
|
30
|
+
* color-agnostic snapshot testing.
|
|
31
|
+
*
|
|
32
|
+
* @param text - The text that may contain ANSI escape sequences
|
|
33
|
+
* @returns The text with all ANSI escape sequences removed
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```typescript
|
|
37
|
+
* const coloredText = '\u001b[31mError:\u001b[39m Something failed';
|
|
38
|
+
* const plainText = stripAnsiColors(coloredText);
|
|
39
|
+
* console.log(plainText); // "Error: Something failed"
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // Common usage in Jest matchers
|
|
45
|
+
* const formattedOutput = matcherUtils.printReceived(value);
|
|
46
|
+
* const cleanOutput = stripAnsiColors(formattedOutput);
|
|
47
|
+
* expect(cleanOutput).toMatchSnapshot();
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
export function stripAnsiColors(text) {
|
|
53
|
+
return text.replace(ANSI_COLOR_REGEX, '');
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Creates a wrapper function that strips ANSI colors from any string-returning function.
|
|
57
|
+
*
|
|
58
|
+
* This is useful for wrapping Jest matcher utility functions to make them
|
|
59
|
+
* color-agnostic for snapshot testing.
|
|
60
|
+
*
|
|
61
|
+
* @param fn - A function that returns a string (potentially with ANSI colors)
|
|
62
|
+
* @returns A wrapped function that returns the same string with colors stripped
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* import { printReceived } from 'jest-matcher-utils';
|
|
67
|
+
*
|
|
68
|
+
* const printReceivedClean = createColorStripWrapper(printReceived);
|
|
69
|
+
* const output = printReceivedClean(someValue); // No colors
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* @public
|
|
73
|
+
*/
|
|
74
|
+
export function createColorStripWrapper(fn) {
|
|
75
|
+
return ((...args) => {
|
|
76
|
+
const result = fn(...args);
|
|
77
|
+
return stripAnsiColors(result);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=colorHelpers.js.map
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { printExpected, printReceived } from 'jest-matcher-utils';
|
|
2
|
+
import { createColorStripWrapper } from './colorHelpers';
|
|
3
|
+
// Create color-stripped versions of Jest matcher utilities
|
|
4
|
+
const printExpectedClean = createColorStripWrapper(printExpected);
|
|
5
|
+
const printReceivedClean = createColorStripWrapper(printReceived);
|
|
6
|
+
function printExpectedValue(outcome, expected) {
|
|
7
|
+
return expected !== undefined ? ` ${outcome} with ${printExpectedClean(expected)}` : ` ${outcome}`;
|
|
8
|
+
}
|
|
9
|
+
export function printExpectedResult(expect, isNot, expected) {
|
|
10
|
+
return [
|
|
11
|
+
'Expected:',
|
|
12
|
+
isNot
|
|
13
|
+
? expect === 'success'
|
|
14
|
+
? printExpectedValue('Success', expected)
|
|
15
|
+
: printExpectedValue('Failure', expected)
|
|
16
|
+
: expect === 'success'
|
|
17
|
+
? printExpectedValue('Not success', expected)
|
|
18
|
+
: printExpectedValue('Not failure', expected)
|
|
19
|
+
].join('\n');
|
|
20
|
+
}
|
|
21
|
+
export function printExpectedDetailedResult(expect, isNot, expectedMessage, expectedDetail) {
|
|
22
|
+
/* c8 ignore next */
|
|
23
|
+
return [
|
|
24
|
+
'Expected:',
|
|
25
|
+
isNot
|
|
26
|
+
? expect === 'success'
|
|
27
|
+
? printExpectedValue('Success', expectedMessage)
|
|
28
|
+
: printExpectedValue('Failure', expectedMessage)
|
|
29
|
+
: expect === 'success'
|
|
30
|
+
? printExpectedValue('Not success', expectedMessage)
|
|
31
|
+
: printExpectedValue('Not failure', expectedMessage),
|
|
32
|
+
` Detail: "${printExpectedClean(expectedDetail)}"`
|
|
33
|
+
].join('\n');
|
|
34
|
+
}
|
|
35
|
+
export function printReceivedResult(received) {
|
|
36
|
+
return [
|
|
37
|
+
'Received:',
|
|
38
|
+
received.isSuccess()
|
|
39
|
+
? ` Success with ${printReceivedClean(received.value)}`
|
|
40
|
+
: ` Failure with "${received.message}"`
|
|
41
|
+
].join('\n');
|
|
42
|
+
}
|
|
43
|
+
export function printReceivedDetailedResult(received) {
|
|
44
|
+
return [
|
|
45
|
+
'Received:',
|
|
46
|
+
received.isSuccess()
|
|
47
|
+
? ` Success with "${printReceivedClean(received.value)}"\n Detail: "${printReceivedClean(received.detail)}"`
|
|
48
|
+
: ` Failure with "${received.message}"\n Detail: "${printReceivedClean(received.detail)}"`
|
|
49
|
+
].join('\n');
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=matcherHelpers.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
export function resolveSnapshotPath(testPath, snapshotExtension, snapshotFolderName) {
|
|
3
|
+
const snapshotPath = path.join(path.join(path.dirname(testPath), '__snapshots__', snapshotFolderName), path.basename(testPath) + snapshotExtension);
|
|
4
|
+
return snapshotPath;
|
|
5
|
+
}
|
|
6
|
+
export function resolveTestPath(snapshotFilePath, snapshotExtension) {
|
|
7
|
+
const testPath = path.join(path.dirname(path.dirname(path.dirname(snapshotFilePath))), path.basename(snapshotFilePath, snapshotExtension));
|
|
8
|
+
return testPath;
|
|
9
|
+
}
|
|
10
|
+
export const testPathForConsistencyCheck = path.posix.join('consistency_check', '__tests__', 'subdir', 'example.test.js');
|
|
11
|
+
//# sourceMappingURL=snapshotResolver.js.map
|
package/lib/tsdoc-metadata.json
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgv/ts-utils-jest",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.2-0",
|
|
4
4
|
"description": "Custom matchers for ts-utils result class",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "lib/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./lib/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"require": "./lib/index.js",
|
|
13
|
+
"default": "./lib/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
6
16
|
"keywords": [
|
|
7
17
|
"typescript",
|
|
8
18
|
"ts-utils",
|
|
@@ -42,10 +52,16 @@
|
|
|
42
52
|
"@types/heft-jest": "1.0.6",
|
|
43
53
|
"eslint-plugin-n": "^17.23.1",
|
|
44
54
|
"eslint-plugin-tsdoc": "~0.4.0",
|
|
45
|
-
"@
|
|
55
|
+
"@microsoft/api-extractor": "^7.53.3",
|
|
56
|
+
"@fgv/ts-utils": "5.0.2-0",
|
|
57
|
+
"@fgv/heft-dual-rig": "0.1.0"
|
|
46
58
|
},
|
|
47
59
|
"peerDependencies": {
|
|
48
|
-
"@fgv/ts-utils": "5.0.
|
|
60
|
+
"@fgv/ts-utils": "5.0.2-0"
|
|
61
|
+
},
|
|
62
|
+
"repository": {
|
|
63
|
+
"type": "git",
|
|
64
|
+
"url": "https://github.com/ErikFortune/fgv.git"
|
|
49
65
|
},
|
|
50
66
|
"scripts": {
|
|
51
67
|
"build": "heft build --clean",
|