Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with ts jest and node-fetch v3 #1289

Closed
a-wf opened this issue Sep 14, 2021 · 21 comments
Closed

issue with ts jest and node-fetch v3 #1289

a-wf opened this issue Sep 14, 2021 · 21 comments
Labels

Comments

@a-wf
Copy link

a-wf commented Sep 14, 2021

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

@a-wf a-wf added the question label Sep 14, 2021
@sorensenNiels

This comment has been minimized.

@jimmywarting
Copy link
Collaborator

ping @LinusU you have some more experiences with jest, maybe you have any suggestions?

@tomas-hartman
Copy link

tomas-hartman commented Sep 24, 2021

+1 same issue with ts/jest compiled with babel

  // jest.config.js option that might be relevant
  transform: {
    '^.+\\.[t|j]sx?$': 'babel-jest',
  },

jpb06 added a commit to jpb06/node-jest-badges that referenced this issue Sep 26, 2021
@berenddeboer
Copy link

Having the same problem here.

@SashaJson
Copy link

i have same this problem! how fix this now ? Or need await solution ?

@markusl
Copy link

markusl commented Oct 14, 2021

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

@JulienKode
Copy link

JulienKode commented Oct 14, 2021

I experience the same issue on my side with swc and jest

@lynxtaa
Copy link
Contributor

lynxtaa commented Oct 15, 2021

I think I made it work with jest and Typescript. Run jest with experimental-vm-modules enabled:

"test:watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch",

jest.config.json if you are using ES Modules:

{
  "roots": ["<rootDir>/src"],
  "extensionsToTreatAsEsm": [".ts"],
  "testEnvironment": "node",
  "moduleNameMapper": {
    "^(\\.{1,2}/.*)\\.js$": "$1"
  },
  "transform": {
    "\\.[jt]sx?$": [
      "babel-jest",
      {
        "babelrc": false,
        "presets": ["@babel/preset-typescript"],
        "plugins": ["@babel/plugin-proposal-optional-chaining"]
      }
    ]
  }
}

Check you my example repository: https://github.com/lynxtaa/node-fetch-jest-example

jest.config.json if you are using CommonJS:

{
  "roots": ["<rootDir>/src"],
  "testEnvironment": "node",
  "transform": {
    "\\.[jt]sx?$": [
      "babel-jest",
      {
        "babelrc": false,
        "presets": ["@babel/preset-typescript"],
        "plugins": [
          "@babel/plugin-proposal-optional-chaining",
          "@babel/plugin-transform-modules-commonjs"
        ]
      }
    ]
  }
}

And in this case you should use dynamic import:

const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));

@storyalex
Copy link

I have a few typescript projects where this happened to me.

Add the following to jest.config.js to run all .js-files through babel when running jest:

  transform: {
    "^.+\\.(ts|tsx)$": "ts-jest",
    "^.+\\.(js)$": "babel-jest",
  },
  transformIgnorePatterns: [
  ],

Note the empty transformIgnorePatterns. By default transforms ignore node_modules.
This worked for me with:

node-fetch: ^3.0.0
babel-jest: ^27.3.1
jest: ^27.3.1
ts-jest: ^27.0.7

@martsie
Copy link

martsie commented Dec 8, 2021

Rolling back to 2.6 worked for me to get serverless-bundle to run jest tests.

@bertrand-caron
Copy link

Has anyone made any progress on this? I tried @lynxtaa's solution with no success.

@ccarcaci
Copy link

Same here, rolling back to 2.6 worked. Any of the other solutions were not working.

@p0wl
Copy link

p0wl commented Dec 23, 2021

same issue here, rolled back as well...

@rguzg
Copy link

rguzg commented Dec 26, 2021

rolling back worked as well!

@rguzg
Copy link

rguzg commented Jan 10, 2022 via email

@mikeaustin
Copy link

Is there a solution to using node-fetch 3.x with jest?

@node-fetch node-fetch deleted a comment from veritem Jan 26, 2022
@ksw25
Copy link

ksw25 commented Jan 28, 2022

Is there a solution for this?

@ravshansbox
Copy link

This is a small example where jest works with node-fetch@3: https://github.com/ravshansbox/jest-node-fetch-es-modules

@plemelin
Copy link

plemelin commented Feb 9, 2022

Note: edited after original comment this issue should be closed and reference #1279 End note

This is a small example where jest works with node-fetch@3: https://github.com/ravshansbox/jest-node-fetch-es-modules

This is misleading as it works only because you are telling jest to run with support for ESM (--experimental-vm-modules).

As far as I can tell, node-fetch v3 only works as an ESM and you cannot make it work in jest without that flag. Our teams are not going to use that flag unfortunately. So unless this becomes supported in Jest, we are going to use node-fetch <3.0.0

Also looks like it's slowing the adoption of node-fetch v3:

Screenshot from 2022-02-09 15-07-41

@jimmywarting
Copy link
Collaborator

I haven't played with jest lately...
but is the "commonjs" vm able to load esm-only packages using the lazy/async import('node-fetch')?

@node-fetch node-fetch locked and limited conversation to collaborators Feb 9, 2022
@jimmywarting jimmywarting converted this issue into discussion #1503 Feb 9, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Projects
None yet
Development

No branches or pull requests