Jest test files that import typescript modules have to `require` and then use the `default` key

Viewed 359

I'm running a test file that imports a typescript module, and as such the test.js file looks something like:

// typescript-module.ts
const fn = (): void => false;

export default fn
const module = require("./typescript-module");

The only problem I seem to have is that I don't automatically get the default export assigned via commonjs? I have access to an object with the shape { default : ..., __esmodule: true} which tells me that... it hasn't been parsed into commonjs properly?

I obviously can get it working if I just const module = require("./typescript-module").default but that just feels... real messy and unsanctioned to me.

My .tsconfig has allowSyntheticDefaultImports set to true, and esModuleInterop is true also.

I'm using ts-jest for transforming ts files for tests in my jest.config.js. I'm just wondering if there's something I'm missing

I want to be able to require my ts modules default exports, but I'm just not sure what part of the toolchain controls that.

If you need any more information, please let me know!

0 Answers
Related