ThomZz / ts-jest-mock-import-meta

A simple Typescript AST transformer for ts-jest, that mock import.meta expressions.
MIT License
36 stars 2 forks source link

Not getting this plugin to work #8

Closed master117 closed 8 months ago

master117 commented 8 months ago

On JEST 29.2.1

jest.config.ts:

import type { JestConfigWithTsJest } from "ts-jest";

const jestConfig: JestConfigWithTsJest = {
    preset: "ts-jest",
    testEnvironment: "jsdom",
    testEnvironmentOptions: {
        testURL: "http://localhost:8888",
    },
    transform: {
        "^.+\\.tsx?$": [
            "ts-jest",
            {
                diagnostics: {
                    ignoreCodes: [1343],
                },
                astTransformers: {
                    before: [
                        {
                            path: "node_modules/ts-jest-mock-import-meta", // or, alternatively, 'ts-jest-mock-import-meta' directly, without node_modules.
                            options: {
                                metaObjectReplacement: {
                                    env: {
                                        PROD: false,
                                        DEV: true,
                                    },
                                    url: "https://www.url.com",
                                },
                            },
                        },
                    ],
                },
            },
        ],
        // process `*.tsx` files with `ts-jest`
    },
    moduleNameMapper: {
        "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
        "\\.(css|less|scss)$": "identity-obj-proxy",
    },
};

export default jestConfig;

grafik

Note that this is on untested files, which jest is rgabbing for coverage info via jest --coverage --collectCoverageFrom='src/**/*.{ts,tsx} No tested file currently contains meta.

Please let me know if there is any other info you require.

ThomZz commented 8 months ago

Hi @master117 ! Is every concerned files are well included within the typescript conmpilation (files, or include property in the root tsconfig.json) ?. ts-jest is using the root tsconfig.json by default if no tsconfig configuration property is provided.

ThomZz commented 8 months ago

After a quick investigation, it seems that "untested files" (and files that are not imported by any of your tests), are not processed by ts-jest, therefore, astTransformer are not applied.

So, if files that you specify within the --collectCoverageFrom options contains "import.meta" statements, they will not be transformed by ts-jest, but by a simplier typescript processor (?) provided by jest directly.

https://github.com/kulshekhar/ts-jest/issues/3207

master117 commented 8 months ago

After a quick investigation, it seems that "untested files" (and files that are not imported by any of your tests), are not processed by ts-jest, therefore, astTransformer are not applied.

So, if files that you specify within the --collectCoverageFrom options contains "import.meta" statements, they will not be transformed by ts-jest, but by a simplier typescript processor (?) provided by jest directly.

kulshekhar/ts-jest#3207

Ah, good to know. Nothing you can do about that I guess. Thank you for the help!