question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

issue with ts jest and node-fetch v3

See original GitHub issue

I upgraded node-fetch to v3, and run jest test and I get:

 Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:
import http from 'http';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { Task, DataError } from '@koober/std';
    > 2 | import fetch from 'node-fetch';
        | ^
      3 | import { FacebookResponseError, FacebookTokenInfo, FacebookTokenInfoFormatted, FacebookUserIdentity } from './data';
      4 |
      5 | export interface FacebookClient

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1597:14)
      at Object.<anonymous> (src/bundle/facebook/client.ts:2:1)

I followed instructions, but nothing changes, I also tried this jest config

  "extensionsToTreatAsEsm": [
      ".ts"
    ],
    "globals": {
      "ts-jest": {
        "tsconfig": "tsconfig.json",
        "useESM": true
      }
    },
     "preset": "ts-jest/presets/default-esm",

but nothing happens

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:70
  • Comments:21 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
markuslcommented, Oct 14, 2021

@SashaJson we are currently migrating to axios since there are no responses to these quite challenging issues users are facing

4reactions
berenddeboercommented, Oct 3, 2021

Having the same problem here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use node-fetch with Jest and Typescript?
I my case, it looks like there was some issue with the latest node-fetch 3.x packaged and ts-jest . I went back to...
Read more >
Bypassing module mocks - Jest
Jest allows you to mock out whole modules in your tests, which can be useful for testing if your code is ... import...
Read more >
syntaxerror cannot use import statement outside a module ...
So this jest configuration solved my issue: "jest": { "preset": "ts-jest", "testEnvironment": "node", "transform": { "node_modules/variables/.+\\.(j|t)sx?$
Read more >
ts-jest - npm Package Health Analysis - Snyk
The npm package ts-jest was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as...
Read more >
How to correctly mock node-fetch? - Reddit
I'm having problems mocking node-fetch it is not mocking and makes an actual request to an API. Here is the module I'm trying...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found