Jest stranspiles code differently then ts-node

859 Views Asked by At

I can't make my code work when running the project normally and running in Jest.

Running the project requires this kind of imports for dayjs:

import * as dayjs from 'dayjs';
import * as utc from 'dayjs/plugin/utc';
import * as customParseFormat  from 'dayjs/plugin/customParseFormat';

npm run dev (nodemon --exec ./node_modules/.bin/ts-node ./src/app.ts): No issues

Jest: TypeError: t is not a function

Running the Jest tests requires this kind of imports for dayjs:

import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import customParseFormat  from 'dayjs/plugin/customParseFormat';

npm run dev (nodemon --exec ./node_modules/.bin/ts-node ./src/app.ts):

dayjs.extend(utc);
      ^
TypeError: Cannot read property 'extend' of undefined`

Jest: Success!

So now it is impossible for me to write Tests. Why is it transpiled differntly?

2

There are 2 best solutions below

1
On

I switched to ts-jest and it solved my problem. I was assuming that I could write JS tests to test the TS code but I suppose that is not possible.

2
On

In my case,

tsconfig.json

{
  "compilerOptions": {
    "esModuleInterop": true
  }
}

solved it.