jason-henriksen / typescript-with-esm-no-babel-boilerplate

Using ESM, TypeScript and no babel took a lot of research to get right. Here is a starter that works.
MIT License
24 stars 8 forks source link

Fail to run jest test with "ts-node": "^10.4.0" #1

Open jaybe78 opened 2 years ago

jaybe78 commented 2 years ago

Hello Thanks for putting this boilerplate together. I've been banging my head against the wall to use es module with jest, tried everything and always fieled. Been stuck on this for days but stumbled upon your work, which helped finding out my code is failing because I use "ts-node": "^10.4.0", you use 9.1.1 and it works.

Any idea what's changed between 9 and 10 ?

EDIT:

I've found out this is related to this https://github.com/facebook/jest/issues/11453

You have to rename jest.config.ts to jestconfig.cjs

module.exports = {
    globals: {
        extensionsToTreatAsEsm: ['.ts', '.js'],
        'ts-jest': {
            // without this, you git really frustrating errors.
            useESM: true,
        }
    },

    // this preset is absurdly important and (for me) was really a PITA to discover!
    preset: 'ts-jest/presets/js-with-ts-esm',

    // also important to not have anything in here
    transformIgnorePatterns: [
    ],
    testPathIgnorePatterns:[
      "/node_modules/",
      "dist"
    ]
  }
jason-henriksen commented 2 years ago

Thanks for the kind words and for the self-reply! Really glad it was helpful, it took me a long time to figure it out too!

jaybe78 commented 2 years ago

Any idea why I would get

require() of `/fileX` from `fileY` is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.

whereas when I look into my fileY.ts I only have import there and no require at all ?

FileX is a ts file coming from an ES module that I import in my main app