@fgv/ts-utils-jest 5.0.0-11 → 5.0.0-14

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 (150) hide show
  1. package/.vscode/launch.json +16 -0
  2. package/config/api-extractor.json +343 -0
  3. package/config/rig.json +16 -0
  4. package/lib/helpers/fsHelpers.test.d.ts +2 -0
  5. package/lib/index.test.d.ts +2 -0
  6. package/lib/matchers/toFail/index.test.d.ts +2 -0
  7. package/lib/matchers/toFail/predicate.test.d.ts +2 -0
  8. package/lib/matchers/toFailTest/index.test.d.ts +2 -0
  9. package/lib/matchers/toFailTest/predicate.test.d.ts +2 -0
  10. package/lib/matchers/toFailTestAndMatchSnapshot/index.test.d.ts +2 -0
  11. package/lib/matchers/toFailTestAndMatchSnapshot/predicate.test.d.ts +2 -0
  12. package/lib/matchers/toFailTestWith/index.test.d.ts +2 -0
  13. package/lib/matchers/toFailTestWith/predicate.test.d.ts +2 -0
  14. package/lib/matchers/toFailWith/index.test.d.ts +2 -0
  15. package/lib/matchers/toFailWith/predicate.test.d.ts +2 -0
  16. package/lib/matchers/toFailWithDetail/index.test.d.ts +2 -0
  17. package/lib/matchers/toFailWithDetail/predicate.test.d.ts +2 -0
  18. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/index.test.d.ts +2 -0
  19. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.test.d.ts +2 -0
  20. package/lib/matchers/toSucceed/index.test.d.ts +2 -0
  21. package/lib/matchers/toSucceed/predicate.test.d.ts +2 -0
  22. package/lib/matchers/toSucceedAndMatchInlineSnapshot/index.test.d.ts +2 -0
  23. package/lib/matchers/toSucceedAndMatchSnapshot/index.test.d.ts +2 -0
  24. package/lib/matchers/toSucceedAndSatisfy/index.test.d.ts +2 -0
  25. package/lib/matchers/toSucceedAndSatisfy/predicate.test.d.ts +2 -0
  26. package/lib/matchers/toSucceedWith/index.test.d.ts +2 -0
  27. package/lib/matchers/toSucceedWith/predicate.test.d.ts +2 -0
  28. package/lib/matchers/toSucceedWithDetail/index.test.d.ts +2 -0
  29. package/lib/matchers/toSucceedWithDetail/predicate.test.d.ts +2 -0
  30. package/lib/resolvers/resolvers.test.d.ts +2 -0
  31. package/lib/tsdoc-metadata.json +1 -1
  32. package/package.json +8 -8
  33. package/src/__snapshots__/index.test.ts.snap +24 -0
  34. package/src/helpers/fsHelpers.ts +184 -0
  35. package/src/helpers/index.ts +1 -0
  36. package/src/index.ts +27 -0
  37. package/src/matchers/index.ts +29 -0
  38. package/src/matchers/toFail/__snapshots__/index.test.ts.snap +17 -0
  39. package/src/matchers/toFail/index.ts +45 -0
  40. package/src/matchers/toFail/predicate.ts +7 -0
  41. package/src/matchers/toFailTest/__snapshots__/index.test.ts.snap +15 -0
  42. package/src/matchers/toFailTest/index.ts +48 -0
  43. package/src/matchers/toFailTest/predicate.ts +8 -0
  44. package/src/matchers/toFailTestAndMatchSnapshot/__snapshots__/index.test.ts.snap +14 -0
  45. package/src/matchers/toFailTestAndMatchSnapshot/index.ts +47 -0
  46. package/src/matchers/toFailTestAndMatchSnapshot/predicate.ts +12 -0
  47. package/src/matchers/toFailTestWith/__snapshots__/index.test.ts.snap +34 -0
  48. package/src/matchers/toFailTestWith/index.ts +56 -0
  49. package/src/matchers/toFailTestWith/predicate.ts +24 -0
  50. package/src/matchers/toFailWith/__snapshots__/index.test.ts.snap +25 -0
  51. package/src/matchers/toFailWith/index.ts +52 -0
  52. package/src/matchers/toFailWith/predicate.ts +16 -0
  53. package/src/matchers/toFailWithDetail/__snapshots__/index.test.ts.snap +41 -0
  54. package/src/matchers/toFailWithDetail/index.ts +63 -0
  55. package/src/matchers/toFailWithDetail/predicate.ts +27 -0
  56. package/src/matchers/toHaveBeenCalledWithArgumentsMatching/__snapshots__/index.test.ts.snap +82 -0
  57. package/src/matchers/toHaveBeenCalledWithArgumentsMatching/index.ts +100 -0
  58. package/src/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.ts +18 -0
  59. package/src/matchers/toSucceed/__snapshots__/index.test.ts.snap +17 -0
  60. package/src/matchers/toSucceed/index.ts +45 -0
  61. package/src/matchers/toSucceed/predicate.ts +7 -0
  62. package/src/matchers/toSucceedAndMatchInlineSnapshot/__snapshots__/index.test.ts.snap +15 -0
  63. package/src/matchers/toSucceedAndMatchInlineSnapshot/index.ts +44 -0
  64. package/src/matchers/toSucceedAndMatchSnapshot/__snapshots__/index.test.ts.snap +27 -0
  65. package/src/matchers/toSucceedAndMatchSnapshot/index.ts +47 -0
  66. package/src/matchers/toSucceedAndSatisfy/__snapshots__/index.test.ts.snap +37 -0
  67. package/src/matchers/toSucceedAndSatisfy/index.ts +78 -0
  68. package/src/matchers/toSucceedAndSatisfy/predicate.ts +22 -0
  69. package/src/matchers/toSucceedWith/__snapshots__/index.test.ts.snap +25 -0
  70. package/src/matchers/toSucceedWith/index.ts +58 -0
  71. package/src/matchers/toSucceedWith/predicate.ts +14 -0
  72. package/src/matchers/toSucceedWithDetail/__snapshots__/index.test.ts.snap +41 -0
  73. package/src/matchers/toSucceedWithDetail/index.ts +62 -0
  74. package/src/matchers/toSucceedWithDetail/predicate.ts +23 -0
  75. package/src/resolvers/cli.ts +21 -0
  76. package/src/resolvers/ide.ts +21 -0
  77. package/src/ts-utils.ts +1 -0
  78. package/src/types/index.ts +107 -0
  79. package/src/utils/matcherHelpers.ts +57 -0
  80. package/src/utils/snapshotResolver.ts +28 -0
  81. package/test/data/testData.json +4 -0
  82. package/CHANGELOG.md +0 -105
  83. package/lib/helpers/fsHelpers.d.ts.map +0 -1
  84. package/lib/helpers/fsHelpers.js.map +0 -1
  85. package/lib/helpers/index.d.ts.map +0 -1
  86. package/lib/helpers/index.js.map +0 -1
  87. package/lib/index.d.ts.map +0 -1
  88. package/lib/index.js.map +0 -1
  89. package/lib/matchers/index.d.ts.map +0 -1
  90. package/lib/matchers/index.js.map +0 -1
  91. package/lib/matchers/toFail/index.d.ts.map +0 -1
  92. package/lib/matchers/toFail/index.js.map +0 -1
  93. package/lib/matchers/toFail/predicate.d.ts.map +0 -1
  94. package/lib/matchers/toFail/predicate.js.map +0 -1
  95. package/lib/matchers/toFailTest/index.d.ts.map +0 -1
  96. package/lib/matchers/toFailTest/index.js.map +0 -1
  97. package/lib/matchers/toFailTest/predicate.d.ts.map +0 -1
  98. package/lib/matchers/toFailTest/predicate.js.map +0 -1
  99. package/lib/matchers/toFailTestAndMatchSnapshot/index.d.ts.map +0 -1
  100. package/lib/matchers/toFailTestAndMatchSnapshot/index.js.map +0 -1
  101. package/lib/matchers/toFailTestAndMatchSnapshot/predicate.d.ts.map +0 -1
  102. package/lib/matchers/toFailTestAndMatchSnapshot/predicate.js.map +0 -1
  103. package/lib/matchers/toFailTestWith/index.d.ts.map +0 -1
  104. package/lib/matchers/toFailTestWith/index.js.map +0 -1
  105. package/lib/matchers/toFailTestWith/predicate.d.ts.map +0 -1
  106. package/lib/matchers/toFailTestWith/predicate.js.map +0 -1
  107. package/lib/matchers/toFailWith/index.d.ts.map +0 -1
  108. package/lib/matchers/toFailWith/index.js.map +0 -1
  109. package/lib/matchers/toFailWith/predicate.d.ts.map +0 -1
  110. package/lib/matchers/toFailWith/predicate.js.map +0 -1
  111. package/lib/matchers/toFailWithDetail/index.d.ts.map +0 -1
  112. package/lib/matchers/toFailWithDetail/index.js.map +0 -1
  113. package/lib/matchers/toFailWithDetail/predicate.d.ts.map +0 -1
  114. package/lib/matchers/toFailWithDetail/predicate.js.map +0 -1
  115. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/index.d.ts.map +0 -1
  116. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/index.js.map +0 -1
  117. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.d.ts.map +0 -1
  118. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.js.map +0 -1
  119. package/lib/matchers/toSucceed/index.d.ts.map +0 -1
  120. package/lib/matchers/toSucceed/index.js.map +0 -1
  121. package/lib/matchers/toSucceed/predicate.d.ts.map +0 -1
  122. package/lib/matchers/toSucceed/predicate.js.map +0 -1
  123. package/lib/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts.map +0 -1
  124. package/lib/matchers/toSucceedAndMatchInlineSnapshot/index.js.map +0 -1
  125. package/lib/matchers/toSucceedAndMatchSnapshot/index.d.ts.map +0 -1
  126. package/lib/matchers/toSucceedAndMatchSnapshot/index.js.map +0 -1
  127. package/lib/matchers/toSucceedAndSatisfy/index.d.ts.map +0 -1
  128. package/lib/matchers/toSucceedAndSatisfy/index.js.map +0 -1
  129. package/lib/matchers/toSucceedAndSatisfy/predicate.d.ts.map +0 -1
  130. package/lib/matchers/toSucceedAndSatisfy/predicate.js.map +0 -1
  131. package/lib/matchers/toSucceedWith/index.d.ts.map +0 -1
  132. package/lib/matchers/toSucceedWith/index.js.map +0 -1
  133. package/lib/matchers/toSucceedWith/predicate.d.ts.map +0 -1
  134. package/lib/matchers/toSucceedWith/predicate.js.map +0 -1
  135. package/lib/matchers/toSucceedWithDetail/index.d.ts.map +0 -1
  136. package/lib/matchers/toSucceedWithDetail/index.js.map +0 -1
  137. package/lib/matchers/toSucceedWithDetail/predicate.d.ts.map +0 -1
  138. package/lib/matchers/toSucceedWithDetail/predicate.js.map +0 -1
  139. package/lib/resolvers/cli.d.ts.map +0 -1
  140. package/lib/resolvers/cli.js.map +0 -1
  141. package/lib/resolvers/ide.d.ts.map +0 -1
  142. package/lib/resolvers/ide.js.map +0 -1
  143. package/lib/ts-utils.d.ts.map +0 -1
  144. package/lib/ts-utils.js.map +0 -1
  145. package/lib/types/index.d.ts.map +0 -1
  146. package/lib/types/index.js.map +0 -1
  147. package/lib/utils/matcherHelpers.d.ts.map +0 -1
  148. package/lib/utils/matcherHelpers.js.map +0 -1
  149. package/lib/utils/snapshotResolver.d.ts.map +0 -1
  150. package/lib/utils/snapshotResolver.js.map +0 -1
