jaredpalmer / tsdx

Zero-config CLI for TypeScript package development
https://tsdx.io
MIT License
11.2k stars 505 forks source link

(TSDX Jest) SyntaxError: Cannot use import statement outside a module #1076

Closed ssaket closed 2 years ago

ssaket commented 2 years ago

Hi there,

I am using node-fetch api and when I run yarn run test, I get the following error:

    Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /Users/ssaurabh/Projects/typescript-images-api/node_modules/node-fetch/src/index.js:9
    import http from 'http';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import fetch from 'node-fetch';
        | ^
      2 | import { RequestInit, Response } from 'node-fetch';
      3 |
      4 | class FetchProxy {

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1258:14)
      at Object.<anonymous> (src/FetchProxy.ts:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        3.166s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

environment

System:
    OS: macOS 11.5.2
    CPU: (8) x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
    Memory: 125.96 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 12.22.4 - ~/.nvm/versions/node/v12.22.4/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.20.3 - ~/.nvm/versions/node/v12.22.4/bin/npm
  Browsers:
    Chrome: 93.0.4577.82
    Firefox: 89.0.2
    Safari: 14.1.2
  npmPackages:
    tsdx: ^0.14.1 => 0.14.1 
    typescript: ^4.4.3 => 4.4.3 
  npmGlobalPackages:
    typescript: 4.4.3

I choose this package/lib because I don't know much about configuration stuff (Jest, node, tsconfig) but apparently I am stuck and can't find any solution. Can you please let me know what am I missing here?

amerryma commented 2 years ago

See here https://github.com/jaredpalmer/tsdx/pull/1019/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5

ssaket commented 2 years ago

@amerryma Thanks for the answer, but this ain't working as well :( here's the new error - TypeError: Jest: a transform must export a 'process' function.

ssaket commented 2 years ago

I figured it out. While installing @babel/plugin-transform-modules-commonjs I saw a warning saying I should also install jest-babel which is not required, removing it solved the problem. Thanks a lot for your help :)

amerryma commented 2 years ago

Thanks for the update. I'll try this out myself too!

AndrzejSala commented 2 years ago

I have the same issue. I'm trying to import something to a test file but still get SyntaxError: Cannot use import statement outside a module.

In my opinion, the issue should be reopened as the problem still occurs on the master branch.