模块方向键不允许导入我的测试实用程序
原学程将引见模块偏向键没有许可导进我的尝试适用法式的处置办法,这篇学程是从其余处所瞅到的,而后减了1些海外法式员的疑问与解问,愿望能对于您有所赞助,佳了,上面开端进修吧。
成绩描写
我想用Jest以及@testing-lib/react-native
尝试我的EXPO Reaction 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(一, 三),
expected: 四,
});
});
个中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
导进为相对途径,而没有是绝对途径。
推举谜底
我找到懂得决计划。今朝这些文档便是欠好。
假如您有此文件夹构造
├──node_modules/
├──src/
├──utils/
└──package.json
而后,您愿望将模块目次定名为:
"jest": {
"preset": "jest-expo",
"setupFilesAfterEnv": [
"@testing-library/react-native/cleanup-after-each"
],
"moduleDirectories": [
"node_modules",
"utils"
]
},
而后在utils/
内,您愿望具有.js
文件test-utils.js
。而后您不妨在尝试中从test-utils
导进。
是以,症结是您从中导出的模块的称号必需是.js
文件的称号。而且您搁进moduleDirectories
的文件夹必需包括此.js
文件。
佳了闭于模块偏向键没有许可导进我的尝试适用法式的学程便到这里便停止了,愿望趣模板源码网找到的这篇技巧文章能赞助到年夜野,更多技巧学程不妨在站内搜刮。