在玩笑中导入 es 模块 deps

问题描述

我有一个关于 jest transformIgnorePatterns 选项的问题。我目前正在测试文件中导入一些由 esm(带有 import / export)编写的 deps。使主题简单:只需 @a/b @c/de

如何编写transformIgnorePatterns让babel将它们解析为commonjs?

目前,我收到这样的错误:

● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse,e.g. it's not plain JavaScript.

    By default,if Jest sees a Babel config,it will use that to transform your files,ignoring "node_modules".

    Here's what you can do:
     • If you are trying to use ECMAScript Modules,see https://jestjs.io/docs/en/ecmascript-modules for how to enable it.
     • To have some of your "node_modules" files transformed,you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.      

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    F:\project\vuepress-theme-hope-v2\node_modules\@vuepress\client\lib\index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export * from './app';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token 'export'



      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
      at Object.<anonymous> (packages/shared/lib/index.js:2689:27)

babel 配置

module.exports = {
  env: {
    test: {
      plugins: ["@babel/plugin-transform-modules-commonjs"],presets: ["env"],},};

玩笑配置

const { resolve } = require("path");

module.exports = {
  rootDir: resolve(__dirname),collectCoverage: true,testEnvironment: "node",globals: {
    "ts-jest": {
      tsconfig: "<rootDir>/tsconfig.json",transform: {
    "<rootDir>/node_modules/@vuepress/client/.*.js$": "babel-jest","\\.ts$": "ts-jest",transformIgnorePatterns: [
    "node_modules/(?!(@vuepress/client|@mr-hope/vuepress-shared)/)",],testMatch: ["<rootDir>/packages/**/__tests__/**/*.spec.ts"],preset: "ts-jest/presets/js-with-babel",};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...