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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/.vscode/launch.json +16 -0
  2. package/README.md +11 -0
  3. package/config/api-extractor.json +343 -0
  4. package/config/jest-disable-colors.js +34 -0
  5. package/config/rig.json +16 -0
  6. package/lib/helpers/fsHelpers.test.d.ts +2 -0
  7. package/lib/index.test.d.ts +2 -0
  8. package/lib/matchers/toFail/index.test.d.ts +2 -0
  9. package/lib/matchers/toFail/predicate.test.d.ts +2 -0
  10. package/lib/matchers/toFailTest/index.test.d.ts +2 -0
  11. package/lib/matchers/toFailTest/predicate.test.d.ts +2 -0
  12. package/lib/matchers/toFailTestAndMatchSnapshot/index.js +2 -1
  13. package/lib/matchers/toFailTestAndMatchSnapshot/index.test.d.ts +2 -0
  14. package/lib/matchers/toFailTestAndMatchSnapshot/predicate.test.d.ts +2 -0
  15. package/lib/matchers/toFailTestWith/index.test.d.ts +2 -0
  16. package/lib/matchers/toFailTestWith/predicate.test.d.ts +2 -0
  17. package/lib/matchers/toFailWith/index.test.d.ts +2 -0
  18. package/lib/matchers/toFailWith/predicate.test.d.ts +2 -0
  19. package/lib/matchers/toFailWithDetail/index.test.d.ts +2 -0
  20. package/lib/matchers/toFailWithDetail/predicate.test.d.ts +2 -0
  21. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/index.test.d.ts +2 -0
  22. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.test.d.ts +2 -0
  23. package/lib/matchers/toSucceed/index.test.d.ts +2 -0
  24. package/lib/matchers/toSucceed/predicate.test.d.ts +2 -0
  25. package/lib/matchers/toSucceedAndMatchInlineSnapshot/index.test.d.ts +2 -0
  26. package/lib/matchers/toSucceedAndMatchSnapshot/index.test.d.ts +2 -0
  27. package/lib/matchers/toSucceedAndSatisfy/index.test.d.ts +2 -0
  28. package/lib/matchers/toSucceedAndSatisfy/predicate.test.d.ts +2 -0
  29. package/lib/matchers/toSucceedWith/index.test.d.ts +2 -0
  30. package/lib/matchers/toSucceedWith/predicate.test.d.ts +2 -0
  31. package/lib/matchers/toSucceedWithDetail/index.test.d.ts +2 -0
  32. package/lib/matchers/toSucceedWithDetail/predicate.test.d.ts +2 -0
  33. package/lib/resolvers/resolvers.test.d.ts +2 -0
  34. package/lib/tsdoc-metadata.json +1 -1
  35. package/lib/utils/colorHelpers.d.ts +54 -0
  36. package/lib/utils/colorHelpers.js +84 -0
  37. package/lib/utils/matcherHelpers.js +9 -5
  38. package/package.json +8 -8
  39. package/src/__snapshots__/index.test.ts.snap +24 -0
  40. package/src/helpers/fsHelpers.ts +184 -0
  41. package/src/helpers/index.ts +1 -0
  42. package/src/index.ts +27 -0
  43. package/src/matchers/index.ts +29 -0
  44. package/src/matchers/toFail/__snapshots__/index.test.ts.snap +17 -0
  45. package/src/matchers/toFail/index.ts +45 -0
  46. package/src/matchers/toFail/predicate.ts +7 -0
  47. package/src/matchers/toFailTest/__snapshots__/index.test.ts.snap +15 -0
  48. package/src/matchers/toFailTest/index.ts +48 -0
  49. package/src/matchers/toFailTest/predicate.ts +8 -0
  50. package/src/matchers/toFailTestAndMatchSnapshot/__snapshots__/index.test.ts.snap +14 -0
  51. package/src/matchers/toFailTestAndMatchSnapshot/index.ts +48 -0
  52. package/src/matchers/toFailTestAndMatchSnapshot/predicate.ts +12 -0
  53. package/src/matchers/toFailTestWith/__snapshots__/index.test.ts.snap +34 -0
  54. package/src/matchers/toFailTestWith/index.ts +56 -0
  55. package/src/matchers/toFailTestWith/predicate.ts +24 -0
  56. package/src/matchers/toFailWith/__snapshots__/index.test.ts.snap +25 -0
  57. package/src/matchers/toFailWith/index.ts +52 -0
  58. package/src/matchers/toFailWith/predicate.ts +16 -0
  59. package/src/matchers/toFailWithDetail/__snapshots__/index.test.ts.snap +41 -0
  60. package/src/matchers/toFailWithDetail/index.ts +63 -0
  61. package/src/matchers/toFailWithDetail/predicate.ts +27 -0
  62. package/src/matchers/toHaveBeenCalledWithArgumentsMatching/__snapshots__/index.test.ts.snap +82 -0
  63. package/src/matchers/toHaveBeenCalledWithArgumentsMatching/index.ts +100 -0
  64. package/src/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.ts +18 -0
  65. package/src/matchers/toSucceed/__snapshots__/index.test.ts.snap +17 -0
  66. package/src/matchers/toSucceed/index.ts +45 -0
  67. package/src/matchers/toSucceed/predicate.ts +7 -0
  68. package/src/matchers/toSucceedAndMatchInlineSnapshot/__snapshots__/index.test.ts.snap +15 -0
  69. package/src/matchers/toSucceedAndMatchInlineSnapshot/index.ts +44 -0
  70. package/src/matchers/toSucceedAndMatchSnapshot/__snapshots__/index.test.ts.snap +27 -0
  71. package/src/matchers/toSucceedAndMatchSnapshot/index.ts +47 -0
  72. package/src/matchers/toSucceedAndSatisfy/__snapshots__/index.test.ts.snap +37 -0
  73. package/src/matchers/toSucceedAndSatisfy/index.ts +78 -0
  74. package/src/matchers/toSucceedAndSatisfy/predicate.ts +22 -0
  75. package/src/matchers/toSucceedWith/__snapshots__/index.test.ts.snap +25 -0
  76. package/src/matchers/toSucceedWith/index.ts +58 -0
  77. package/src/matchers/toSucceedWith/predicate.ts +14 -0
  78. package/src/matchers/toSucceedWithDetail/__snapshots__/index.test.ts.snap +41 -0
  79. package/src/matchers/toSucceedWithDetail/index.ts +62 -0
  80. package/src/matchers/toSucceedWithDetail/predicate.ts +23 -0
  81. package/src/resolvers/cli.ts +21 -0
  82. package/src/resolvers/ide.ts +21 -0
  83. package/src/ts-utils.ts +1 -0
  84. package/src/types/index.ts +107 -0
  85. package/src/utils/colorHelpers.ts +82 -0
  86. package/src/utils/matcherHelpers.ts +64 -0
  87. package/src/utils/snapshotResolver.ts +28 -0
  88. package/test/data/testData.json +4 -0
  89. package/CHANGELOG.md +0 -105
  90. package/lib/helpers/fsHelpers.d.ts.map +0 -1
  91. package/lib/helpers/fsHelpers.js.map +0 -1
  92. package/lib/helpers/index.d.ts.map +0 -1
  93. package/lib/helpers/index.js.map +0 -1
  94. package/lib/index.d.ts.map +0 -1
  95. package/lib/index.js.map +0 -1
  96. package/lib/matchers/index.d.ts.map +0 -1
  97. package/lib/matchers/index.js.map +0 -1
  98. package/lib/matchers/toFail/index.d.ts.map +0 -1
  99. package/lib/matchers/toFail/index.js.map +0 -1
  100. package/lib/matchers/toFail/predicate.d.ts.map +0 -1
  101. package/lib/matchers/toFail/predicate.js.map +0 -1
  102. package/lib/matchers/toFailTest/index.d.ts.map +0 -1
  103. package/lib/matchers/toFailTest/index.js.map +0 -1
  104. package/lib/matchers/toFailTest/predicate.d.ts.map +0 -1
  105. package/lib/matchers/toFailTest/predicate.js.map +0 -1
  106. package/lib/matchers/toFailTestAndMatchSnapshot/index.d.ts.map +0 -1
  107. package/lib/matchers/toFailTestAndMatchSnapshot/index.js.map +0 -1
  108. package/lib/matchers/toFailTestAndMatchSnapshot/predicate.d.ts.map +0 -1
  109. package/lib/matchers/toFailTestAndMatchSnapshot/predicate.js.map +0 -1
  110. package/lib/matchers/toFailTestWith/index.d.ts.map +0 -1
  111. package/lib/matchers/toFailTestWith/index.js.map +0 -1
  112. package/lib/matchers/toFailTestWith/predicate.d.ts.map +0 -1
  113. package/lib/matchers/toFailTestWith/predicate.js.map +0 -1
  114. package/lib/matchers/toFailWith/index.d.ts.map +0 -1
  115. package/lib/matchers/toFailWith/index.js.map +0 -1
  116. package/lib/matchers/toFailWith/predicate.d.ts.map +0 -1
  117. package/lib/matchers/toFailWith/predicate.js.map +0 -1
  118. package/lib/matchers/toFailWithDetail/index.d.ts.map +0 -1
  119. package/lib/matchers/toFailWithDetail/index.js.map +0 -1
  120. package/lib/matchers/toFailWithDetail/predicate.d.ts.map +0 -1
  121. package/lib/matchers/toFailWithDetail/predicate.js.map +0 -1
  122. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/index.d.ts.map +0 -1
  123. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/index.js.map +0 -1
  124. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.d.ts.map +0 -1
  125. package/lib/matchers/toHaveBeenCalledWithArgumentsMatching/predicate.js.map +0 -1
  126. package/lib/matchers/toSucceed/index.d.ts.map +0 -1
  127. package/lib/matchers/toSucceed/index.js.map +0 -1
  128. package/lib/matchers/toSucceed/predicate.d.ts.map +0 -1
  129. package/lib/matchers/toSucceed/predicate.js.map +0 -1
  130. package/lib/matchers/toSucceedAndMatchInlineSnapshot/index.d.ts.map +0 -1
  131. package/lib/matchers/toSucceedAndMatchInlineSnapshot/index.js.map +0 -1
  132. package/lib/matchers/toSucceedAndMatchSnapshot/index.d.ts.map +0 -1
  133. package/lib/matchers/toSucceedAndMatchSnapshot/index.js.map +0 -1
  134. package/lib/matchers/toSucceedAndSatisfy/index.d.ts.map +0 -1
  135. package/lib/matchers/toSucceedAndSatisfy/index.js.map +0 -1
  136. package/lib/matchers/toSucceedAndSatisfy/predicate.d.ts.map +0 -1
  137. package/lib/matchers/toSucceedAndSatisfy/predicate.js.map +0 -1
  138. package/lib/matchers/toSucceedWith/index.d.ts.map +0 -1
  139. package/lib/matchers/toSucceedWith/index.js.map +0 -1
  140. package/lib/matchers/toSucceedWith/predicate.d.ts.map +0 -1
  141. package/lib/matchers/toSucceedWith/predicate.js.map +0 -1
  142. package/lib/matchers/toSucceedWithDetail/index.d.ts.map +0 -1
  143. package/lib/matchers/toSucceedWithDetail/index.js.map +0 -1
  144. package/lib/matchers/toSucceedWithDetail/predicate.d.ts.map +0 -1
  145. package/lib/matchers/toSucceedWithDetail/predicate.js.map +0 -1
  146. package/lib/resolvers/cli.d.ts.map +0 -1
  147. package/lib/resolvers/cli.js.map +0 -1
  148. package/lib/resolvers/ide.d.ts.map +0 -1
  149. package/lib/resolvers/ide.js.map +0 -1
  150. package/lib/ts-utils.d.ts.map +0 -1
  151. package/lib/ts-utils.js.map +0 -1
  152. package/lib/types/index.d.ts.map +0 -1
  153. package/lib/types/index.js.map +0 -1
  154. package/lib/utils/matcherHelpers.d.ts.map +0 -1
  155. package/lib/utils/matcherHelpers.js.map +0 -1
  156. package/lib/utils/snapshotResolver.d.ts.map +0 -1
  157. package/lib/utils/snapshotResolver.js.map +0 -1
