别名和Jest:无法从'src / index.js'中找到模块'global / http'

问题描述 投票:0回答:1

我在具有Rollup的项目中使用香草javascript。我正在使用,并且是第一次安装Jest,在对我的问题进行了一些研究之后,我有了一个基本的设置:

// jest.config.js
module.exports = {
  coverageDirectory: 'coverage',
  moduleFileExtensions: ['js'],
  moduleNameMapper: {
    '^applicationRoot/(.*)$': '<rootDir>/src/$1',
  },
};

我正在使用rollup-plugin-alias库,这是我的rollup.config.js中的一个示例

// rollup.config.js
// ...
alias({
      resolve: ['.js'],
      applicationRoot: __dirname + '/src',
      entries: [
        {
          find: 'global/http',
          replacement: `${__dirname}/src/global/http/index.js`,
        },
      ]})

我的库中的所有类都位于./ src目录

中。如果在我的输入文件(./ src / index.js)中,则有以下几行:
// ./src/index.js
import Http from 'global/http';
export class MyClass {

}

在我的测试文件中,我有:

// ./src/index.spec.js
import { MyClass } from './index';

在终端中出现以下错误:

 ● Test suite failed to run

    Cannot find module 'global/http' from 'src/index.js'

    Require stack:
      src/index.js
      src/index.spec.js
    > 1 | import Http from 'global/http';

我还应该做什么?感谢您的帮助或评论。

[[已编辑]

我创建了一个虚拟存储库,其结构与上述类似:

https://github.com/luenmuvel/dummy-project-rollup-jest-issues

在这种情况下,他们要做的就是克隆项目并运行“ npm i

”或“ yarn”。

在浏览器中可以正常工作:您只需要运行:

  • npm run build”或“ yarn build”]
  • 请参阅浏览器终端的输出。
  • 正在开玩笑不起作用。您只需要运行:-“ npm run t

”或“ yarn t”-等待输出。

我在具有Rollup的项目中使用香草javascript。我正在使用,并且是第一次安装Jest,在对我的问题进行了一些研究之后,我有了一个基本的设置:// jest.config.js模块....

javascript jestjs alias rollup rollupjs
1个回答
0
投票

据我所知,问题是在您的.spec文件中,您正在导入原始文件

© www.soinside.com 2019 - 2024. All rights reserved.