omrilotan / isbot

🤖/👨‍🦰 Detect bots/crawlers/spiders using the user agent string
https://isbot.js.org/
The Unlicense
905 stars 74 forks source link

Not compatible with jest version 28 #183

Closed MarkLyck closed 2 years ago

MarkLyck commented 2 years ago

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 commented 2 years ago

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

omrilotan commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

image

wadehammes commented 2 years ago

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 commented 2 years ago

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

wadehammes commented 2 years ago

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 commented 2 years ago

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

wadehammes commented 2 years ago

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 commented 2 years ago

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

{
  "test": "NODE_OPTIONS=--experimental-vm-modules jest"
}
wadehammes commented 2 years ago

That works fine locally but in Github actions:

image

Any ideas?

omrilotan commented 2 years ago

@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 commented 2 years ago

@omrilotan this resolved the issue, thank you!

omrilotan commented 2 years ago

@omrilotan this resolved the issue, thank you!

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

omrilotan commented 2 years ago

Resolved in version 3.5.0