TexKiller / jest-tsd-transform

Jest transform for running tsd tests.
MIT License
2 stars 1 forks source link

Cannot use import statement outside a module #1

Open PindaPixel opened 1 year ago

PindaPixel commented 1 year ago

I import a type (with or without type annotation, either way it breaks) from a library, and it seems to break when I do:

 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { KebabCase } from "type-fest";
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

Config:

// tsconfig.json
{
    "compilerOptions": {
        "module": "ES2022",
        "target": "ES2022",
        "moduleResolution": "Node",
        "esModuleInterop": true,
        "strict": true,
        "types": ["jest"]
    },
   "include": ["tests"]
}
// jest.config.ts 
import type { JestConfigWithTsJest } from "ts-jest";

export default {
    transform: {
        "^.*(\\.|\\/)(test\\.ts)$": [
            "jest-chain-transform",
            {
                transformers: ["jest-tsd-transform", "ts-jest"],
            },
        ],
    },
    testMatch: ["<rootDir>/tests/**/*test.ts"],
} satisfies JestConfigWithTsJest;

What can I do to fix this?

TexKiller commented 1 year ago

Hi, @PindaPixel. Thanks for using the library!

I'm assuming the error only shows up when running Jest, right? Can you provide a sample test file which produces that error? A minimal project reproducing the error might also make it easier to identify the cause.

PindaPixel commented 1 year ago

Of course! I created one here: https://github.com/PindaPixel/tsd-transform-error-repro

TexKiller commented 1 year ago

Ok, it took me a while but I think I got it figured out.

The error you are reporting is happening because your transform pattern is too specific. If you change "^.*(\\.|\\/)(test\\.ts)$" to simply "^.*\\.ts$" that error goes away, but then we get some other issues.

Chaining transforms is only supported by version 27.1.3 of ts-jest (see this: https://github.com/kulshekhar/ts-jest/issues/3372). If you change ts-jest to that version you get some peer dependency errors because your version of Jest is not supported by ts-jest. You can fix that with npm package.json overrides, but then we hit another issue: jest-chain-transform became unsupported by Jest versions 28 and higher and an error is thrown, because of this: https://jestjs.io/docs/28.x/upgrading-to-jest28#transformer

So, without fixing jest-chain-transform I think the only way to use jest-tsd-transform is to downgrade Jest to <28.0.0 while setting ts-jest to 27.1.3.

TexKiller commented 1 year ago

Upon further investigation, it isn't jest-chain-transform but ts-jest<28 that became unsupported, as it returns a simple string instead of the object the new versions of Jest expect. However, it is possible to make a simple change to jest-chain-transform to "fix" the return of ts-jest.

Alternatively it might be preferable to get https://github.com/kulshekhar/ts-jest/issues/3372 on the newest version of ts-jest so we don't have to use an old version, but apparently the change required for that breaks something else, which is why it got removed after 27.1.3. I think it is easier to propose a little change to jest-chain-transform, unless you need some feature from the newest versions of ts-jest.

TexKiller commented 1 year ago

Another even easier alternative: making a tiny transformer to add to the end of the chain, with the sole purpose of fixing the return of the previous transforms. I can try making a package for that when I have some time.

TexKiller commented 1 year ago

@PindaPixel I just updated jest-tsd-transform, embedding version 27.1.3 of ts-jest and fixing its return. Now you don't have to chain transforms anymore, and you should be able to use jest-tsd-transform with any version of Jest.

Please update to version 1.1.1, change jest.config.js so you don't chain transforms anymore and let me know if you still get any errors!