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

Not compatible with jest version 28 #183

Closed
MarkLyck opened this issue May 5, 2022 · 15 comments
Closed

Not compatible with jest version 28 #183

MarkLyck opened this issue May 5, 2022 · 15 comments
Assignees

Comments

@MarkLyck
Copy link

MarkLyck commented May 5, 2022

Steps to reproduce

import isbot from 'isbot' anywhere in a file that's tested.
install and use jest@28 to test the file.

Expected behaviour

The isbot import should work.

Actual behaviour

Jest throws an error about unexpected token inside isbot.

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/marklyck/colony/colony-frontend/node_modules/.pnpm/isbot@3.4.6/node_modules/isbot/index.mjs:328
    export { isbot as default };
    ^^^^^^

    SyntaxError: Unexpected token 'export'

      1 | import LogRocket from 'logrocket'
    > 2 | import isbot from 'isbot'
        | ^

Additional details

Jest@28 started supporting exports in package.json;
https://jestjs.io/docs/upgrading-to-jest28#packagejson-exports

This is why this breaks, and it suggests submitting an issue to isbot to fix the exports in this package to be correct.

@omrilotan
Copy link
Owner

Thank you for opening an issue, Mark. I will investigate today

@omrilotan
Copy link
Owner

I'm afraid the issue may with your build system. Would you like to share some project details?
Node version, Jest configuration, and any additional build configuration (webpack etc).

I've successfully tested using Jest with no build, using both native ESM and commonjs.
Please review Pull Request #184

@omrilotan
Copy link
Owner

omrilotan commented May 9, 2022

Update

The following change also did not make Jest resolve to commonjs

- "browser": "./index.mjs",
+ "browser": {
+   "import": "./index.mjs",
+   "require": "./index.js"
+ },
- "node": "./index.mjs",
+ "node": {
+   "import": "./index.mjs",
+   "require": "./index.js"
+ },

Jest resolve the module without modules in VM flag.

I'm open to suggestions

@wadehammes
Copy link

This is failing for me using NextJS 12.1.6 with vanilla configuration (using their built in SWC compiler)

image

@wadehammes
Copy link

This helped me: https://stackoverflow.com/questions/60714101/how-to-setup-jest-with-node-modules-that-use-es6

Adding transformIgnorePatterns: ["<rootDir>/node_modules/(?!isbot)"], to my custom Jest config allowed tests to pass with Jest 28:

image

@omrilotan
Copy link
Owner

So it is a transforming issue. I'm closing this issue but please don't hesitate if you're having any other issues

@wadehammes
Copy link

I spoke to soon, apparently my yarn had updated on this branch to a previous version and that's why it passed. my fix above does not work with Jest 28. My apologies. Still experiencing the export issue.

@omrilotan
Copy link
Owner

I've added babel transpiling, removed VM module flag, and typescript tests.
I'll need to see your transpiling setup. Are you using webpack or something on top of everything?
Have a look at #186

@omrilotan omrilotan reopened this May 11, 2022
@wadehammes
Copy link

Im using a standard NextJS setup. There is some webpack config but mainly for Sentry.

Next 12.1.5 using their SWC compiler (no babel)

jest.config.ts

// jest.config.ts
import type { Config } from "@jest/types";
import nextJest from "next/jest";

// Sync object
const customJestConfig: Config.InitialOptions = {
  verbose: true,
  testPathIgnorePatterns: ["<rootDir>/.next/", "<rootDir>/node_modules/"],
  setupFiles: ["<rootDir>/.jest/setEnvVars.ts"],
  setupFilesAfterEnv: ["<rootDir>/.jest/setupTests.ts"],
  moduleDirectories: ["node_modules", "<rootDir>"],
  testEnvironment: "jest-environment-jsdom",
  transformIgnorePatterns: ["<rootDir>/node_modules/(?!isbot)"],
};

// Providing the path to your Next.js app which will enable loading next.config.js and .env files
const createJestConfig = nextJest({ dir: "./" })(customJestConfig);

module.exports = async () => {
  // Create Next.js jest configuration presets
  const jestConfig = await createJestConfig();

  // Custom `moduleNameMapper` configuration
  const moduleNameMapper = {
    ...jestConfig.moduleNameMapper,
    "\\.(css|less|scss|sass)$": "identity-obj-proxy",
  };

  return { ...jestConfig, moduleNameMapper };
};

tsconfig

{
  "compilerOptions": {
    "allowJs": false,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "declaration": false,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "importHelpers": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "module": "esnext",
    "moduleResolution": "node",
    "noEmit": true,
    "resolveJsonModule": true,
    "rootDir": ".",
    "skipLibCheck": true,
    "strict": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "incremental": true,
    "paths": {
      "@mui/styled-engine": ["./node_modules/@mui/styled-engine-sc"]
    }
  },
  "include": [
    "cssprops.d.ts",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    ".jest/setupTests.ts",
    "i18next.config.ts"
  ],
  "exclude": [
    "node_modules",
    "next.config.js"
  ]
}

@omrilotan
Copy link
Owner

Still can't reproduce. Would you like to try and test using?

{
  "test": "NODE_OPTIONS=--experimental-vm-modules jest"
}

@wadehammes
Copy link

That works fine locally but in Github actions:

image

Any ideas?

@omrilotan
Copy link
Owner

@wadehammes Good news,
Once I've defined jest environment as jsdom - I was able to replicate your issue.

If you could please test the next version using "next" tag - we can see that it solved the issue for you and release a stable version:

npm i isbot@next

@wadehammes
Copy link

@omrilotan this resolved the issue, thank you!

@omrilotan
Copy link
Owner

@omrilotan this resolved the issue, thank you!

Brilliant.Ill release a version in about 30 minutes. Thank you for taking the time

@omrilotan
Copy link
Owner

Resolved in version 3.5.0

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

3 participants