jsynowiec/node-typescript-boilerplate

Jest issue

whoisdominic opened this issue · 3 comments

When I use the boilerplate and try and add and import statement it throws an error in jest

SyntaxError: Cannot use import statement outside a module

Screen Shot 2022-04-03 at 3 13 14 PM

Please read this first. If you have problems with Jest, check the Jest and the ts-jest transformer docs. Also, you can check out one of the earlier tags if you do not want ESM and keep using CommonJS instead.

Regarding chalk, please read their v5 release notes - it mentions that you should probably stay on v4 in this case.

The following code works fine when added to the unit test and use chalk@v4.

import * as chalk from 'chalk';

console.log(chalk.bgCyan('hello'));
npm run test

> node-typescript-boilerplate@0.0.0 test
> jest --coverage

  console.log
    hello
danwt commented

Did you get it solved @whoisdominic?

@danwt
tsconfig.json:

{
  "compilerOptions": {
    ...
    "esModuleInterop": true,
    ...
  }
}

Fixes most of the annoying BS for me.

@jsynowiec Really not sure why this isn't the default. No one wants to waste time reading 100 pages of documentation to understand what is going on...