如何将全局命令添加到Jest中,例如describe及其它? [英] How to add global commands to Jest like describe and it?
问题描述
我正在为Jest写一个小的测试工具(仅供学习).它称为assertTruthy(msg, fn, args)
,需要一条消息,一个函数和一个参数,并且如果使用参数调用该函数时返回的内容为真,则应通过,否则返回失败.
I'm writing a little testing tool for Jest (just to learn). It is called assertTruthy(msg, fn, args)
, expects a message, a function and arguments and should pass if the thing that is returned when the function is invoked with the arguments is truthy and fail if its not.
我想将其添加到Jest中,以便您可以调用它而无需在每个环境中导入它.
I would like to add it to Jest, so that you could call it without importing it in every environment.
describe('someFn()', () => {
// like 'it', 'describe' and 'expect' it should just be available here
it('is a function', () => {
expect(typeop someFN).toEqual('Function');
});
assertTruthy('it should return true', someFn, 3, 4);
});
我知道Jest有 setupFiles
和setupFilesAfterEnv
但我不知道如何使用它们来实现这一目标.
I know Jest has setupFiles
and setupFilesAfterEnv
but I can't figure out how to use them to achieve this.
如何向Jest添加命令?
How do you add commands to Jest?
PS: On a single project basis (in CRA) I managed to do this like this:
// in src/setupFiles.js
const assertTruthy = // function definition ...
global.assertTruthy = assertTruthy
推荐答案
将全局函数添加到Jest
要向玩笑添加全局功能,您需要在配置中定义setupFiles
并将该功能附加到安装文件中的全局对象:
Add global functions to Jest
In order to add global functions to jest, you need to define setupFiles
in your configuration and attach that function to the global object in the setup file:
module.exports = {
// ...
setupFiles: ['<rootDir>/setupFile.js'],
// ...
};
因此,如果您想做与it
非常相似的事情,我建议您做这样的事情:
so if you want to do something very similar to it
, I would suggest you do something like this:
// /setupFile.js
// in order to change an existing function (not youre case):
global.it = function(description, fn) { /* ... */ };
// this is how you define a new function globally
global.assertTruthy = (message, func, ...args) => {
return global.it(message, () => {
expect(func(...args)).toBeTruthy();
});
// optional: implementing the same interface as `jest.It`
支持与jest.It
相同的界面
这是 Airbnb 库签出it
界面). todo
和skip
非常简单,因为您想要做的与原始操作完全相同.
Supporting the same interface as jest.It
Here's an example from the Airbnb library airbnb/jest-wrap
where they wrapped the describe function. If you want to implement the jest.It
interface, you'll also need to implement assertTruthy.todo
, assertTruthy.skip
, assertTruthy.only
, & assertTruthy.each
(check out the it
interface). todo
and skip
are pretty easy since you want to do exactly the same as the original ones.
对于each
& only
,我们需要在实现中更改it
函数.支持only
的简单方法是使用闭包,并从闭包的inpu传递正确的it
函数. each
实现起来可能会稍微复杂些.
For each
& only
, we need to change the it
function inside our implementation. A simple way to support only
is by using a closure and passing the correct it
function from the closure's inpu. each
can be a little more complex to implement.
// /setupFile.js
// normaly, use the jest `it` function
global.assertTruthy = assertTruthyCreator(it);
// bypass for todo and skip
global.assertTruthy.todo = global.it.todo;
global.assertTruthy.skip = global.it.skip;
// only calls the same function but uses `only` internaly
global.assertTruthy.only = assertTruthyCreator(it.only);
// special case which needs special implementation
// see usage below
global.assertTruthy.each = assertTruthyCreator(it.each, true);
function assertTruthyCreator(itFunc, withTable) {
if (withTable) {
return (message, func, ...args) => {
return itFunc(args)(message, (...caseArgs) => {
expect(func(...caseArgs)).toBeTruthy();
});
};
}
return (message, func, ...args) => {
return itFunc(message, () => {
expect(func(...args)).toBeTruthy();
});
};
}
// usage:
assertTruthy.each(
'add numbers',
(a, b) => a + b,
[2, 4],
[4, 5],
[7, 9]);
如何在测试文件中使用
如果您正在使用打字稿来编写笑话测试,那么您要做的第一件事就是declare
某个地方的新函数:
How to use in test files
If you're using typescript for writing jest test, the first thing you'll need to do is declare
your new function somewhere:
interface IGenericFunction {
(...args: any[]): any;
}
declare const assertTruthy: (message: string, func: IGenericFunction, ...args: any[]) => any;
使用javascript,您可以跳过这一步.
With javascript, you can skip that step.
之后,就像使用describe
和it
一样使用它:
After that, just use it like you use describe
and it
:
const funcToTest = (a: number, b: number) => a + b;
describe("Test Suite", () => {
assertTruthy('this ran with assertTruthy', funcToTest, 5, 3);
test("another test", () => {
// ...
});
});
和开玩笑会将其视为其他任何it
函数
and jest will treat this as any other it
function
如果要以此创建一个库,基本上可以将node_modules
路径传递给setupFiles
数组.
If you want to create a library from this, you can basically just pass a node_modules
path to the setupFiles
array.
例如,使用此存储库,您可以执行以下操作:
For example, with this repository, you can do as follows:
- 使用
npm install --save-dev @kibibit/jest-utils
进行安装
- 将以下内容添加到您的笑话配置
- 使用如上所述的功能
- Install using
npm install --save-dev @kibibit/jest-utils
- Add the following to your jest configuration
- Use the function as described above
module.exports = {
// ...
setupFiles: ['node_modules/@kibibit/jest-utils/lib/jest-utils.js'],
// ...
};
,它的作用应与本地导入相同.
and it should work the same as importing it locally.
这篇关于如何将全局命令添加到Jest中,例如describe及其它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!