-
Hi there. Could you tell me the right way please? Log:
src/index.spec.ts: import { expect } from "@jest/globals";
import "./matcher";
test("foo", async () => {
expect("foo").toBeFoo();
}); src/matcher.ts: import { expect } from "@jest/globals";
declare global {
namespace jest {
interface Matchers<R> {
toBeFoo(): R;
}
}
}
expect.extend({
toBeFoo(received: string) {
if (received == "foo") {
return {
message: () => "OK",
pass: true,
};
} else {
return {
message: () => "NG",
pass: false,
};
}
},
}); package.json: {
"name": "tmp",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "jest"
},
"jest": {
"preset": "ts-jest",
"setupFilesAfterEnv": [
"<rootDir>/src/matcher.ts"
]
},
"dependencies": {},
"devDependencies": {
"typescript": "^4.1.3",
"@types/jest": "^26.0.20",
"jest": "^26.6.3",
"ts-jest": "^26.4.4"
}
}
(Node.js version: v14.2.0) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I think in this case, TypeScript doesn't recognize your custom typing. Maybe you can try to put the typing in a |
Beta Was this translation helpful? Give feedback.
-
Here is the minimum code that worked: src/index.spec.ts: import "./matcher";
test("foo", async () => {
expect("foo").toBeFoo();
}); src/global.d.ts: declare namespace jest {
interface Matchers<R> {
toBeFoo(): R;
}
} src/matcher.ts: import { expect } from "@jest/globals";
expect.extend({
toBeFoo(received: string) {
if (received == "foo") {
return {
message: () => "OK",
pass: true,
};
} else {
return {
message: () => "NG",
pass: false,
};
}
},
}); package.json: {
"name": "tmp",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "jest"
},
"jest": {
"preset": "ts-jest"
},
"dependencies": {},
"devDependencies": {
"typescript": "^4.1.3",
"@types/jest": "^26.0.20",
"jest": "^26.6.3",
"ts-jest": "^26.4.4"
}
} |
Beta Was this translation helpful? Give feedback.
Here is the minimum code that worked:
src/index.spec.ts:
src/global.d.ts:
src/matcher.ts:
package.json: