【问题标题】:Test suite failed to run Jest (very simple test)测试套件无法运行 Jest(非常简单的测试)
【发布时间】:2022-03-25 22:00:03
【问题描述】:

我继承了一个使用 @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', () => {

    });
    
});

我的玩笑配置是这样的,

    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 支持做了哪些更改?你能分享一个重现问题的链接吗?

标签: javascript vue.js unit-testing jestjs tdd


【解决方案1】:

core-js/modules/es.error.cause.js 文件是 core-js 模块的 recent 附加文件。我在 Vue.js 项目中遇到了完全相同的问题。我遇到错误的 core-js 版本是3.16.0

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

我遇到的奇怪的事情是,我在我的 node_modules 目录中 grep 了所有模块的 package.json 文件,并且找不到任何高于 3.16.0 版本的 core-js 依赖项。此外,我只在 Windows 上运行我的测试套件时遇到此错误,而不是在 Linux 上运行时。

【讨论】:

    猜你喜欢
    • 2019-10-21
    • 2022-01-05
    • 2020-11-25
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多