@forwardimpact/libmock 0.1.7 → 0.1.10
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/LICENSE +21 -201
- package/package.json +6 -2
- package/src/expect/index.js +476 -0
- package/src/mock/git-client.js +75 -9
package/LICENSE
CHANGED
|
@@ -1,201 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
-
exercising permissions granted by this License.
|
|
25
|
-
|
|
26
|
-
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
-
including but not limited to software source code, documentation
|
|
28
|
-
source, and configuration files.
|
|
29
|
-
|
|
30
|
-
"Object" form shall mean any form resulting from mechanical
|
|
31
|
-
transformation or translation of a Source form, including but
|
|
32
|
-
not limited to compiled object code, generated documentation,
|
|
33
|
-
and conversions to other media types.
|
|
34
|
-
|
|
35
|
-
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
-
Object form, made available under the License, as indicated by a
|
|
37
|
-
copyright notice that is included in or attached to the work
|
|
38
|
-
(an example is provided in the Appendix below).
|
|
39
|
-
|
|
40
|
-
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
-
form, that is based on (or derived from) the Work and for which the
|
|
42
|
-
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
-
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
-
of this License, Derivative Works shall not include works that remain
|
|
45
|
-
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
-
the Work and Derivative Works thereof.
|
|
47
|
-
|
|
48
|
-
"Contribution" shall mean any work of authorship, including
|
|
49
|
-
the original version of the Work and any modifications or additions
|
|
50
|
-
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
-
submitted to the Licensor for inclusion in the Work by the copyright owner
|
|
52
|
-
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
-
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
-
means any form of electronic, verbal, or written communication sent
|
|
55
|
-
to the Licensor or its representatives, including but not limited to
|
|
56
|
-
communication on electronic mailing lists, source code control systems,
|
|
57
|
-
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
-
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
-
excluding communication that is conspicuously marked or otherwise
|
|
60
|
-
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
-
|
|
62
|
-
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
-
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
-
subsequently incorporated within the Work.
|
|
65
|
-
|
|
66
|
-
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
-
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
-
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
-
Work and such Derivative Works in Source or Object form.
|
|
72
|
-
|
|
73
|
-
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
-
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
-
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
-
(except as stated in this section) patent license to make, have made,
|
|
77
|
-
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
-
where such license applies only to those patent claims licensable
|
|
79
|
-
by such Contributor that are necessarily infringed by their
|
|
80
|
-
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
-
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
-
institute patent litigation against any entity (including a
|
|
83
|
-
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
-
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
-
or contributory patent infringement, then any patent licenses
|
|
86
|
-
granted to You under this License for that Work shall terminate
|
|
87
|
-
as of the date such litigation is filed.
|
|
88
|
-
|
|
89
|
-
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
-
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
-
modifications, and in Source or Object form, provided that You
|
|
92
|
-
meet the following conditions:
|
|
93
|
-
|
|
94
|
-
(a) You must give any other recipients of the Work or
|
|
95
|
-
Derivative Works a copy of this License; and
|
|
96
|
-
|
|
97
|
-
(b) You must cause any modified files to carry prominent notices
|
|
98
|
-
stating that You changed the files; and
|
|
99
|
-
|
|
100
|
-
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
-
that You distribute, all copyright, patent, trademark, and
|
|
102
|
-
attribution notices from the Source form of the Work,
|
|
103
|
-
excluding those notices that do not pertain to any part of
|
|
104
|
-
the Derivative Works; and
|
|
105
|
-
|
|
106
|
-
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
-
distribution, then any Derivative Works that You distribute must
|
|
108
|
-
include a readable copy of the attribution notices contained
|
|
109
|
-
within such NOTICE file, excluding those notices that do not
|
|
110
|
-
pertain to any part of the Derivative Works, in at least one
|
|
111
|
-
of the following places: within a NOTICE text file distributed
|
|
112
|
-
as part of the Derivative Works; within the Source form or
|
|
113
|
-
documentation, if provided along with the Derivative Works; or,
|
|
114
|
-
within a display generated by the Derivative Works, if and
|
|
115
|
-
wherever such third-party notices normally appear. The contents
|
|
116
|
-
of the NOTICE file are for informational purposes only and
|
|
117
|
-
do not modify the License. You may add Your own attribution
|
|
118
|
-
notices within Derivative Works that You distribute, alongside
|
|
119
|
-
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
-
that such additional attribution notices cannot be construed
|
|
121
|
-
as modifying the License.
|
|
122
|
-
|
|
123
|
-
You may add Your own copyright statement to Your modifications and
|
|
124
|
-
may provide additional or different license terms and conditions
|
|
125
|
-
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
-
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
-
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
-
the conditions stated in this License.
|
|
129
|
-
|
|
130
|
-
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
-
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
-
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
-
this License, without any additional terms or conditions.
|
|
134
|
-
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
-
the terms of any separate license agreement you may have executed
|
|
136
|
-
with Licensor regarding such Contributions.
|
|
137
|
-
|
|
138
|
-
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
-
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
-
except as required for reasonable and customary use in describing the
|
|
141
|
-
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
-
|
|
143
|
-
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
-
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
-
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
-
implied, including, without limitation, any warranties or conditions
|
|
148
|
-
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
-
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
-
appropriateness of using or redistributing the Work and assume any
|
|
151
|
-
risks associated with Your exercise of permissions under this License.
|
|
152
|
-
|
|
153
|
-
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
-
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
-
unless required by applicable law (such as deliberate and grossly
|
|
156
|
-
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
-
liable to You for damages, including any direct, indirect, special,
|
|
158
|
-
incidental, or consequential damages of any character arising as a
|
|
159
|
-
result of this License or out of the use or inability to use the
|
|
160
|
-
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
-
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
-
other commercial damages or losses), even if such Contributor
|
|
163
|
-
has been advised of the possibility of such damages.
|
|
164
|
-
|
|
165
|
-
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
-
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
-
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
-
or other liability obligations and/or rights consistent with this
|
|
169
|
-
License. However, in accepting such obligations, You may act only
|
|
170
|
-
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
-
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
-
defend, and hold each Contributor harmless for any liability
|
|
173
|
-
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
-
of your accepting any such warranty or additional liability.
|
|
175
|
-
|
|
176
|
-
END OF TERMS AND CONDITIONS
|
|
177
|
-
|
|
178
|
-
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
-
|
|
180
|
-
To apply the Apache License to your work, attach the following
|
|
181
|
-
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
-
replaced with your own identifying information. (Don't include
|
|
183
|
-
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
-
comment syntax for the file format. We also recommend that a
|
|
185
|
-
file or class name and description of purpose be included on the
|
|
186
|
-
same "printed page" as the copyright notice for easier
|
|
187
|
-
identification within third-party archives.
|
|
188
|
-
|
|
189
|
-
Copyright 2026 Dick Olsson
|
|
190
|
-
|
|
191
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
-
you may not use this file except in compliance with the License.
|
|
193
|
-
You may obtain a copy of the License at
|
|
194
|
-
|
|
195
|
-
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
-
|
|
197
|
-
Unless required by applicable law or agreed to in writing, software
|
|
198
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
-
See the License for the specific language governing permissions and
|
|
201
|
-
limitations under the License.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Zachary Rice
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@forwardimpact/libmock",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.10",
|
|
4
4
|
"description": "Shared mocks and test fixtures so every library and service tests the same way.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"test",
|
|
@@ -22,7 +22,8 @@
|
|
|
22
22
|
".": "./src/index.js",
|
|
23
23
|
"./mock": "./src/mock/index.js",
|
|
24
24
|
"./fixture": "./src/fixture/index.js",
|
|
25
|
-
"./runtime": "./src/runtime.js"
|
|
25
|
+
"./runtime": "./src/runtime.js",
|
|
26
|
+
"./expect": "./src/expect/index.js"
|
|
26
27
|
},
|
|
27
28
|
"files": [
|
|
28
29
|
"src/**/*.js",
|
|
@@ -31,6 +32,9 @@
|
|
|
31
32
|
"scripts": {
|
|
32
33
|
"test": "bun test test/*.test.js"
|
|
33
34
|
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@grpc/grpc-js": "^1.14.4"
|
|
37
|
+
},
|
|
34
38
|
"peerDependencies": {
|
|
35
39
|
"@forwardimpact/libtype": "*"
|
|
36
40
|
},
|
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dependency-free, runner-independent `expect()` shim. Replaces `expect` from
|
|
3
|
+
* `bun:test` so the test suite can run under `node --test` (which ships no
|
|
4
|
+
* `expect`) as well as `bun test`. Mirrors how `spy()` replaced `mock.fn`.
|
|
5
|
+
*
|
|
6
|
+
* Covers exactly the matcher surface the converged test files use: `toBe`,
|
|
7
|
+
* `toEqual`, `toMatchObject`, `toBeNull`, `toBeUndefined`, `toBeDefined`,
|
|
8
|
+
* `toBeTruthy`, `toBeGreaterThan`, `toBeGreaterThanOrEqual`,
|
|
9
|
+
* `toBeLessThanOrEqual`, `toHaveLength`, `toContain`, `toMatch`, `toThrow`,
|
|
10
|
+
* plus the `.not`, `.resolves`, and `.rejects` chain modifiers.
|
|
11
|
+
*
|
|
12
|
+
* Imports only from the Node standard library.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { AssertionError } from "node:assert";
|
|
16
|
+
import { isDeepStrictEqual } from "node:util";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Render a value for assertion messages without throwing on circular refs.
|
|
20
|
+
* @param {unknown} value - Value to stringify.
|
|
21
|
+
* @returns {string} Human-readable rendering.
|
|
22
|
+
*/
|
|
23
|
+
function show(value) {
|
|
24
|
+
if (typeof value === "string") {
|
|
25
|
+
return JSON.stringify(value);
|
|
26
|
+
}
|
|
27
|
+
if (typeof value === "bigint") {
|
|
28
|
+
return `${value}n`;
|
|
29
|
+
}
|
|
30
|
+
if (value instanceof Map) {
|
|
31
|
+
return `Map(${value.size})`;
|
|
32
|
+
}
|
|
33
|
+
if (value instanceof Set) {
|
|
34
|
+
return `Set(${value.size})`;
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
return JSON.stringify(value) ?? String(value);
|
|
38
|
+
} catch {
|
|
39
|
+
return String(value);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Throw or, when negated, swallow — the single choke point that makes `.not`
|
|
45
|
+
* invert every matcher. `pass` is the un-negated truth of the assertion.
|
|
46
|
+
* @param {boolean} pass - Whether the positive assertion holds.
|
|
47
|
+
* @param {boolean} negated - Whether `.not` is active.
|
|
48
|
+
* @param {string} message - Message for the failing (positive) direction.
|
|
49
|
+
* @param {string} [negatedMessage] - Message for the failing negated direction.
|
|
50
|
+
*/
|
|
51
|
+
function settle(pass, negated, message, negatedMessage) {
|
|
52
|
+
if (negated) {
|
|
53
|
+
if (pass) {
|
|
54
|
+
throw new AssertionError({
|
|
55
|
+
message: negatedMessage ?? `not: ${message}`,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (!pass) {
|
|
61
|
+
throw new AssertionError({ message });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const ASYMMETRIC = Symbol.for("libmock.expect.asymmetric");
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Whether `value` is an asymmetric matcher produced by `expect.any` / friends.
|
|
69
|
+
* @param {unknown} value - Candidate.
|
|
70
|
+
* @returns {boolean} True when `value` carries the asymmetric marker.
|
|
71
|
+
*/
|
|
72
|
+
function isAsymmetric(value) {
|
|
73
|
+
return (
|
|
74
|
+
value != null && typeof value === "object" && value[ASYMMETRIC] === true
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Structural equality that understands asymmetric matchers (`expect.any`).
|
|
80
|
+
* Falls back to `node:util.isDeepStrictEqual` when neither side carries one.
|
|
81
|
+
* @param {unknown} actual - Observed value.
|
|
82
|
+
* @param {unknown} expected - Expected value, possibly an asymmetric matcher.
|
|
83
|
+
* @returns {boolean} Whether they match.
|
|
84
|
+
*/
|
|
85
|
+
function equals(actual, expected) {
|
|
86
|
+
if (isAsymmetric(expected)) {
|
|
87
|
+
return expected.asymmetricMatch(actual);
|
|
88
|
+
}
|
|
89
|
+
if (expected === null || typeof expected !== "object") {
|
|
90
|
+
return Object.is(actual, expected) || isDeepStrictEqual(actual, expected);
|
|
91
|
+
}
|
|
92
|
+
if (actual === null || typeof actual !== "object") {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
if (Array.isArray(expected)) {
|
|
96
|
+
if (!Array.isArray(actual) || actual.length !== expected.length) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
return expected.every((e, i) => equals(actual[i], e));
|
|
100
|
+
}
|
|
101
|
+
// No asymmetric matcher anywhere → exact deep equality is correct and cheap.
|
|
102
|
+
if (!containsAsymmetric(expected)) {
|
|
103
|
+
return isDeepStrictEqual(actual, expected);
|
|
104
|
+
}
|
|
105
|
+
const keys = Object.keys(expected);
|
|
106
|
+
if (keys.length !== Object.keys(actual).length) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
return keys.every((k) => equals(actual[k], expected[k]));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Whether an expected value tree contains any asymmetric matcher.
|
|
114
|
+
* @param {unknown} value - Tree to scan.
|
|
115
|
+
* @returns {boolean} True if an asymmetric matcher is present.
|
|
116
|
+
*/
|
|
117
|
+
function containsAsymmetric(value) {
|
|
118
|
+
if (isAsymmetric(value)) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
if (value === null || typeof value !== "object") {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
if (Array.isArray(value)) {
|
|
125
|
+
return value.some(containsAsymmetric);
|
|
126
|
+
}
|
|
127
|
+
return Object.values(value).some(containsAsymmetric);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Deep subset match: every own enumerable key of `subset` matches the same key
|
|
132
|
+
* in `actual` (recursively for plain objects, asymmetric-aware).
|
|
133
|
+
* @param {unknown} actual - Candidate object.
|
|
134
|
+
* @param {unknown} subset - Expected subset.
|
|
135
|
+
* @returns {boolean} Whether `actual` contains `subset`.
|
|
136
|
+
*/
|
|
137
|
+
function matchesObject(actual, subset) {
|
|
138
|
+
if (isAsymmetric(subset)) {
|
|
139
|
+
return subset.asymmetricMatch(actual);
|
|
140
|
+
}
|
|
141
|
+
if (subset === null || typeof subset !== "object") {
|
|
142
|
+
return equals(actual, subset);
|
|
143
|
+
}
|
|
144
|
+
if (actual === null || typeof actual !== "object") {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
for (const key of Object.keys(subset)) {
|
|
148
|
+
const sv = subset[key];
|
|
149
|
+
const av = actual[key];
|
|
150
|
+
if (
|
|
151
|
+
sv !== null &&
|
|
152
|
+
typeof sv === "object" &&
|
|
153
|
+
!Array.isArray(sv) &&
|
|
154
|
+
!isAsymmetric(sv)
|
|
155
|
+
) {
|
|
156
|
+
if (!matchesObject(av, sv)) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
} else if (!equals(av, sv)) {
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Run a possibly-throwing function and apply the `toThrow` predicate.
|
|
168
|
+
* @param {() => unknown} fn - The thunk under test.
|
|
169
|
+
* @param {unknown} matcher - undefined, a string (substring), or a RegExp.
|
|
170
|
+
* @param {boolean} negated - Whether `.not` is active.
|
|
171
|
+
*/
|
|
172
|
+
function assertThrow(fn, matcher, negated) {
|
|
173
|
+
if (typeof fn !== "function") {
|
|
174
|
+
throw new AssertionError({
|
|
175
|
+
message: `toThrow expects a function, got ${show(fn)}`,
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
let thrown;
|
|
179
|
+
let didThrow = false;
|
|
180
|
+
try {
|
|
181
|
+
fn();
|
|
182
|
+
} catch (err) {
|
|
183
|
+
didThrow = true;
|
|
184
|
+
thrown = err;
|
|
185
|
+
}
|
|
186
|
+
if (!didThrow) {
|
|
187
|
+
settle(false, negated, "expected function to throw, but it did not");
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const message = thrown instanceof Error ? thrown.message : String(thrown);
|
|
191
|
+
let pass = true;
|
|
192
|
+
if (typeof matcher === "string") {
|
|
193
|
+
pass = message.includes(matcher);
|
|
194
|
+
} else if (matcher instanceof RegExp) {
|
|
195
|
+
pass = matcher.test(message);
|
|
196
|
+
}
|
|
197
|
+
settle(
|
|
198
|
+
pass,
|
|
199
|
+
negated,
|
|
200
|
+
`expected thrown message ${show(message)} to match ${show(matcher)}`,
|
|
201
|
+
`expected thrown message ${show(message)} not to match ${show(matcher)}`,
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Build the synchronous matcher object for an already-resolved actual value.
|
|
207
|
+
* @param {unknown} actual - The value under test.
|
|
208
|
+
* @param {boolean} negated - Whether `.not` is active.
|
|
209
|
+
* @returns {object} Matcher object.
|
|
210
|
+
*/
|
|
211
|
+
function matchers(actual, negated) {
|
|
212
|
+
return {
|
|
213
|
+
get not() {
|
|
214
|
+
return matchers(actual, !negated);
|
|
215
|
+
},
|
|
216
|
+
toBe(expected) {
|
|
217
|
+
settle(
|
|
218
|
+
Object.is(actual, expected),
|
|
219
|
+
negated,
|
|
220
|
+
`expected ${show(actual)} to be ${show(expected)}`,
|
|
221
|
+
`expected ${show(actual)} not to be ${show(expected)}`,
|
|
222
|
+
);
|
|
223
|
+
},
|
|
224
|
+
toEqual(expected) {
|
|
225
|
+
settle(
|
|
226
|
+
equals(actual, expected),
|
|
227
|
+
negated,
|
|
228
|
+
`expected ${show(actual)} to deep-equal ${show(expected)}`,
|
|
229
|
+
`expected ${show(actual)} not to deep-equal ${show(expected)}`,
|
|
230
|
+
);
|
|
231
|
+
},
|
|
232
|
+
toMatchObject(expected) {
|
|
233
|
+
settle(
|
|
234
|
+
matchesObject(actual, expected),
|
|
235
|
+
negated,
|
|
236
|
+
`expected ${show(actual)} to match object ${show(expected)}`,
|
|
237
|
+
`expected ${show(actual)} not to match object ${show(expected)}`,
|
|
238
|
+
);
|
|
239
|
+
},
|
|
240
|
+
toBeNull() {
|
|
241
|
+
settle(
|
|
242
|
+
actual === null,
|
|
243
|
+
negated,
|
|
244
|
+
`expected ${show(actual)} to be null`,
|
|
245
|
+
`expected ${show(actual)} not to be null`,
|
|
246
|
+
);
|
|
247
|
+
},
|
|
248
|
+
toBeUndefined() {
|
|
249
|
+
settle(
|
|
250
|
+
actual === undefined,
|
|
251
|
+
negated,
|
|
252
|
+
`expected ${show(actual)} to be undefined`,
|
|
253
|
+
`expected ${show(actual)} not to be undefined`,
|
|
254
|
+
);
|
|
255
|
+
},
|
|
256
|
+
toBeDefined() {
|
|
257
|
+
settle(
|
|
258
|
+
actual !== undefined,
|
|
259
|
+
negated,
|
|
260
|
+
`expected ${show(actual)} to be defined`,
|
|
261
|
+
`expected ${show(actual)} not to be defined`,
|
|
262
|
+
);
|
|
263
|
+
},
|
|
264
|
+
toBeTruthy() {
|
|
265
|
+
settle(
|
|
266
|
+
Boolean(actual),
|
|
267
|
+
negated,
|
|
268
|
+
`expected ${show(actual)} to be truthy`,
|
|
269
|
+
`expected ${show(actual)} not to be truthy`,
|
|
270
|
+
);
|
|
271
|
+
},
|
|
272
|
+
toBeGreaterThan(expected) {
|
|
273
|
+
settle(
|
|
274
|
+
actual > expected,
|
|
275
|
+
negated,
|
|
276
|
+
`expected ${show(actual)} to be greater than ${show(expected)}`,
|
|
277
|
+
`expected ${show(actual)} not to be greater than ${show(expected)}`,
|
|
278
|
+
);
|
|
279
|
+
},
|
|
280
|
+
toBeGreaterThanOrEqual(expected) {
|
|
281
|
+
settle(
|
|
282
|
+
actual >= expected,
|
|
283
|
+
negated,
|
|
284
|
+
`expected ${show(actual)} to be >= ${show(expected)}`,
|
|
285
|
+
`expected ${show(actual)} not to be >= ${show(expected)}`,
|
|
286
|
+
);
|
|
287
|
+
},
|
|
288
|
+
toBeLessThanOrEqual(expected) {
|
|
289
|
+
settle(
|
|
290
|
+
actual <= expected,
|
|
291
|
+
negated,
|
|
292
|
+
`expected ${show(actual)} to be <= ${show(expected)}`,
|
|
293
|
+
`expected ${show(actual)} not to be <= ${show(expected)}`,
|
|
294
|
+
);
|
|
295
|
+
},
|
|
296
|
+
toHaveLength(expected) {
|
|
297
|
+
const len = actual == null ? undefined : actual.length;
|
|
298
|
+
settle(
|
|
299
|
+
len === expected,
|
|
300
|
+
negated,
|
|
301
|
+
`expected length ${show(len)} to be ${show(expected)}`,
|
|
302
|
+
`expected length ${show(len)} not to be ${show(expected)}`,
|
|
303
|
+
);
|
|
304
|
+
},
|
|
305
|
+
toContain(expected) {
|
|
306
|
+
let pass = false;
|
|
307
|
+
if (typeof actual === "string") {
|
|
308
|
+
pass = actual.includes(expected);
|
|
309
|
+
} else if (Array.isArray(actual)) {
|
|
310
|
+
pass = actual.includes(expected);
|
|
311
|
+
} else if (actual instanceof Set) {
|
|
312
|
+
pass = actual.has(expected);
|
|
313
|
+
} else if (
|
|
314
|
+
actual != null &&
|
|
315
|
+
typeof actual[Symbol.iterator] === "function"
|
|
316
|
+
) {
|
|
317
|
+
pass = [...actual].includes(expected);
|
|
318
|
+
}
|
|
319
|
+
settle(
|
|
320
|
+
pass,
|
|
321
|
+
negated,
|
|
322
|
+
`expected ${show(actual)} to contain ${show(expected)}`,
|
|
323
|
+
`expected ${show(actual)} not to contain ${show(expected)}`,
|
|
324
|
+
);
|
|
325
|
+
},
|
|
326
|
+
toMatch(expected) {
|
|
327
|
+
const str = String(actual);
|
|
328
|
+
const pass =
|
|
329
|
+
expected instanceof RegExp
|
|
330
|
+
? expected.test(str)
|
|
331
|
+
: str.includes(String(expected));
|
|
332
|
+
settle(
|
|
333
|
+
pass,
|
|
334
|
+
negated,
|
|
335
|
+
`expected ${show(str)} to match ${show(expected)}`,
|
|
336
|
+
`expected ${show(str)} not to match ${show(expected)}`,
|
|
337
|
+
);
|
|
338
|
+
},
|
|
339
|
+
toThrow(expected) {
|
|
340
|
+
assertThrow(actual, expected, negated);
|
|
341
|
+
},
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Build the async matcher object for a promise-producing actual. Each matcher
|
|
347
|
+
* awaits the promise and applies the underlying matcher to the settled value.
|
|
348
|
+
* @param {unknown} actual - A promise (or thenable) under test.
|
|
349
|
+
* @param {boolean} negated - Whether `.not` is active.
|
|
350
|
+
* @param {"resolves" | "rejects"} mode - Which settlement to assert on.
|
|
351
|
+
* @returns {object} Matcher object whose methods return promises.
|
|
352
|
+
*/
|
|
353
|
+
function asyncMatchers(actual, negated, mode) {
|
|
354
|
+
const wrap = (apply) => async () => {
|
|
355
|
+
let value;
|
|
356
|
+
let rejected = false;
|
|
357
|
+
let rejection;
|
|
358
|
+
try {
|
|
359
|
+
value = await actual;
|
|
360
|
+
} catch (err) {
|
|
361
|
+
rejected = true;
|
|
362
|
+
rejection = err;
|
|
363
|
+
}
|
|
364
|
+
if (mode === "rejects") {
|
|
365
|
+
if (!rejected) {
|
|
366
|
+
throw new AssertionError({
|
|
367
|
+
message: "expected promise to reject, but it resolved",
|
|
368
|
+
});
|
|
369
|
+
}
|
|
370
|
+
return apply(rejection);
|
|
371
|
+
}
|
|
372
|
+
if (rejected) {
|
|
373
|
+
throw new AssertionError({
|
|
374
|
+
message: `expected promise to resolve, but it rejected with ${show(rejection)}`,
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
return apply(value);
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
const make =
|
|
381
|
+
(name) =>
|
|
382
|
+
(...args) => {
|
|
383
|
+
if (name === "toThrow") {
|
|
384
|
+
// `rejects.toThrow(x)` asserts the rejection's message; the settled value
|
|
385
|
+
// here is the rejection itself, so wrap it back into a throwing thunk.
|
|
386
|
+
return wrap((settled) => {
|
|
387
|
+
const thunk = () => {
|
|
388
|
+
throw settled;
|
|
389
|
+
};
|
|
390
|
+
assertThrow(thunk, args[0], negated);
|
|
391
|
+
})();
|
|
392
|
+
}
|
|
393
|
+
return wrap((settled) => matchers(settled, negated)[name](...args))();
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
return {
|
|
397
|
+
get not() {
|
|
398
|
+
return asyncMatchers(actual, !negated, mode);
|
|
399
|
+
},
|
|
400
|
+
toBe: make("toBe"),
|
|
401
|
+
toEqual: make("toEqual"),
|
|
402
|
+
toMatchObject: make("toMatchObject"),
|
|
403
|
+
toBeNull: make("toBeNull"),
|
|
404
|
+
toBeUndefined: make("toBeUndefined"),
|
|
405
|
+
toBeDefined: make("toBeDefined"),
|
|
406
|
+
toBeTruthy: make("toBeTruthy"),
|
|
407
|
+
toBeGreaterThan: make("toBeGreaterThan"),
|
|
408
|
+
toBeGreaterThanOrEqual: make("toBeGreaterThanOrEqual"),
|
|
409
|
+
toBeLessThanOrEqual: make("toBeLessThanOrEqual"),
|
|
410
|
+
toHaveLength: make("toHaveLength"),
|
|
411
|
+
toContain: make("toContain"),
|
|
412
|
+
toMatch: make("toMatch"),
|
|
413
|
+
toThrow: make("toThrow"),
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* Jest-style `expect`. Returns a matcher object with `.not`, `.resolves`, and
|
|
419
|
+
* `.rejects` chain modifiers plus the supported matchers.
|
|
420
|
+
* @param {unknown} actual - The value under test.
|
|
421
|
+
* @returns {object} Matcher object.
|
|
422
|
+
*/
|
|
423
|
+
export function expect(actual) {
|
|
424
|
+
const base = matchers(actual, false);
|
|
425
|
+
Object.defineProperties(base, {
|
|
426
|
+
resolves: {
|
|
427
|
+
get() {
|
|
428
|
+
return asyncMatchers(actual, false, "resolves");
|
|
429
|
+
},
|
|
430
|
+
},
|
|
431
|
+
rejects: {
|
|
432
|
+
get() {
|
|
433
|
+
return asyncMatchers(actual, false, "rejects");
|
|
434
|
+
},
|
|
435
|
+
},
|
|
436
|
+
});
|
|
437
|
+
return base;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Asymmetric matcher: matches any value constructed by / typed as `ctor`,
|
|
442
|
+
* for use inside `toEqual` / `toMatchObject`. Mirrors Jest/bun `expect.any`.
|
|
443
|
+
* @param {Function} ctor - A constructor or primitive wrapper (String, Number…).
|
|
444
|
+
* @returns {object} An asymmetric matcher.
|
|
445
|
+
*/
|
|
446
|
+
const PRIMITIVE_TYPEOF = new Map([
|
|
447
|
+
[String, "string"],
|
|
448
|
+
[Number, "number"],
|
|
449
|
+
[Boolean, "boolean"],
|
|
450
|
+
[BigInt, "bigint"],
|
|
451
|
+
[Object, "object"],
|
|
452
|
+
[Function, "function"],
|
|
453
|
+
]);
|
|
454
|
+
|
|
455
|
+
expect.any = (ctor) => ({
|
|
456
|
+
[ASYMMETRIC]: true,
|
|
457
|
+
asymmetricMatch(value) {
|
|
458
|
+
if (value == null) {
|
|
459
|
+
return false;
|
|
460
|
+
}
|
|
461
|
+
const primitive = PRIMITIVE_TYPEOF.get(ctor);
|
|
462
|
+
if (primitive) {
|
|
463
|
+
return typeof value === primitive || value instanceof ctor;
|
|
464
|
+
}
|
|
465
|
+
return value instanceof ctor;
|
|
466
|
+
},
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Asymmetric matcher: matches any non-null, non-undefined value.
|
|
471
|
+
* @returns {object} An asymmetric matcher.
|
|
472
|
+
*/
|
|
473
|
+
expect.anything = () => ({
|
|
474
|
+
[ASYMMETRIC]: true,
|
|
475
|
+
asymmetricMatch: (value) => value != null,
|
|
476
|
+
});
|
package/src/mock/git-client.js
CHANGED
|
@@ -8,16 +8,86 @@ const GIT_METHODS = [
|
|
|
8
8
|
"rebase",
|
|
9
9
|
"rebaseAbort",
|
|
10
10
|
"mergeOursStrategy",
|
|
11
|
+
"resetSoft",
|
|
12
|
+
"checkoutPaths",
|
|
13
|
+
"mergeAbort",
|
|
14
|
+
"unmergedPaths",
|
|
15
|
+
"isMidMerge",
|
|
16
|
+
"introducedByFile",
|
|
11
17
|
"commitAll",
|
|
12
18
|
"commitPaths",
|
|
13
19
|
"push",
|
|
20
|
+
"logByAuthor",
|
|
21
|
+
"diffRange",
|
|
22
|
+
"showFile",
|
|
14
23
|
"revListCount",
|
|
15
24
|
"configGet",
|
|
16
25
|
"configSet",
|
|
17
26
|
"aheadCount",
|
|
18
27
|
"remoteGetUrl",
|
|
28
|
+
"headBranch",
|
|
29
|
+
"refExists",
|
|
30
|
+
"mergeBaseExists",
|
|
31
|
+
"remoteBranchExists",
|
|
32
|
+
"fetchDeepen",
|
|
33
|
+
"pushPorcelain",
|
|
34
|
+
"remoteRefTip",
|
|
35
|
+
"isAncestor",
|
|
36
|
+
"statusPorcelain",
|
|
37
|
+
"revParse",
|
|
38
|
+
"diffNameStatus",
|
|
39
|
+
"stashDropBySha",
|
|
19
40
|
];
|
|
20
41
|
|
|
42
|
+
// A response descriptor models one git failure: an `Error` thrown as-is, or
|
|
43
|
+
// `{ throw: <message>, stderr?: <text> }` thrown as an Error carrying that
|
|
44
|
+
// stderr — mirroring how GitClient's `#runRaw` surfaces a failure (the real
|
|
45
|
+
// GitError exposes `.stderr`), so a caller inspecting stderr (e.g. to tell a
|
|
46
|
+
// push rejection from an auth failure) sees a faithful shape.
|
|
47
|
+
function isResponseDescriptor(value) {
|
|
48
|
+
return (
|
|
49
|
+
value instanceof Error ||
|
|
50
|
+
(value && typeof value === "object" && "throw" in value)
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// A configured response that is an array of response descriptors is a per-call
|
|
55
|
+
// sequence: consumed one entry per invocation, reusing the last entry once
|
|
56
|
+
// exhausted, so a test can express "push rejected on call 1, succeeds on call
|
|
57
|
+
// 2". An array that is plain data (e.g. a `logByAuthor` commit list) is returned
|
|
58
|
+
// whole — the descriptor check keeps data returns and failure sequences apart.
|
|
59
|
+
function makeResponder(configured) {
|
|
60
|
+
const isSequence =
|
|
61
|
+
Array.isArray(configured) && configured.some(isResponseDescriptor);
|
|
62
|
+
if (!isSequence) return () => configured;
|
|
63
|
+
let i = 0;
|
|
64
|
+
return () => configured[Math.min(i++, configured.length - 1)];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function resolveResponse(responder) {
|
|
68
|
+
const value = responder();
|
|
69
|
+
if (value instanceof Error) throw value;
|
|
70
|
+
if (value && typeof value === "object" && "throw" in value) {
|
|
71
|
+
const err = new Error(value.throw);
|
|
72
|
+
err.stderr = value.stderr ?? value.throw;
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Per-method default returns when no `responses[method]` is configured.
|
|
79
|
+
// Methods absent here default to a no-op success `{ stdout, stderr, exitCode }`.
|
|
80
|
+
const GIT_DEFAULTS = {
|
|
81
|
+
revListCount: 0,
|
|
82
|
+
aheadCount: 0,
|
|
83
|
+
logByAuthor: [],
|
|
84
|
+
diffRange: "",
|
|
85
|
+
showFile: null,
|
|
86
|
+
status: "",
|
|
87
|
+
configGet: "",
|
|
88
|
+
remoteGetUrl: "",
|
|
89
|
+
};
|
|
90
|
+
|
|
21
91
|
/**
|
|
22
92
|
* Creates a mock `GitClient` collaborator. Every method on the real
|
|
23
93
|
* `GitClient` surface is a spy returning a no-op success by default, or the
|
|
@@ -32,18 +102,14 @@ export function createMockGitClient({ responses = {} } = {}) {
|
|
|
32
102
|
const calls = [];
|
|
33
103
|
const client = { calls };
|
|
34
104
|
|
|
105
|
+
const responders = {};
|
|
35
106
|
for (const method of GIT_METHODS) {
|
|
107
|
+
if (method in responses)
|
|
108
|
+
responders[method] = makeResponder(responses[method]);
|
|
36
109
|
client[method] = spy(async (...args) => {
|
|
37
110
|
calls.push({ method, args });
|
|
38
|
-
if (method in
|
|
39
|
-
if (method
|
|
40
|
-
if (
|
|
41
|
-
method === "status" ||
|
|
42
|
-
method === "configGet" ||
|
|
43
|
-
method === "remoteGetUrl"
|
|
44
|
-
) {
|
|
45
|
-
return "";
|
|
46
|
-
}
|
|
111
|
+
if (method in responders) return resolveResponse(responders[method]);
|
|
112
|
+
if (method in GIT_DEFAULTS) return GIT_DEFAULTS[method];
|
|
47
113
|
return { stdout: "", stderr: "", exitCode: 0 };
|
|
48
114
|
});
|
|
49
115
|
}
|