简体   繁体   English

vue.config.js configureWebpack 未在单元测试中使用(vue 3)

[英]vue.config.js configureWebpack not used in unit tests (vue 3)

In my vue config I have the following:在我的 vue 配置中,我有以下内容:

configureWebpack: {
  resolve: {
    alias: {
      react: path.resolve(__dirname, 'composition/react'),
      hooks: path.resolve(__dirname, 'composition'),
    },
  },
},

In my component I have the following:在我的组件中,我有以下内容:

import { useEffect, useState } from 'react';

This is so I can re use some of the Vue code in React projects.这样我就可以在 React 项目中重新使用一些 Vue 代码。 This works when running and building the project but not when testing.这在运行和构建项目时有效,但在测试时无效。 When unit testing I get the error: Cannot find module 'react' from 'useCategories.js' , the above import is from useCategories.单元测试时出现错误: Cannot find module 'react' from 'useCategories.js' ,上面的导入来自 useCategories。

The following is in the jest.config.js:以下是在 jest.config.js 中:

module.exports = {
  preset: '@vue/cli-plugin-unit-jest',
  transform: {
    '^.+\\.vue$': 'vue-jest'
  }
}

If unit testing in vue is ignoring values from vue.config.js then how can I set the webpack path resolve values for testing.如果 vue 中的单元测试忽略了 vue.config.js 中的值,那么我该如何设置 webpack 路径解析值以进行测试。 I would rather not repeat these values but if it's a JavaScript file I guess I can import it from the same file in different configs.我宁愿不重复这些值,但如果它是一个 JavaScript 文件,我想我可以从不同配置的同一个文件导入它。

Webpack isn't used in Jest, this would make module testing impractical, so Webpack config cannot affect anything. Webpack 没有在 Jest 中使用,这会使模块测试变得不切实际,因此 Webpack 配置不会影响任何东西。

As the documentation explains, it's necessary to provide module mapping in Jest config as well, eg:正如文档所解释的那样,也有必要在 Jest 配置中提供模块映射,例如:

moduleNameMapper: {
  '^react$': '<rootDir>/composition/react',
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM