我继承了一个使用@vue/cli创建的代码库,最初该项目没有单元测试,现在我正在尝试添加它,到目前为止,我已经创建了一个非常简单的测试,并将jest添加到了应用程序中。
我的简单测试如下所示:
import { createLocalVue, mount, shallowMount } from '@vue/test-utils';
import ModalRefactored from '../../src/components/modal2/ModalRefactored.vue';
const localVue = createLocalVue();
describe('ModalRefactored', () => {
let storeMocks, wrapper;
beforeEach(() => {
// Create a fresh store and wrapper instance for every test case.
wrapper = shallowMount(ModalRefactored, {
localVue
});
});
test('It should render a modal when open', () => {
});
test('It should not render an overlay when not open', () => {
});
test('It should close whn the user clicks the close button', () => {
});
test('It should close when the user presses the esc key', () => {
});
});
我的 Jest 配置如下:
module.exports = {
preset: '@vue/cli-plugin-unit-jest'
}
当我运行
yarn test:unit
时,会得到以下结果:ests/unit/ModalRefactored.spec.js
● 测试套件无法运行
Cannot find module 'core-js/modules/es.error.cause.js' from 'Api.js'
However, Jest was able to find:
'./Api.d.ts'
'./Api.js'
'./Api.js.map'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'jsx', 'json', 'vue'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
However, Jest was able to find:
'../core/IPPComponent.js'
You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'jsx', 'json', 'vue'].
See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string
10 | this.method = "";
11 | this.code = 0;
> 12 | this.statusCode = 0;
| ^
13 | this.httpCode = 0;
14 | this.error = {};
15 | this.httpText = "";
at Resolver.resolveModule (../../node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (../js/dist/Api.js:12:1)
我不知道问题出在哪里?我猜想使用 shallowMount
将会使测试独立运行?