jestjs - 如何在玩笑中配置所有 cmmon 文件?
问题描述
如何在 jest.config 中加载所有常用文件
如何加载所有常见文件和 3td 方库,如 jquery
{
"name": "my-project",
"jest": {
setupFiles:[../src/assert]
}
}
解决方案
您可以在. jest.config.js
将jquery
and分配moment
给 node.jsglobal
对象。global.$
然后,您可以使用和在每个测试用例中获取它们global.moment
。
例如
setup.js
:
const jquery = function () {
return "I'm fake jquery";
};
const moment = function () {
return "I'm fake moment";
};
global.$ = jquery;
global.moment = moment;
jest.config.js
:
module.exports = {
preset: 'ts-jest/presets/js-with-ts',
testEnvironment: 'enzyme',
setupFilesAfterEnv: [
'jest-enzyme',
'./jest.setup.js',
],
setupFiles: [
'/Users/ldu020/workspace/github.com/mrdulin/react-apollo-graphql-starter-kit/stackoverflow/61727628/setup.js',
],
testMatch: ['**/?(*.)+(spec|test).[jt]s?(x)'],
verbose: true,
};
a.test.js
:
describe('61727628', () => {
describe('a', () => {
it('should pass', () => {
console.log('global.$:', global.$);
console.log('global.moment:', global.moment);
expect(1 + 1).toBe(2);
});
});
});
b.test.js
:
describe('61727628', () => {
describe('b', () => {
it('should pass', () => {
console.log('global.$:', global.$);
console.log('global.moment:', global.moment);
expect(1 + 1).toBe(2);
});
});
});
单元测试结果:
PASS stackoverflow/61727628/a.test.js
61727628
a
✓ should pass (28ms)
console.log
global.$: function () {
return "I'm fake jquery";
}
at Object.<anonymous> (stackoverflow/61727628/b.test.js:4:15)
console.log
global.$: function () {
return "I'm fake jquery";
}
at Object.<anonymous> (stackoverflow/61727628/a.test.js:4:15)
console.log
global.moment: function () {
return "I'm fake moment";
}
at Object.<anonymous> (stackoverflow/61727628/a.test.js:5:15)
console.log
global.moment: function () {
return "I'm fake moment";
}
at Object.<anonymous> (stackoverflow/61727628/b.test.js:5:15)
PASS stackoverflow/61727628/b.test.js
61727628
b
✓ should pass (28ms)
Test Suites: 2 passed, 2 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 6.026s, estimated 16s
推荐阅读
- flutter - 尝试在颤振中进行改造并在制作restclient时出错
- android - 无法从库模块中引用 xml 中的样式和字体资源
- python - 一个文件中的全局变量在另一个文件中未定义
- php - 从 pdf 创建缩略图
- php - 带有文件名的Htaccess重定向查询字符串?wordpress 干扰
- c++ - 如何使用 std::vector 更改缓冲区数据?
- javascript - React hooks 回调引用模式
- python-3.x - 根据列表的值过滤python3中的熊猫数据框
- jquery - 单击时自动选择文本
- python - 如何从 blob 容器中正确延迟加载图像?