我正在尝试在Typescript中为Jest添加自定义匹配器。这工作正常,但我不能让Typescript识别扩展 Matchers
。
myMatcher.ts
export default function myMatcher (this: jest.MatcherUtils, received: any, expected: any): { pass: boolean; message (): string; } {
const pass = received === expected;
return {
pass: pass,
message: () => `expected ${pass ? '!' : '='}==`,
}
}
myMatcher.d.ts
declare namespace jest {
interface Matchers {
myMatcher (expected: any): boolean;
}
}
someTest.ts
import myMatcher from './myMatcher';
expect.extend({
myMatcher,
})
it('should work', () => {
expect('str').myMatcher('str');
})
tsconfig.json
{
"compilerOptions": {
"outDir": "./dist/",
"moduleResolution": "node",
"module": "es6",
"target": "es5",
"lib": [
"es7",
"dom"
]
},
"types": [
"jest"
],
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist",
"doc",
"**/__mocks__/*",
"**/__tests__/*"
]
}
在someTests.ts中,我收到错误
error TS2339: Property 'myMatcher' does not exist on type 'Matchers'
我已多次阅读Microsoft文档,但我无法弄清楚如何使用全局可用类型(未导出)进行命名空间合并。
将它放在来自jest的index.d.ts中工作正常,但对于快速变化的代码库和多方扩展的类不是一个好的解决方案。
好的,这里有几个问题
当源文件(.ts
要么 .tsx
)文件和声明文件(.d.ts
)文件都是模块解析的候选者,就像这里的情况一样,编译器将解析源文件。
您可能有两个文件,因为您要导出值并修改全局对象 jest
。但是,您不需要两个文件,因为TypeScript具有一个特定的构造,用于从模块中扩充全局范围。也就是说,您需要的只是以下内容 .ts
文件
myMatcher.ts
// use declare global within a module to introduce or augment a global declaration.
declare global {
namespace jest {
interface Matchers {
myMatcher: typeof myMatcher;
}
}
}
export default function myMatcher<T>(this: jest.MatcherUtils, received: T, expected: T) {
const pass = received === expected;
return {
pass,
message: () => `expected ${pass ? '!' : '='}==`
}
}
也就是说,如果你有这样一个模块,那么执行全局是一个好习惯 突变 和全球类型 增强 在同一个文件中。鉴于此,我会考虑将其重写如下
myMatcher.ts
// no declare global wrapper because this is not a module
declare namespace jest {
interface Matchers {
myMatcher: typeof myMatcher;
}
}
function myMatcher<T>(this: jest.MatcherUtils, received: T, expected: T) {
const pass = received === expected;
return {
pass,
message: () => `expected ${pass ? '!' : '='}==`
}
}
expect.extend({
myMatcher
});
someTest.ts
import './myMatcher';
it('should work', () => {
expect('str').myMatcher('str');
});
好的,这里有几个问题
当源文件(.ts
要么 .tsx
)文件和声明文件(.d.ts
)文件都是模块解析的候选者,就像这里的情况一样,编译器将解析源文件。
您可能有两个文件,因为您要导出值并修改全局对象 jest
。但是,您不需要两个文件,因为TypeScript具有一个特定的构造,用于从模块中扩充全局范围。也就是说,您需要的只是以下内容 .ts
文件
myMatcher.ts
// use declare global within a module to introduce or augment a global declaration.
declare global {
namespace jest {
interface Matchers {
myMatcher: typeof myMatcher;
}
}
}
export default function myMatcher<T>(this: jest.MatcherUtils, received: T, expected: T) {
const pass = received === expected;
return {
pass,
message: () => `expected ${pass ? '!' : '='}==`
}
}
也就是说,如果你有这样一个模块,那么执行全局是一个好习惯 突变 和全球类型 增强 在同一个文件中。鉴于此,我会考虑将其重写如下
myMatcher.ts
// no declare global wrapper because this is not a module
declare namespace jest {
interface Matchers {
myMatcher: typeof myMatcher;
}
}
function myMatcher<T>(this: jest.MatcherUtils, received: T, expected: T) {
const pass = received === expected;
return {
pass,
message: () => `expected ${pass ? '!' : '='}==`
}
}
expect.extend({
myMatcher
});
someTest.ts
import './myMatcher';
it('should work', () => {
expect('str').myMatcher('str');
});
一个简单的方法是:
customMatchers.ts
declare global {
namespace jest {
interface Matchers<R> {
// add any of your custom matchers here
toBeDivisibleBy: (argument: number) => {};
}
}
}
// this will extend the expect with a custom matcher
expect.extend({
toBeDivisibleBy(received: number, argument: number) {
const pass = received % argument === 0;
if (pass) {
return {
message: () => `expected ${received} not to be divisible by ${argument}`,
pass: true
};
} else {
return {
message: () => `expected ${received} to be divisible by ${argument}`,
pass: false
};
}
}
});
my.spec.ts
import "path/to/customMatchers";
test('even and odd numbers', () => {
expect(100).toBeDivisibleBy(2);
expect(101).not.toBeDivisibleBy(2);
});