admin管理员组文章数量:1429841
I need to add some type checker to the Jest. It must look somehow like expect(someVar).toBeType('string')
and expect(someVar).toBeType(['string', 'object'])
.
I tried to add some checker-helper, but it looks a little bit ugly.
const toBeType = (arg:any, type:string) => {
const argType = typeof arg;
if (argType !== type)
throw new Error(`Expected '${type}' but got '${argType}' for the '${arg}'.`);
};
I want to add similar functionality to the jest
namespace to have ability to call type checker like expect(someVar).toBeType('boolean')
.
I need to add some type checker to the Jest. It must look somehow like expect(someVar).toBeType('string')
and expect(someVar).toBeType(['string', 'object'])
.
I tried to add some checker-helper, but it looks a little bit ugly.
const toBeType = (arg:any, type:string) => {
const argType = typeof arg;
if (argType !== type)
throw new Error(`Expected '${type}' but got '${argType}' for the '${arg}'.`);
};
I want to add similar functionality to the jest
namespace to have ability to call type checker like expect(someVar).toBeType('boolean')
.
- The piler (assuming your tests are in typescript too and run by ts-jest) will enforce type safety, no need to add assertions. – Jared Smith Commented Aug 30, 2019 at 8:28
1 Answer
Reset to default 4I resolved this problem this way. To add functionality to the Jest we should use expect.extend({...})
. So, to add toBeType
method to the Jest we should write this code to some setupTests.js
file:
// setupTests.js
expect.extend({
/**
* @param {*} received
* @param {string|string[]} arg
* @return {{pass:boolean,message:(function():string)}}
*/
toBeType(received, arg) {
const isCorrectType = arg => {
const receivedType = typeof received;
const checkForSingle = arg => {
const type = receivedType === 'object'
? Array.isArray(received)
? 'array'
: receivedType
: receivedType;
return type === arg;
};
const checkForArr = arg => {
const reducer = (prev, curr) => prev
|| isCorrectType(curr).isCorrect;
return arg.reduce(reducer, false);
};
return {
receivedType,
isCorrect: Array.isArray(arg)
? checkForArr(arg)
: checkForSingle(arg)
};
};
const {isCorrect, receivedType} = isCorrectType(arg);
return {
pass: isCorrect,
message: () => {
const toBe = Array.isArray(arg)
? arg.join(`' or '`)
: arg;
return `Expected '${received}' of '${receivedType}' type to be of '${toBe}' type(s)`;
}
};
}
});
Don't forget to add setupTests.js
to the jest.config.js
file as follows:
// jest.config.js
module.exports = {
...your_configurations...
setupFilesAfterEnv: ['<rootDir>/setupTests.js'],
};
Also we have to extend global.d.ts
file to say interpreter we have toBeType
method at the extend
namespace (it is required if only you are using TypeScript). Here is code we have to add to global.d.ts
:
// global.d.ts
declare namespace jest {
interface Matchers<R> {
toBeType(type:string|string[]);
}
}
This code says: get jest
namespace and extend Matchers<R>
interface with the toBeType
method. (You can look at Matchers<R>
interface implementation at the @types/jest
node module.)
本文标签: javascriptHow to add types checker for the Jest in the TypeScript appStack Overflow
版权声明:本文标题:javascript - How to add types checker for the Jest in the TypeScript app? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745515256a2661497.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论