我想用 Jest 和@testing-lib/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作为绝对路径而不是相对路径导入。