@@ -0,0 +1,44 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
2
+ import { Result } from '@fgv/ts-utils';
3
+ import { matcherHint } from 'jest-matcher-utils';
4
+ import { Context, toMatchInlineSnapshot } from 'jest-snapshot';
5
+ import { printReceivedResult } 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 .toSucceedAndMatchInlineSnapshot to verify that a Result<T> is a success
14
+ * and that the result value matches an inline snapshot
15
+ */
16
+ toSucceedAndMatchInlineSnapshot<T>(snapshot: string | undefined): R;
17
+ }
18
+ }
19
+ }
20
+
21
+ const matcherName: string = 'toSucceedAndMatchInlineSnapshot';
22
+
23
+ export default {
24
+ toSucceedAndMatchInlineSnapshot: function <T>(
25
+ this: jest.MatcherContext,
26
+ received: Result<T>,
27
+ snapshot: string | undefined
28
+ ): jest.CustomMatcherResult {
29
+ const context = this as unknown as Context;
30
+ if (received.isFailure()) {
31
+ return {
32
+ pass: false,
33
+ message: (): string => {
34
+ return [
35
+ matcherHint(`${matcherName}`, 'callback'),
36
+ 'Expected:\n Callback to succeed with a result that matches the snapshot',
37
+ printReceivedResult(received)
38
+ ].join('\n');
39
+ }
40
+ };
41
+ }
42
+ return toMatchInlineSnapshot.call(context, received.value, {}, snapshot) as jest.CustomMatcherResult;
43
+ }
44
+ };
@@ -0,0 +1,27 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`.toSucceedAndMatchSnapshot logs details correctly for a failed result: toFailTestAndMatchSnapshot 1`] = `
4
+ "expect(callback).toSucceedAndMatchSnapshot(expected)
5
+ Expected:
6
+ Callback to succeed with a result that matches the snapshot
7
+ Received:
8
+ Failure with \\"oopsy\\""
9
+ `;
10
+
11
+ exports[`.toSucceedAndMatchSnapshot logs details correctly for a failed result: toFailTestAndMatchSnapshot 2`] = `
12
+ "expect(received).not.toMatchSnapshot(hint)
13
+
14
+ Matcher error: Snapshot matchers cannot be used with not"
15
+ `;
16
+
17
+ exports[`.toSucceedAndMatchSnapshot passes for a success result that matches the snapshot: toSucceedAndMatchSnapshot 1`] = `
18
+ Object {
19
+ "nestedObject": Object {
20
+ "anArray": Array [
21
+ "element 1",
22
+ "element 2",
23
+ ],
24
+ },
25
+ "someField": "this is a value",
26
+ }
27
+ `;
@@ -0,0 +1,47 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */
2
+ import { Result } from '@fgv/ts-utils';
3
+ import { matcherHint } from 'jest-matcher-utils';
4
+ import { Context, toMatchSnapshot } from 'jest-snapshot';
5
+ import { printReceivedResult } 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 .toSucceedAndMatchSnapshot to verify that a Result<T> is a success
14
+ * and that the result value matches a stored snapshot
15
+ */
16
+ toSucceedAndMatchSnapshot<T>(): R;
17
+ }
18
+ }
19
+ }
20
+
21
+ const matcherName: string = 'toSucceedAndMatchSnapshot';
22
+
23
+ export default {
24
+ toSucceedAndMatchSnapshot: function <T>(
25
+ this: jest.MatcherContext,
26
+ received: Result<T>
27
+ ): jest.CustomMatcherResult {
28
+ const context = this as unknown as Context;
29
+ if (received.isFailure()) {
30
+ return {
31
+ pass: false,
32
+ message: (): string => {
33
+ return [
34
+ matcherHint(`${matcherName}`, 'callback'),
35
+ 'Expected:\n Callback to succeed with a result that matches the snapshot',
36
+ printReceivedResult(received)
37
+ ].join('\n');
38
+ }
39
+ };
40
+ }
41
+ return toMatchSnapshot.call(
42
+ context,
43
+ received.value,
44
+ 'toSucceedAndMatchSnapshot'
45
+ ) as jest.CustomMatcherResult;
46
+ }
47
+ };
@@ -0,0 +1,37 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`.toSucceedAndSatisfy reports details when callback fails an expectation: toFailTestAndMatchSnapshot 1`] = `
4
+ "expect(received).toBe(expected) // Object.is equality
5
+
6
+ Expected: \\"goodbye\\"
7
+ Received: \\"hello\\""
8
+ `;
9
+
10
+ exports[`.toSucceedAndSatisfy reports details when callback returns false: toFailTestAndMatchSnapshot 1`] = `
11
+ "expect(result).toSucceedAndSatisfy(callback)
12
+ Expected:
13
+ Success with \\"successful callback\\"
14
+ Received:
15
+ Success with \\"hello\\"
16
+ Callback returned false"
17
+ `;
18
+
19
+ exports[`.toSucceedAndSatisfy reports details when callback throws an exception: toFailTestAndMatchSnapshot 1`] = `"UH OH AN ERROR"`;
20
+
21
+ exports[`.toSucceedAndSatisfy reports details when received is a failure result: toFailTestAndMatchSnapshot 1`] = `
22
+ "expect(result).toSucceedAndSatisfy(callback)
23
+ Expected:
24
+ Success with \\"successful callback\\"
25
+ Received:
26
+ Failure with \\"oops\\"
27
+ Callback was not invoked"
28
+ `;
29
+
30
+ exports[`.toSucceedAndSatisfy reports details with success and .not: toFailTestAndMatchSnapshot 1`] = `
31
+ "expect(result).not.toSucceedAndSatisfy(callback)
32
+ Expected:
33
+ Not success with \\"successful callback\\"
34
+ Received:
35
+ Success with \\"hello\\"
36
+ Callback returned true"
37
+ `;
@@ -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
+ }