为什么jest不处理ES模块依赖项?

von4xj4u  于 2023-02-20  发布在  Jest
关注(0)|答案(1)|浏览(222)

我的软件包导入了一个名为c2pa的ESM软件包。当我运行jest时,我得到了如下错误:

Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

我相信ECMAScript模块的文档是针对我的包是否为ESM的,但我遇到了依赖性问题。
我还更新了我的Jest配置,以尝试错误和Jest gives an error: "SyntaxError: Unexpected token export"中提到的transformIgnorePatterns覆盖。

const babelConfig = require('../../babel.config.json');

module.exports = {
  preset: 'ts-jest/presets/js-with-babel',
  testEnvironment: 'jsdom',
  roots: ['tests', 'src'],
  collectCoverageFrom: ['src/**/*.ts'],
  coveragePathIgnorePatterns: ['__mocks__'],
  moduleNameMapper: {
    '^uxp$': '<rootDir>/../../dependency-mocks/uxp.js',
  },
  transformIgnorePatterns: ['/node_modules/(?!c2pa)/'],
  globals: {
    'ts-jest': {
      babelConfig,
    },
    tsConfig: {
      declaration: false,
    },
    PLUGIN_REV: '__pluginRev__',
  },
  setupFilesAfterEnv: ['./tests/test.setup.js'],
};

我还尝试了一个巴别塔插件,在链接的SO问题中提到。

"env": {
    "test": {
      "plugins": ["@babel/plugin-transform-modules-commonjs"]
    }
  },

我不确定是否还有其他的巴别塔配置我可能会错过这里。显然是有什么地方出错了!

zbsbpyhn

zbsbpyhn1#

除了ignorePattern,我还更新了预设:
preset: 'ts-jest/presets/js-with-ts',
根本不需要变更babel配置。

相关问题