Skip to content
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

Got 12.0 fails with jest + ts-jest #1952

Closed
1 of 2 tasks
emil-e opened this issue Dec 14, 2021 · 5 comments
Closed
1 of 2 tasks

Got 12.0 fails with jest + ts-jest #1952

emil-e opened this issue Dec 14, 2021 · 5 comments

Comments

@emil-e
Copy link

emil-e commented Dec 14, 2021

Describe the bug

  • Node.js version: v14.17.6
  • OS & version: macOS 12.0

Actual behavior

Failure when using Got 12.0 together with jest and ts-jest:

 FAIL  src/auth.test.ts
  ● Test suite failed to run

    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.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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:

    /Users/emilsahlen/dev/.../node_modules/got/dist/source/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import create from './create.js';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

I tried to have jest/ts-jest also transform got through transformIgnorePatterns but then it complains about not finding the module node:events instead.

Expected behavior

Succeeds just like 11.x did.

Code to reproduce

Jest config:

// eslint-disable-next-line no-undef
module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  modulePathIgnorePatterns: ["dist"],
};

Tsconfig:

{
  "extends": "@tsconfig/node14/tsconfig.json",
  "compilerOptions": {
    "noImplicitAny": true,
    "sourceMap": true
  }
}

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.
@sindresorhus
Copy link
Owner

This is a problem with Jest or your Jest config.

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

@Inrixia
Copy link

Inrixia commented Jan 8, 2022

I am also running into issues with this and jest documentation for esm support is garbage. @emil-e did you figure out a fix for your config that works?

@Inrixia
Copy link

Inrixia commented Jan 8, 2022

After reading more about the release and jest esm support, I'm just going to have to stick with 11 till jest gets there... Totally not a issue on the got teams side of things, shame jest dropped the ball on esm.

@Rychu-Pawel
Copy link
Contributor

Rychu-Pawel commented Jun 1, 2022

This is my workaround for this issue:

  1. npm i --save-dev ts-jest babel-jest @babel/preset-env
  2. Make jest.config.js to look like this:
module.exports = {
  preset: 'ts-jest/presets/js-with-babel',
  testEnvironment: 'node',
  transformIgnorePatterns: [
    "/node_modules/(?!(got|p-cancelable|@szmarczak|lowercase-keys|@sindresorhus/is|form-data-encoder)/)",
  ],
};
  1. Create babel.config.js that looks like this:
module.exports = {
  "presets": [
    [
      "@babel/preset-env",
      {
        "targets": {
          "node": true
        }
      }
    ]
  ]
}

Edit: Updated above to work with v12.3.0

@dandv
Copy link

dandv commented Aug 30, 2023

@Rychu-Pawel: I tried that workaround with got v13 in this project but jest still fails with this error:

$ npm test

> got-jest@1.0.0 test
> jest

 FAIL  ./index.test.ts
  ● Test suite failed to run

    You appear to be using a native ECMAScript module configuration file, which is only supported when running Babel asynchronously.
        at /home/dan/prg/got-jest/babel.config.js

    > 1 | import got from 'got';
        | ^
      2 |
      3 | /* Commented out due to bogus error
      4 |  * error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', or 'nodenext', and the 'target' option is set to 'es2017' or higher.

      at loadPartialConfigSync (node_modules/@babel/core/src/config/index.ts:49:60)
      at Object.<anonymous> (index.ts:1:1)
      at Object.<anonymous> (index.test.ts:1:1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants