当尝试将TS文件用作自定义测试环境时,Jest为什么会抛出err_unknown_file_extension?

发布于 2025-02-13 23:12:31 字数 3019 浏览 0 评论 0原文

我将打字稿项目切换到(PNPM)monorepo,并且要适当运行测试时遇到麻烦。我有一个Jest.config.js,它使用code testEnvironment也编写了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的尝试如何在自定义测试环境文件中使用Typescript在开玩笑中?(这让我认为“为什么这么有效?”),无论出于何种原因,昨天都可以正常工作。我清洁了开玩笑的缓存,并重新安装了所有node_modules,无济于事。我还发现了与“ 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如果是打字稿文件?

I'm switching my TypeScript project to a (pnpm) monorepo and have troubles getting tests to run properly. I have a jest.config.js that uses a custom testEnvironment that's written in TypeScript as well. However, ever since I moved the specific project into my packages directory for the monorepo restructuring, jest throws an Error and doesn't run any tests:

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

I tried it with @swc/jest as well as with ts-jest, had a look at How to use TypeScript in a Custom Test Environment file in Jest? (which makes me think "why did this ever work?") and, for whatever reason, it worked fine yesterday. I cleaned jest cache and reinstalled all node_modules to no avail. I also found answers related to "type": "module" in package.json, but this doesn't apply to my package. It's not an ESM.

Here's how the jest.config.js looks like:

/** @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
quot;: ["@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

Why is jest not able to parse the testEnvironment if it's a TypeScript file?

: '$1' } }; module.exports = config;

Why is jest not able to parse the testEnvironment if it's a TypeScript file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖阳 2025-02-20 23:12:31

我发现了这个问题:在没有应用于ESM文件的变压器周围似乎存在一些混乱。就我而言,jsdom-environment-global.spec.ts从我的monorepo中的其他软件包导入了ESM模块。此导入触发了一个例外,因为玩笑试图通过require()导入它,该被捕获,然后再次通过动态导入导入。然后,此动态导入引发了一个例外,即ts是未知的异常。我不确定为什么没有转换这些文件,但是从如何在开玩笑的自定义测试环境文件中使用Typescript?这似乎是正常的。

底线:不要从嘲笑中的ESM文件导入testenvorlirnment模块。

I found the issue: there seems to be some confusion around the transformer not being applied to ESM files. In my case, the jsdom-environment-global.spec.ts imported an ESM module from a different package within my monorepo. This import triggers an exception because jest tries to import it via require(), which is caught and then again imported via dynamic imports. This dynamic import then throws the exception that ts is an unknown exception. I'm not sure why these files haven't been transformed, but as of How to use TypeScript in a Custom Test Environment file in Jest? this seems to be normal.

Bottom line: don't import from ESM files within your jest testEnvironment module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文