You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Jest到27.0.6后报transform需导出process函数错误如何解决

Jest 27.0.6升级后运行测试报错TypeError修复方案

报错信息

升级jest到27.0.6版本后,运行npm test触发如下报错:

jest --config jest.config.js

TypeError: Jest: a transform must export a `process` or  `processAsync` function.

相关版本信息

"jest": "^27.0.6",
"ts-jest": "^27.0.0",

jest配置

module.exports = {
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  transform: {
    '^.+\\.(js?|tsx?)$': 'ts-jest',
    '^.+\\.(scss?|css?)$': '<rootDir>/__mocks__/fileMock.js',
  },
  testRegex: '__tests__.*test.(ts|tsx)$',
  testPathIgnorePatterns: ['<rootDir>/node_modules/'],
  testURL: 'http://localhost/',

  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
  modulePaths: ['src'],

  // 该配置用于修复CSS和加载gif资源引发的错误
  moduleNameMapper: {
    '^.+\\.(styl|css|scss)': 'identity-obj-proxy',
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga|styl|css)$':
      '<rootDir>/__mocks__/fileMock.js',
  },

  // mapCoverage: true,
  coverageDirectory: '<rootDir>/coverage',
  collectCoverageFrom: ['**/src/**/*.{ts,tsx}', '!**/loader.{ts,tsx}', '!**/node_modules/**'],
  coveragePathIgnorePatterns: ['.*\\.d\\.ts'],
  globals: {
    __DEV__: false,
    __TEST__: true,
    IS_NOT_SPA: false,
  },
};

修复方案

该报错由Jest 27对自定义transform的导出规则变更导致,要求transform必须导出包含process或processAsync方法的对象,不能直接导出字符串。
修改自定义transform文件<rootDir>/__mocks__/fileMock.js的代码即可:

原错误写法

module.exports = 'test-file-stub'

修改后正确写法

module.exports = {
  process(src, filename, config, options) {
    return "module.exports = 'test-file-stub'";
  },
};

内容的提问来源于stack exchange,提问作者VadimBaranov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 11:36:03