transformIgnorePatterns

transformIgnorePatterns

本文介绍了我如何开玩笑地使用ES6依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个依赖项程序包,正在将其拉入node_modules文件夹.此程序包具有这样的导出功能

So I have a dependency package that I am pulling into my node_modules folder. This package has a export in it like this

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

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

In order to get around this I use https://github.com/standard-things/esm. In my node loader

node -r esm index.js .

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

However this does not work with my tests which are using Jest.

我似乎无法弄清楚如何让Jest为我转换这些导入.我已经尝试了很多事情,并且配置文件的当前状态是.

I cant seem to figure out how to just get Jest to transform these imports for me. I have tried so many things, and the current state of my configuration files is.

// 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>/' }),
};

仍然继续收到该错误.任何人都有建议.

Still keep getting that error. Anyone have a suggestion.

推荐答案

transformIgnorePatterns 默认为 ["/node_modules/"] ,这意味着默认情况下,当 Jest 运行.

transformIgnorePatterns defaults to ["/node_modules/"] which means that by default nothing in node_modules gets transformed when Jest runs.

如果在运行单元测试之前需要转换 node_modules 中的依赖项,则需要在 Jest 配置:

If you have a dependency in node_modules that needs to be transformed before running your unit tests then you will need to whitelist it with transformIgnorePatterns in your Jest config:

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

这篇关于我如何开玩笑地使用ES6依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:47