Search code examples
node.jsreactjstypescriptjestjsts-jest

ts-jest Optional Chaining not working if tsConfig Target as ESNext, ES2021 or ES2020


Optional Chaining is creating issue when I'm try to set the tsConfig Target as ESNext, ES2021 or ES2020. But works fine when I set the target to ES2019. Not sure what I am doing wrong.

Below is the look alike of my jest config:

{
    ...,
    transform: {
        '^.+\\.(ts|tsx)$': 'ts-jest',
    },
    globals: {
        'ts-jest': {
        tsConfig: {
            target: 'ES2019',
        },
    },
}

Solution

  • This is the link for the comment which portrays the complete issue

    Jest uses JS DOM to render the components and run the test cases in it.

    JS DOM internally uses Node JS to perform the above process.

    After ES2019, the conditional chaining is added by default to ECMAScript and so it will not transpile to usual ternary conditions. But Node JS doesn't support conditional chaining by default yet. So on using the ES2019 as the target, it transpiles and works on both React App and Jest and on setting target after ES2019, jest throws error for conditional chaining, as it won't be recognized by Node js