【问题标题】:How do I get jest to to work with ES6 dependencies如何让我开玩笑地使用 ES6 依赖项
【发布时间】:2020-01-14 21:00:05
【问题描述】:

所以我有一个依赖包,我将其拉入我的 node_modules 文件夹。这个包有一个像这样的导出

        ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './client';

为了解决这个问题,我使用https://github.com/standard-things/esm。在我的节点加载器中

node -r esm index.js

但是,这不适用于我使用 Jest 的测试。

我似乎无法弄清楚如何让 Jest 为我转换这些导入。我尝试了很多东西,我的配置文件的当前状态是。

// babel.config.js
// babel.config.js
module.exports = {
    presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
    plugins: ['@babel/plugin-transform-modules-commonjs'],
};


const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');

module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    testMatch: ['<rootDir>/tests/**/*.{ts,js}'],
    testPathIgnorePatterns: ['global.d.ts', 'utils.ts', '<rootDir>/node_modules/'], // tried with and without this
    moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
};

仍然不断收到该错误。任何人都有一个建议。

【问题讨论】:

标签: javascript jestjs babeljs


【解决方案1】:

transformIgnorePatterns 默认为 ["/node_modules/"],这意味着默认情况下,node_modules 中的任何内容都不会在 Jest 运行时进行转换。

如果您在node_modules 中有一个依赖项需要在运行单元测试之前进行转换,那么您需要在Jest 配置中使用transformIgnorePatterns 将其列入白名单:

"transformIgnorePatterns": [
  "node_modules/(?!(module-that-needs-to-be-transformed)/)"
]

【讨论】:

  • 哇,我真的应该更仔细地阅读我的代码。我正在设置“testPathIgnorePatterns”而不是“transformIgnorePatterns”。谢谢这解决了我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 2019-02-28
  • 2021-07-14
相关资源
最近更新 更多