@@ -0,0 +1,78 @@
1
+ import { Result, ResultValueType } from '@fgv/ts-utils';
2
+ import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
3
+ import { matcherName, predicate } from './predicate';
4
+
5
+ import { matcherHint } from 'jest-matcher-utils';
6
+
7
+ declare global {
8
+ // eslint-disable-next-line @typescript-eslint/no-namespace
9
+ namespace jest {
10
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars, @typescript-eslint/naming-convention
11
+ interface Matchers<R, T> {
12
+ /**
13
+ * Use .toSucceedAndSatisfy to verify that a Result<T> is a success
14
+ * and that the supplied test function returns true (or void)
15
+ * for the resulting value
16
+ * @param test -
17
+ */
18
+ toSucceedAndSatisfy(test: (value: ResultValueType<T>) => boolean | void): R;
19
+ }
20
+ }
21
+ }
22
+
23
+ function passMessage<T>(received: Result<T>): () => string {
24
+ const expected = 'successful callback';
25
+ const got = [printReceivedResult(received)];
26
+ got.push(' Callback returned true');
27
+
28
+ return () =>
29
+ [
30
+ matcherHint(`.not.${matcherName}`, 'result', 'callback'),
31
+ printExpectedResult('success', false, expected),
32
+ ...got
33
+ ].join('\n');
34
+ }
35
+
36
+ function failMessage<T>(received: Result<T>, cbResult: Result<boolean | void>): () => string {
37
+ const expected = 'successful callback';
38
+ const got = [printReceivedResult(received)];
39
+
40
+ /* c8 ignore else */
41
+ if (cbResult.isFailure()) {
42
+ got.push(cbResult.message);
43
+ } else if (cbResult.value === false) {
44
+ got.push(' Callback returned false');
45
+ } else if (cbResult.value === undefined) {
46
+ got.push(' Callback was not invoked');
47
+ } else {
48
+ throw new Error('Internal error: toSucceedAndSatisfy.failMessage passed success with true');
49
+ }
50
+
51
+ return () =>
52
+ [
53
+ matcherHint(`${matcherName}`, 'result', 'callback'),
54
+ printExpectedResult('success', true, expected),
55
+ ...got
56
+ ].join('\n');
57
+ }
58
+
59
+ export default {
60
+ toSucceedAndSatisfy: function <T>(
61
+ this: jest.MatcherContext,
62
+ received: Result<T>,
63
+ test: (value: T) => boolean | void
64
+ ): jest.CustomMatcherResult {
65
+ // For the normal (not '.not') case, we do not want to capture exceptions
66
+ // so that the IDE can display exactly the line on which the failure case.
67
+ // For the .not case, we want to swallow exceptions or expect failures since
68
+ // we're just testing failure and not the reason.
69
+ const capture = this.isNot;
70
+ const cbResult = predicate(received, test, !!capture);
71
+ const pass = cbResult.isSuccess() && cbResult.value === true;
72
+ if (pass) {
73
+ return { pass: true, message: passMessage(received) };
74
+ }
75
+
76
+ return { pass: false, message: failMessage(received, cbResult) };
77
+ }
78
+ };
@@ -0,0 +1,22 @@
1
+ import { Result, captureResult, fail, succeed } from '../../ts-utils';
2
+
3
+ export const matcherName: string = 'toSucceedAndSatisfy';
4
+
5
+ // If the result is successful and callback does not throw, returns Success with the callback return value, or
6
+ // true if the callback returns undefined
7
+ // If the result is successful but the callback throws, returns Failure with the thrown error
8
+ // If the result is failure, returns Success with undefined
9
+ export function predicate<T>(
10
+ received: Result<T>,
11
+ cb: (value: T) => boolean | void,
12
+ capture: boolean
13
+ ): Result<boolean | undefined> {
14
+ if (received.isSuccess()) {
15
+ const cbResult = capture ? captureResult(() => cb(received.value)) : succeed(cb(received.value));
16
+ if (cbResult.isSuccess()) {
17
+ return succeed(cbResult.value === true || cbResult.value === undefined);
18
+ }
19
+ return fail(` Callback failed with:\n ${cbResult.message}`);
20
+ }
21
+ return succeed(undefined);
22
+ }
@@ -0,0 +1,25 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`.toSucceedWith reports details when it fails due to a failure result: toFailTestAndMatchSnapshot 1`] = `
4
+ "expect(received).toSucceedWith(expected)
5
+ Expected:
6
+ Success with \\"oops\\"
7
+ Received:
8
+ Failure with \\"oops\\""
9
+ `;
10
+
11
+ exports[`.toSucceedWith reports details when it fails due to a success result with .not: toFailTestAndMatchSnapshot 1`] = `
12
+ "expect(received).not.toSucceedWith(expected)
13
+ Expected:
14
+ Not success with \\"hello\\"
15
+ Received:
16
+ Success with \\"hello\\""
17
+ `;
18
+
19
+ exports[`.toSucceedWith reports details when it fails due to success with a non-matching value: toFailTestAndMatchSnapshot 1`] = `
20
+ "expect(received).toSucceedWith(expected)
21
+ Expected:
22
+ Success with \\"goodbye\\"
23
+ Received:
24
+ Success with \\"hello\\""
25
+ `;
@@ -0,0 +1,58 @@
1
+ import { Result, ResultValueType } from '@fgv/ts-utils';
2
+ import { printExpectedResult, printReceivedResult } from '../../utils/matcherHelpers';
3
+ import { matcherName, predicate } from './predicate';
4
+
5
+ import { matcherHint } from 'jest-matcher-utils';
6
+
7
+ declare global {
8
+ // eslint-disable-next-line @typescript-eslint/no-namespace
9
+ namespace jest {
10
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars, @typescript-eslint/naming-convention
11
+ interface Matchers<R, T> {
12
+ /**
13
+ * Use .toSucceedWith to verify that a Result\<T\> is a success
14
+ * and that the result value matches the supplied value
15
+ * @param expected -
16
+ */
17
+ toSucceedWith(expected: ResultValueType<T> | RegExp): R;
18
+ }
19
+ }
20
+ }
21
+
22
+ function passMessage<T extends Result<unknown>>(
23
+ received: T,
24
+ expected: ResultValueType<T> | RegExp
25
+ ): () => string {
26
+ return () =>
27
+ [
28
+ matcherHint(`.not.${matcherName}`),
29
+ printExpectedResult('success', false, expected),
30
+ printReceivedResult(received)
31
+ ].join('\n');
32
+ }
33
+
34
+ function failMessage<T extends Result<unknown>>(
35
+ received: T,
36
+ expected: ResultValueType<T> | RegExp
37
+ ): () => string {
38
+ return () =>
39
+ [
40
+ matcherHint(`${matcherName}`),
41
+ printExpectedResult('success', true, expected),
42
+ printReceivedResult(received)
43
+ ].join('\n');
44
+ }
45
+
46
+ export default {
47
+ toSucceedWith: function <T extends Result<unknown>>(
48
+ received: T,
49
+ expected: ResultValueType<T> | RegExp
50
+ ): jest.CustomMatcherResult {
51
+ const pass = predicate(received, expected);
52
+ if (pass) {
53
+ return { pass: true, message: passMessage(received, expected) };
54
+ }
55
+
56
+ return { pass: false, message: failMessage(received, expected) };
57
+ }
58
+ };
@@ -0,0 +1,14 @@
1
+ import { equals } from '@jest/expect-utils';
2
+ import { Result } from '../../ts-utils';
3
+
4
+ export const matcherName: string = 'toSucceedWith';
5
+
6
+ export function predicate<T>(received: Result<T>, expected: unknown): boolean {
7
+ if (received.isSuccess()) {
8
+ if (typeof received.value === 'string' && expected instanceof RegExp) {
9
+ return expected.test(received.value);
10
+ }
11
+ return equals(received.value, expected);
12
+ }
13
+ return false;
14
+ }
@@ -0,0 +1,41 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`.toSucceedWithDetail reports details when it fails due to a failure result: toFailTestAndMatchSnapshot 1`] = `
4
+ "expect(received).toSucceedWithDetail(expected)
5
+ Expected:
6
+ Success with \\"oops\\"
7
+ Detail: \\"\\"detail\\"\\"
8
+ Received:
9
+ Failure with \\"oops\\"
10
+ Detail: \\"\\"detail\\"\\""
11
+ `;
12
+
13
+ exports[`.toSucceedWithDetail reports details when it fails due to a success result with .not: toFailTestAndMatchSnapshot 1`] = `
14
+ "expect(received).not.toSucceedWithDetail(expected)
15
+ Expected:
16
+ Not success with \\"hello\\"
17
+ Detail: \\"\\"detail\\"\\"
18
+ Received:
19
+ Success with \\"\\"hello\\"\\"
20
+ Detail: \\"\\"detail\\"\\""
21
+ `;
22
+
23
+ exports[`.toSucceedWithDetail reports details when it fails due to success with a non-matching detail: toFailTestAndMatchSnapshot 1`] = `
24
+ "expect(received).toSucceedWithDetail(expected)
25
+ Expected:
26
+ Success with \\"hello\\"
27
+ Detail: \\"\\"other detail\\"\\"
28
+ Received:
29
+ Success with \\"\\"hello\\"\\"
30
+ Detail: \\"\\"detail\\"\\""
31
+ `;
32
+
33
+ exports[`.toSucceedWithDetail reports details when it fails due to success with a non-matching value: toFailTestAndMatchSnapshot 1`] = `
34
+ "expect(received).toSucceedWithDetail(expected)
35
+ Expected:
36
+ Success with \\"goodbye\\"
37
+ Detail: \\"\\"detail\\"\\"
38
+ Received:
39
+ Success with \\"\\"hello\\"\\"
40
+ Detail: \\"\\"detail\\"\\""
41
+ `;
@@ -0,0 +1,62 @@
1
+ import { DetailedResult, ResultDetailType, ResultValueType } from '@fgv/ts-utils';
2
+ import { printExpectedDetailedResult, printReceivedDetailedResult } from '../../utils/matcherHelpers';
3
+ import { matcherName, predicate } from './predicate';
4
+
5
+ import { matcherHint } from 'jest-matcher-utils';
6
+
7
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars
8
+ declare global {
9
+ // eslint-disable-next-line @typescript-eslint/no-namespace
10
+ namespace jest {
11
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, no-unused-vars, @typescript-eslint/naming-convention
12
+ interface Matchers<R, T> {
13
+ /**
14
+ * Use .toSucceedWithDetail to verify that a DetailedResult\<T, TD\> is
15
+ * a success and that the result value and detail matches the supplied
16
+ * values
17
+ */
18
+ toSucceedWithDetail(expected: ResultValueType<T> | RegExp, detail: ResultDetailType<T> | undefined): R;
19
+ }
20
+ }
21
+ }
22
+
23
+ function passMessage<T extends DetailedResult<unknown, unknown>>(
24
+ received: T,
25
+ expected: ResultValueType<T> | RegExp,
26
+ expectedDetail: ResultDetailType<T> | undefined
27
+ ): () => string {
28
+ return () =>
29
+ [
30
+ matcherHint(`.not.${matcherName}`),
31
+ printExpectedDetailedResult('success', false, expected, expectedDetail),
32
+ printReceivedDetailedResult(received)
33
+ ].join('\n');
34
+ }
35
+
36
+ function failMessage<T extends DetailedResult<unknown, unknown>>(
37
+ received: T,
38
+ expected: ResultValueType<T> | RegExp,
39
+ expectedDetail: ResultDetailType<T> | undefined
40
+ ): () => string {
41
+ return () =>
42
+ [
43
+ matcherHint(`${matcherName}`),
44
+ printExpectedDetailedResult('success', true, expected, expectedDetail),
45
+ printReceivedDetailedResult(received)
46
+ ].join('\n');
47
+ }
48
+
49
+ export default {
50
+ toSucceedWithDetail: function <T extends DetailedResult<unknown, unknown>>(
51
+ received: T,
52
+ expected: ResultValueType<T> | RegExp,
53
+ detail: ResultDetailType<T> | undefined
54
+ ): jest.CustomMatcherResult {
55
+ const pass = predicate(received, expected, detail);
56
+ if (pass) {
57
+ return { pass: true, message: passMessage(received, expected, detail) };
58
+ }
59
+
60
+ return { pass: false, message: failMessage(received, expected, detail) };
61
+ }
62
+ };
@@ -0,0 +1,23 @@
1
+ import { equals } from '@jest/expect-utils';
2
+ import { DetailedResult } from '../../ts-utils';
3
+
4
+ export const matcherName: string = 'toSucceedWithDetail';
5
+
6
+ export function predicate<T, TD>(
7
+ received: DetailedResult<T, TD>,
8
+ expected: unknown,
9
+ detail: TD | undefined
10
+ ): boolean {
11
+ if (received.isSuccess()) {
12
+ let pass = false;
13
+ if (typeof received.value === 'string' && expected instanceof RegExp) {
14
+ pass = expected.test(received.value);
15
+ } else {
16
+ pass = equals(received.value, expected);
17
+ }
18
+
19
+ pass = pass && equals(received.detail, detail);
20
+ return pass;
21
+ }
22
+ return false;
23
+ }
@@ -0,0 +1,21 @@
1
+ import * as Resolver from '../utils/snapshotResolver';
2
+
3
+ const CLI_SNAPSHOT_FOLDER: string = '__cli__';
4
+
5
+ function resolveSnapshotPath(testPath: string, ext: string): string {
6
+ return Resolver.resolveSnapshotPath(testPath, ext, CLI_SNAPSHOT_FOLDER);
7
+ }
8
+
9
+ function resolveTestPath(snapshotPath: string, ext: string): string {
10
+ return Resolver.resolveTestPath(snapshotPath, ext);
11
+ }
12
+
13
+ const testPathForConsistencyCheck: string = Resolver.testPathForConsistencyCheck;
14
+
15
+ module.exports = {
16
+ resolveSnapshotPath,
17
+ resolveTestPath,
18
+ testPathForConsistencyCheck
19
+ };
20
+
21
+ export default module.exports;
@@ -0,0 +1,21 @@
1
+ import * as Resolver from '../utils/snapshotResolver';
2
+
3
+ const CLI_SNAPSHOT_FOLDER: string = '__ide__';
4
+
5
+ function resolveSnapshotPath(testPath: string, ext: string): string {
6
+ return Resolver.resolveSnapshotPath(testPath, ext, CLI_SNAPSHOT_FOLDER);
7
+ }
8
+
9
+ function resolveTestPath(snapshotPath: string, ext: string): string {
10
+ return Resolver.resolveTestPath(snapshotPath, ext);
11
+ }
12
+
13
+ const testPathForConsistencyCheck: string = Resolver.testPathForConsistencyCheck;
14
+
15
+ module.exports = {
16
+ resolveSnapshotPath,
17
+ resolveTestPath,
18
+ testPathForConsistencyCheck
19
+ };
20
+
21
+ export default module.exports;
@@ -0,0 +1 @@
1
+ export * from '@fgv/ts-utils';
@@ -0,0 +1,107 @@
1
+ /// <reference types="jest"/>
2
+
3
+ // eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-explicit-any
4
+ export type Function = (...args: any[]) => any;
5
+
6
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
7
+ import { ResultDetailType, ResultValueType } from '@fgv/ts-utils';
8
+
9
+ /* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
10
+ declare global {
11
+ // eslint-disable-next-line @typescript-eslint/no-namespace
12
+ namespace jest {
13
+ // eslint-disable-next-line @typescript-eslint/naming-convention
14
+ interface Matchers<R, T> {
15
+ /**
16
+ * Use .toSucceed to verify that a Result\<T\> is a success
17
+ */
18
+ toSucceed(): R;
19
+
20
+ /**
21
+ * Use .toSucceedWith to verify that a Result\<T\> is a success
22
+ * and that the result value matches the supplied value
23
+ * @param expected -
24
+ */
25
+ toSucceedWith(expected: ResultValueType<T> | RegExp): R;
26
+
27
+ /**
28
+ * Use .toSucceedWithDetail to verify that a DetailedResult\<T, TD\> is
29
+ * a success and that the result value and detail matches the supplied
30
+ * values
31
+ * @param expected -
32
+ * @param detail -
33
+ */
34
+ toSucceedWithDetail(expected: ResultValueType<T>, detail: ResultDetailType<T> | undefined): R;
35
+
36
+ /**
37
+ * Use .toSucceedAndSatisfy to verify that a Result\<T\> is a success
38
+ * and that the supplied test function returns true (or void)
39
+ * for the resulting value
40
+ * @param test -
41
+ */
42
+ toSucceedAndSatisfy(test: (value: ResultValueType<T>) => boolean | void): R;
43
+
44
+ /**
45
+ * Use .toSucceedAndMatchInlineSnapshot to verify that a Result\<T\> is a success
46
+ * and that the result value matches an inline snapshot
47
+ */
48
+ toSucceedAndMatchInlineSnapshot(snapshot: string | undefined): R;
49
+
50
+ /**
51
+ * Use .toSucceedAndMatchSnapshot to verify that a Result\<T\> is a success
52
+ * and that the result value matches a stored snapshot
53
+ */
54
+ toSucceedAndMatchSnapshot(): R;
55
+
56
+ /**
57
+ * Use .toFail to verify that a Result\<T\> is a failure
58
+ */
59
+ toFail(): R;
60
+
61
+ /**
62
+ * Use .toFailWith to verify that a Result\<T\> is a failure
63
+ * that matches a supplied string, RegExp or undefined value
64
+ * @param message -
65
+ */
66
+ toFailWith(expected: string | RegExp | undefined): R;
67
+
68
+ /**
69
+ * Use .toFailWithDetail to verify that a DetailedResult\<T\> is
70
+ * a failure that matches both a supplied expected failure value
71
+ * (string, RegExp or undefined) and a supplied failure detail.
72
+ * @param message -
73
+ * @param detail -
74
+ */
75
+ toFailWithDetail<TDetail>(message: string | RegExp, detail: TDetail): R;
76
+ toFailWithDetail(message: string | RegExp | undefined, detail: ResultDetailType<T>): R;
77
+
78
+ /**
79
+ * Use .toFailTest to test a custom matcher by
80
+ * verifying that a test case fails.
81
+ */
82
+ toFailTest(): R;
83
+
84
+ /**
85
+ * Use .toFailTestWith to test a custom matcher by
86
+ * verifying that a test case fails as expected and
87
+ * reports an error matching a stored snapshot.
88
+ */
89
+ toFailTestAndMatchSnapshot(): R;
90
+
91
+ /**
92
+ * Use .toFailTestWith to test a custom matcher by
93
+ * verifying that a test case fails as expected and
94
+ * reports an error matching a supplied value.
95
+ * @param expected -
96
+ */
97
+ toFailTestWith(expected: string | string[] | RegExp): R;
98
+
99
+ /**
100
+ * Use .toSucceedWith to verify that a Result\<T\> is a success
101
+ * and that the result value matches the supplied value
102
+ * @param expected -
103
+ */
104
+ toHaveBeenCalledWithArgumentsMatching(expected: unknown): R;
105
+ }
106
+ }
107
+ }
@@ -0,0 +1,57 @@
1
+ import { printExpected, printReceived } from 'jest-matcher-utils';
2
+ import { DetailedResult, Result } from '../ts-utils';
3
+
4
+ function printExpectedValue<T>(outcome: string, expected?: T): string {
5
+ return expected !== undefined ? ` ${outcome} with ${printExpected(expected)}` : ` ${outcome}`;
6
+ }
7
+
8
+ export function printExpectedResult<T>(expect: 'success' | 'failure', isNot: boolean, expected?: T): string {
9
+ return [
10
+ 'Expected:',
11
+ isNot
12
+ ? expect === 'success'
13
+ ? printExpectedValue('Success', expected)
14
+ : printExpectedValue('Failure', expected)
15
+ : expect === 'success'
16
+ ? printExpectedValue('Not success', expected)
17
+ : printExpectedValue('Not failure', expected)
18
+ ].join('\n');
19
+ }
20
+
21
+ export function printExpectedDetailedResult<T, TD>(
22
+ expect: 'success' | 'failure',
23
+ isNot: boolean,
24
+ expectedMessage?: T,
25
+ expectedDetail?: TD
26
+ ): string {
27
+ /* c8 ignore next */
28
+ return [
29
+ 'Expected:',
30
+ isNot
31
+ ? expect === 'success'
32
+ ? printExpectedValue('Success', expectedMessage)
33
+ : printExpectedValue('Failure', expectedMessage)
34
+ : expect === 'success'
35
+ ? printExpectedValue('Not success', expectedMessage)
36
+ : printExpectedValue('Not failure', expectedMessage),
37
+ ` Detail: "${printExpected(expectedDetail)}"`
38
+ ].join('\n');
39
+ }
40
+
41
+ export function printReceivedResult<T>(received: Result<T>): string {
42
+ return [
43
+ 'Received:',
44
+ received.isSuccess()
45
+ ? ` Success with ${printReceived(received.value)}`
46
+ : ` Failure with "${received.message}"`
47
+ ].join('\n');
48
+ }
49
+
50
+ export function printReceivedDetailedResult<T, TD>(received: DetailedResult<T, TD>): string {
51
+ return [
52
+ 'Received:',
53
+ received.isSuccess()
54
+ ? ` Success with "${printReceived(received.value)}"\n Detail: "${printReceived(received.detail)}"`
55
+ : ` Failure with "${received.message}"\n Detail: "${printReceived(received.detail)}"`
56
+ ].join('\n');
57
+ }
@@ -0,0 +1,28 @@
1
+ import path from 'path';
2
+
3
+ export function resolveSnapshotPath(
4
+ testPath: string,
5
+ snapshotExtension: string,
6
+ snapshotFolderName: string
7
+ ): string {
8
+ const snapshotPath = path.join(
9
+ path.join(path.dirname(testPath), '__snapshots__', snapshotFolderName),
10
+ path.basename(testPath) + snapshotExtension
11
+ );
12
+ return snapshotPath;
13
+ }
14
+
15
+ export function resolveTestPath(snapshotFilePath: string, snapshotExtension: string): string {
16
+ const testPath = path.join(
17
+ path.dirname(path.dirname(path.dirname(snapshotFilePath))),
18
+ path.basename(snapshotFilePath, snapshotExtension)
19
+ );
20
+ return testPath;
21
+ }
22
+
23
+ export const testPathForConsistencyCheck: string = path.posix.join(
24
+ 'consistency_check',
25
+ '__tests__',
26
+ 'subdir',
27
+ 'example.test.js'
28
+ );
@@ -0,0 +1,4 @@
1
+ {
2
+ "filename": "testData.json",
3
+ "fromBackingFile": true
4
+ }
package/CHANGELOG.md DELETED
@@ -1,105 +0,0 @@
1
- # Change Log - @fgv/ts-utils-jest
2
-
3
- This log was last generated on Thu, 17 Jul 2025 00:13:24 GMT and should not be manually modified.
4
-
5
- ## 5.0.0
6
- Thu, 17 Jul 2025 00:13:24 GMT
7
-
8
- ### Updates
9
-
10
- - minor fixes
11
- - bump version
12
-
13
- ## 4.6.0
14
- Wed, 02 Jul 2025 05:48:16 GMT
15
-
16
- _Version update only_
17
-
18
- ## 4.5.1
19
- Wed, 02 Jul 2025 05:47:11 GMT
20
-
21
- ### Updates
22
-
23
- - update rushstack
24
-
25
- ## 4.5.0
26
- Tue, 01 Jul 2025 03:26:11 GMT
27
-
28
- _Version update only_
29
-
30
- ## 4.4.0
31
- Sat, 01 Feb 2025 17:13:10 GMT
32
-
33
- _Version update only_
34
-
35
- ## 4.3.0
36
- Thu, 30 Jan 2025 00:35:17 GMT
37
-
38
- _Version update only_
39
-
40
- ## 4.2.2
41
- Thu, 23 Jan 2025 06:19:32 GMT
42
-
43
- _Version update only_
44
-
45
- ## 4.2.1
46
- Tue, 21 Jan 2025 04:19:21 GMT
47
-
48
- _Version update only_
49
-
50
- ## 4.2.0
51
- Mon, 20 Jan 2025 09:46:53 GMT
52
-
53
- _Version update only_
54
-
55
- ## 4.1.0
56
- Thu, 09 Jan 2025 05:33:39 GMT
57
-
58
- ### Updates
59
-
60
- - update dependencies
61
-
62
- ## 4.0.2
63
- Tue, 14 May 2024 14:45:53 GMT
64
-
65
- _Version update only_
66
-
67
- ## 4.0.1
68
- Tue, 14 May 2024 05:02:20 GMT
69
-
70
- ### Updates
71
-
72
- - publish
73
-
74
- ## 4.0.0
75
- Tue, 14 May 2024 03:09:27 GMT
76
-
77
- ### Updates
78
-
79
- - update dependencies
80
-
81
- ## 3.0.0
82
- Mon, 22 Jan 2024 07:00:18 GMT
83
-
84
- ### Updates
85
-
86
- - gitignore tweaks
87
- - fix types for ts-utils-jest
88
- - fix peers
89
- - bump versions
90
- - cleanup
91
- - Update dependencies
92
- - bump dependencies, fix tests
93
- - version upgrades
94
- - update dependencies
95
- - update peer dependencies
96
- - bump version
97
-
98
- ## 2.0.0
99
- Mon, 10 Apr 2023 03:33:11 GMT
100
-
101
- ### Breaking changes
102
-
103
- - initial checking
104
- - refactor
105
-