Skip to content

Commit 28c56fb

Browse files
authored
move custom jest matcher to new package (#541)
Signed-off-by: Brian DeHamer <[email protected]>
1 parent c78bc0f commit 28c56fb

File tree

15 files changed

+483
-84
lines changed

15 files changed

+483
-84
lines changed

.changeset/happy-cycles-guess.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

package-lock.json

Lines changed: 41 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/client/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const base = require('../../jest.config.base');
1818
module.exports = {
1919
...base,
2020
displayName: 'client',
21-
setupFilesAfterEnv: ['./jest.setup.ts'],
21+
setupFilesAfterEnv: ['./jest.setup.ts', '@sigstore/jest/all'],
2222
testPathIgnorePatterns: [
2323
'<rootDir>/dist/',
2424
'<rootDir>/src/__tests__/__fixtures__',

packages/client/jest.setup.ts

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,5 @@ limitations under the License.
1515
*/
1616
console.error = jest.fn();
1717

18-
const result = (pass: boolean, msg: string) => ({ pass, message: () => msg });
19-
const pass = (msg: string) => result(true, msg);
20-
const fail = (msg: string) => result(false, msg);
21-
22-
const customMatchersObj = {
23-
// The promise attribute is necessary for the type checking to work correctly
24-
// in the matchers below. It is not used in the matchers themselves as the
25-
// functions are invoked with `this` bound to the Jest matchers object.
26-
promise: undefined,
27-
28-
// Matcher for checking that a function throws an error of a specific type
29-
// with a specific `code` property
30-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
31-
toThrowWithCode(received: any, type: any, code: string) {
32-
const isFromReject = this && this.promise === 'rejects';
33-
34-
// Must be called with either a rejected promise, or a function to be
35-
// invoked
36-
if ((!received || typeof received !== 'function') && !isFromReject) {
37-
return fail(`Excpected ${received} to be a function`);
38-
}
39-
40-
// Type must be some kind of class (e.g. Error)
41-
if (!type || typeof type !== 'function') {
42-
return fail(`Type argument must be a function, received ${type}`);
43-
}
44-
45-
if (typeof code !== 'string') {
46-
return fail(`Code argument must be a string, received ${code}`);
47-
}
48-
49-
// Gather the error, either from the rejected promise or by invoking the
50-
// supplied function
51-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
52-
let error: (object & { code?: string; name?: string }) | null = null;
53-
if (isFromReject) {
54-
error = received;
55-
} else {
56-
try {
57-
received();
58-
} catch (e) {
59-
error = e as object;
60-
}
61-
}
62-
63-
if (!error) {
64-
return fail(`Received function did not throw`);
65-
}
66-
67-
if (!(error instanceof type)) {
68-
return fail(
69-
`Expected function to throw ${type.name}, but got ${error.name}`
70-
);
71-
}
72-
73-
if (error.code !== code) {
74-
return fail(
75-
`Expected error to have code "${code}", but got "${error.code}"`
76-
);
77-
}
78-
return pass(`Expected function to throw ${type.name} with code ${code}`);
79-
},
80-
};
81-
82-
// Strip out the dummy `promise` attribute from the custom matchers object
83-
// before passing it to Jest's `expect.extend` function
84-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
85-
const { promise: _, ...customMatchers } = customMatchersObj;
86-
expect.extend(customMatchers);
87-
8818
// Export something so we look like a an importable module
8919
export {};

packages/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
},
3232
"devDependencies": {
3333
"@sigstore/rekor-types": "^1.0.0",
34+
"@sigstore/jest": "^0.0.0",
3435
"@total-typescript/shoehorn": "^0.1.0",
3536
"@tufjs/repo-mock": "^1.1.0",
3637
"@types/make-fetch-happen": "^10.0.0",
37-
"@types/sigstore-jest-extended": "^0.0.0",
3838
"@types/node": "^20.2.5",
3939
"json-schema-to-typescript": "^13.0.0",
4040
"nock": "^13.2.4",

0 commit comments

Comments
 (0)