【问题标题】:Why does jest throw an ERR_UNKNOWN_FILE_EXTENSION when trying to use a ts file as custom testEnvironment?为什么 jest 在尝试将 ts 文件用作自定义 testEnvironment 时会抛出 ERR_UNKNOWN_FILE_EXTENSION?
【发布时间】:2022-10-17 23:29:14
【问题描述】:

我正在将我的 TypeScript 项目切换到 (pnpm) monorepo,并且无法让测试正常运行。我有一个使用自定义testEnvironment 的 jest.config.js,它也是用 TypeScript 编写的。但是,自从我将特定项目移动到我的包目录中以进行 monorepo 重组后,jest 抛出一个错误并且不运行任何测试:

    TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:\workspaces\repos\the-monorepo\packages\testproject\source\testtools\jsdom-environment-global.spec.ts

我用@swc/jest 和 ts-jest 试过了,看看How to use TypeScript in a Custom Test Environment file in Jest?(这让我想“为什么这会奏效?”),不管出于什么原因,昨天它工作得很好。我清理了 jest 缓存并重新安装了所有 node_modules 无济于事。我还在 package.json 中找到了与 "type": "module" 相关的答案,但这不适用于我的包。这不是 ESM。

这是 jest.config.js 的样子:

/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
    silent: true,
    testEnvironment: "<rootDir>/source/testtools/jsdom-environment-global.spec.ts",
    roots: [
        "<rootDir>/source"
    ],
    maxWorkers: "50%",
    transform: {
        "^.+\\.(t|j)s$": ["@swc/jest", {
            sourceMaps: "inline",
            module: {
                strict: false,
                strictMode: false
            },

            jsc: {
                target: "es2021",
                parser: {
                    syntax: "typescript",
                    dynamicImport: true
                }
            }
        }]
    },
    transformIgnorePatterns: [
        "node_modules"
    ],
    testMatch: [
        "**/*/*.spec.ts",
        "**/*/*.test.ts",
        "!**/playwright-tests/**",
        "!**/playwright-tests-smoke/**"
    ],
    moduleFileExtensions: ["ts", "js", "node", "json"],
    reporters: [
        "default"
    ],
    globals: {
        self: {},
        navigator: {},
        jasmine: {},
        __UNIT__: true
    },
    coverageDirectory: "test-results",
    collectCoverage: false,
    collectCoverageFrom: [
        "./source/**/*.ts"
    ],
    coveragePathIgnorePatterns: [
        "/\\.spec\\.ts$/i",
        "/.*node_modules.*/",
        "/.*testtools.*/"
    ],
    coverageReporters: [
        "lcov", "cobertura"
    ],
    coverageProvider: "v8",
    resetMocks: true,
    restoreMocks: true,
    resetModules: true,
    setupFilesAfterEnv: [
        "jest-extended/all",
        "<rootDir>/source/testtools/setup.spec.ts"
    ],
    testPathIgnorePatterns: [
        "<rootDir>/source/testtools/",
        "<rootDir>/source/smoke-tests/",
        "<rootDir>/source/performance-tests/",
        "<rooDir>/source/playwright-tests/",
        "<rooDir>/source/playwright-tests-smoke/"
    ],
    moduleNameMapper: {
        "^@test-helpers": "<rootDir>/source/testtools/index.spec.ts",
        "^@test-tools/(.*)": "<rootDir>/source/testtools/$1",
        '^(\\.{1,2}/.*)\\.js$': '$1'
    }
};
module.exports = config;

如果 testEnvironment 是 TypeScript 文件,为什么 jest 无法解析它?

【问题讨论】:

    标签: javascript typescript jestjs


    【解决方案1】:

    我发现了问题:未将转换器应用于 ESM 文件似乎有些混乱。在我的例子中,jsdom-environment-global.spec.ts 从我的 monorepo 中的不同包中导入了一个 ESM 模块。此导入触发异常,因为 jest 尝试通过 require() 导入它,该导入被捕获,然后通过动态导入再次导入。然后,此动态导入会引发 ts 是未知异常的异常。我不确定为什么这些文件没有被转换,但截至How to use TypeScript in a Custom Test Environment file in Jest? 这似乎是正常的。

    底线:不要从您的 jest testEnvironment 模块中的 ESM 文件中导入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-23
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      • 2022-10-21
      • 1970-01-01
      • 2022-11-04
      • 2013-01-01
      相关资源
      最近更新 更多