@fgv/ts-utils-jest 5.0.0-22 → 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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgv/ts-utils-jest",
3
- "version": "5.0.0-22",
3
+ "version": "5.0.0-23",
4
4
  "description": "Custom matchers for ts-utils result class",
5
5
  "main": "lib/index.js",
6
6
  "keywords": [
@@ -42,10 +42,10 @@
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-22"
45
+ "@fgv/ts-utils": "5.0.0-23"
46
46
  },
47
47
  "peerDependencies": {
48
- "@fgv/ts-utils": "5.0.0-22"
48
+ "@fgv/ts-utils": "5.0.0-23"
49
49
  },
50
50
  "scripts": {
51
51
  "build": "heft build --clean",
@@ -1,24 +0,0 @@
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
- `;
@@ -1,184 +0,0 @@
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
- }
@@ -1 +0,0 @@
1
- export * from './fsHelpers';
package/src/index.ts DELETED
@@ -1,27 +0,0 @@
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
- }
@@ -1,29 +0,0 @@
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
- };
@@ -1,17 +0,0 @@
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
- `;
@@ -1,45 +0,0 @@
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
- };
@@ -1,7 +0,0 @@
1
- import { Result } from '../../ts-utils';
2
-
3
- export const matcherName: string = 'toFail';
4
-
5
- export function predicate<T>(received: Result<T>): boolean {
6
- return received.isFailure();
7
- }
@@ -1,15 +0,0 @@
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
- `;
@@ -1,48 +0,0 @@
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
- };
@@ -1,8 +0,0 @@
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
- }
@@ -1,14 +0,0 @@
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
- `;
@@ -1,48 +0,0 @@
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
- };
@@ -1,12 +0,0 @@
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
- }
@@ -1,34 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`.toFailTestWith logs failure correctly when callback fails with .not: toFailTestAndMatchSnapshot 1`] = `
4
- "expect(callback).not.toFailTestWith(expectedMessage)
5
- Expected:
6
- Not failure with /expect/i
7
- Received: Callback failed with:
8
- >>>>
9
- expect(received).toBe(expected) // Object.is equality
10
-
11
- Expected: false
12
- Received: true
13
- <<<<"
14
- `;
15
-
16
- exports[`.toFailTestWith logs failure correctly when callback returns an unexpected value: toFailTestAndMatchSnapshot 1`] = `
17
- "expect(callback).toFailTestWith(expectedMessage)
18
- Expected:
19
- Failure with /random text/i
20
- Received: Callback failed with:
21
- >>>>
22
- expect(received).toBe(expected) // Object.is equality
23
-
24
- Expected: \\"goodbye\\"
25
- Received: \\"hello\\"
26
- <<<<"
27
- `;
28
-
29
- exports[`.toFailTestWith logs failure correctly when callback succeeds: toFailTestAndMatchSnapshot 1`] = `
30
- "expect(callback).toFailTestWith(expectedMessage)
31
- Expected:
32
- Failure with \\"whatever\\"
33
- Received: Callback succeeded"
34
- `;
@@ -1,56 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
2
- import { Failure, Success } from '../../ts-utils';
3
- import { matcherName, predicate } from './predicate';
4
-
5
- import { matcherHint } from 'jest-matcher-utils';
6
- import { printExpectedResult } from '../../utils/matcherHelpers';
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 supplied value.
17
- * @param expected -
18
- */
19
- toFailTestWith<T>(expected: string | string[] | RegExp): R;
20
- }
21
- }
22
- }
23
-
24
- function passMessage<T>(cbResult: Success<string>, expected: RegExp | string | string[]): () => string {
25
- return () =>
26
- [
27
- matcherHint(`.not.${matcherName}`, 'callback', 'expectedMessage'),
28
- printExpectedResult('failure', false, expected),
29
- ` Received: Callback failed with:\n>>>>\n${cbResult.value}\n<<<<`
30
- ].join('\n');
31
- }
32
-
33
- function failMessage<T>(cbResult: Failure<string>, expected: RegExp | string | string[]): () => string {
34
- return () =>
35
- [
36
- matcherHint(`${matcherName}`, 'callback', 'expectedMessage'),
37
- printExpectedResult('failure', true, expected),
38
- cbResult.message === ''
39
- ? ' Received: Callback succeeded'
40
- : ` Received: Callback failed with:\n>>>>\n${cbResult.message}\n<<<<`
41
- ].join('\n');
42
- }
43
-
44
- export default {
45
- toFailTestWith: function <T>(
46
- cb: () => void,
47
- expected: RegExp | string | string[]
48
- ): jest.CustomMatcherResult {
49
- const cbResult = predicate(cb, expected);
50
- if (cbResult.isSuccess()) {
51
- return { pass: true, message: passMessage(cbResult, expected) };
52
- }
53
-
54
- return { pass: false, message: failMessage(cbResult, expected) };
55
- }
56
- };
@@ -1,24 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
2
- import { equals } from '@jest/expect-utils';
3
- import { stringify } from 'jest-matcher-utils';
4
- import { Result, captureResult, fail, succeed } from '../../ts-utils';
5
-
6
- export const matcherName: string = 'toFailTestWith';
7
-
8
- export function predicate<T>(cb: () => void, expected: string | string[] | RegExp): Result<string> {
9
- const cbResult = captureResult(() => cb());
10
- if (cbResult.isFailure()) {
11
- let success = false;
12
- if (expected instanceof RegExp) {
13
- success = cbResult.message.match(expected) !== null;
14
- } else if (typeof expected === 'string') {
15
- success = cbResult.message === expected;
16
- } else if (Array.isArray(expected)) {
17
- success = equals(cbResult.message.split('\n'), expected);
18
- } else {
19
- return fail(`Unsupported expected value "${stringify(expected)}" for toFailTestWith`);
20
- }
21
- return success ? succeed(cbResult.message) : fail(cbResult.message);
22
- }
23
- return fail('');
24
- }
@@ -1,25 +0,0 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
2
-
3
- exports[`.toFailWith reports details correctly on a failure due to a success result: toFailTestAndMatchSnapshot 1`] = `
4
- "expect(received).toFailWith(expected)
5
- Expected:
6
- Failure with \\"hello\\"
7
- Received:
8
- Success with \\"hello\\""
9
- `;
10
-
11
- exports[`.toFailWith reports details correctly on a failure due to non-matching failure value: toFailTestAndMatchSnapshot 1`] = `
12
- "expect(received).toFailWith(expected)
13
- Expected:
14
- Failure with \\"error\\"
15
- Received:
16
- Failure with \\"oops\\""
17
- `;
18
-
19
- exports[`.toFailWith reports details correctly on a failure with a .not: toFailTestAndMatchSnapshot 1`] = `
20
- "expect(received).not.toFailWith(expected)
21
- Expected:
22
- Not failure with \\"oops\\"
23
- Received:
24
- Failure with \\"oops\\""
25
- `;