我用 vue-cli 生成了一个应用程序,并选择了 Jest 作为测试运行器。使用 HelloWorld 默认 vue 一切正常。我在“.vue”中添加了以下语句:
import aService from 'my-lib';
Run Code Online (Sandbox Code Playgroud)
当我使用以下命令运行项目时,它工作正常: npm run dev
但是,当我使用 运行测试时npm run test:unit,Cannot find module 'my-lib' from 'HelloWorld.vue'出现错误:
> vue-cli-service test:unit
FAIL tests/unit/example.spec.js
? Test suite failed to run
Cannot find module 'my-lib' from 'HelloWorld.vue'
93 |
94 | <script>
> 95 | import aService from 'my-lib';
| ^
96 |
97 | export default {
98 | name: 'HelloWorld',
Run Code Online (Sandbox Code Playgroud)
为什么 Jest 找不到我的模块?知道它运行良好并且它是一个 ES6 模块。
到目前为止我尝试过的:我将以下代码添加到我的jest.config.js文件中,但没有帮助:
module.exports = {
preset: '@vue/cli-plugin-unit-jest',
transformIgnorePatterns: [
"node_modules/(?!(my-lib)/)"
],
moduleDirectories: [
"node_modules"
],
};
Run Code Online (Sandbox Code Playgroud)