Jest无法运行测试套件(非常简单的测试)。

3

我继承了一个使用@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 将会使测试独立运行?


你添加 Jest 支持做了哪些更改?你能分享一个问题重现的链接吗? - tony19
1个回答

0

文件 core-js/modules/es.error.cause.js 是核心 js 模块的一个 最近 添加。我在 Vue.js 项目中也遇到了同样的问题。我使用的 core-js 版本是 3.16.0

将 core-js 版本更新到当前版本(3.21.1)后,问题得到了解决。

奇怪的是,我在我的 node_modules 目录中对所有模块的 package.json 文件进行了 grep,却找不到任何比 3.16.0 更高版本的 core-js 依赖项。而且只有在 Windows 上运行测试套件时才会出现此错误,而在 Linux 上运行时则没有。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接