Closed MarkLyck closed 2 years ago
Thank you for opening an issue, Mark. I will investigate today
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
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
This is failing for me using NextJS 12.1.6 with vanilla configuration (using their built in SWC compiler)
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:
So it is a transforming issue. I'm closing this issue but please don't hesitate if you're having any other issues
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.
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
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"
]
}
Still can't reproduce. Would you like to try and test using?
{
"test": "NODE_OPTIONS=--experimental-vm-modules jest"
}
That works fine locally but in Github actions:
Any ideas?
@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
@omrilotan this resolved the issue, thank you!
@omrilotan this resolved the issue, thank you!
Brilliant.Ill release a version in about 30 minutes. Thank you for taking the time
Resolved in version 3.5.0
Steps to reproduce
import isbot from 'isbot'
anywhere in a file that's tested. install and usejest@28
to test the file.Expected behaviour
The isbot import should work.
Actual behaviour
Jest throws an error about unexpected token inside 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.