kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.
https://kulshekhar.github.io/ts-jest
MIT License
6.94k stars 452 forks source link

[Bug]: dynamic imports create a double default-wrapped export -- `{ default: { default: MyDefaultExport } }` #4448

Closed magicmark closed 2 months ago

magicmark commented 2 months ago

Version

29.2.1

Steps to reproduce

foo.ts

export default function MyFoo() {
  return 'foo';
}

foo.test.ts

it('should be a foo', async () => {
  const foo = await import('../src/foo');
  console.log(foo);

  // Expected assertion (does not currently work) - this throws :(
  // expect(typeof foo.default).toBe('function');

  // This currently passes :(
  expect(typeof foo.default.default).toBe('function');
});

Repro link on github: https://github.com/magicmark/ts-jest-repro-1

You can run it in stackblitz here:

Actual vs Expected behavior

const foo = await import('../src/foo');
console.log(foo);

This produces the following output:

[Module: null prototype] {
  __esModule: true,
  default: { default: [Function: MyFoo] }
}

Expected behavior is that it produces the following output:

[Module: null prototype] {
  __esModule: true,
  default: [Function: MyFoo]
}

Debug log

https://i.fluffy.cc/j8klVXNFT1pr5733Hq1SBcz1Q3N6Z6PH.html

Additional context

No response

Environment

System:
  OS: Linux 5.0 undefined
  CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
Binaries:
  Node: 18.20.3 - /usr/local/bin/node
  Yarn: 1.22.19 - /usr/local/bin/yarn
  npm: 10.2.3 - /usr/local/bin/npm
  pnpm: 8.15.6 - /usr/local/bin/pnpm
npmPackages:
  jest: ^29.7.0 => 29.7.0
magicmark commented 2 months ago

Hmm i'm actually seeing this reproing outside of ts-jest, maybe i'm in the wrong place...

src/bar.ts

async function main() {
  const foo = await import('./foo.js');
  console.log(foo);
}

main();

$ npx tsc && node build/bar.js produces the same output 😢

magicmark commented 2 months ago

Looks like this is just a typescript config issue: https://github.com/tsconfig/bases/issues/273