【发布时间】:2019-12-20 16:43:15
【问题描述】:
我想用 Jest 和 @testing-lib/react-native 测试我的 Expo React Native 应用程序。
我在package.json 中设置了以下内容。
"jest": {
"preset": "jest-expo",
"moduleDirectories": [
"node_modules",
"test-utils"
]
},
我的文件夹结构如下:
├──node_modules/
├──test-utils/
├──src/
└──package.json
src/ 包含测试文件。我正在src/index.test.js 使用这个简单的测试来测试我的配置:
import { assert } from 'test-utils';
const sum = (a, b) => a + b;
describe('sum', () => {
assert({
given: 'two numbers',
should: 'add the numbers',
actual: sum(1, 3),
expected: 4,
});
});
assert 在test-utils/index.js 中的位置:
const assert = ({
given = undefined,
should = '',
actual = undefined,
expected = undefined,
} = {}) => {
it(`given ${given}: should ${should}`, () => {
expect(actual).toEqual(expected);
});
};
export { assert };
如果我运行测试,我会收到错误:
Cannot find module 'test-utils' from 'index.test.js'
这是为什么呢?我的意思是我已经配置了moduleDirectories 键? ???你怎么能解决这个问题?我真的很想能够将assert 作为绝对路径而不是相对路径导入。
【问题讨论】:
标签: javascript reactjs unit-testing jestjs react-testing-library