I’m trying to write some unit tests in jest for a react native library I’m writing. However, I keep getting this pesky “unexpected identifier: ‘Error Handler'” message. I’ve tried a lot of possible answers I found on the internet, but none have seemed to work yet.
package.json:
{
"name": "geist-native-icons",
"version": "0.0.1",
"description": "React native components for geist icons",
"type": "module",
"main": "index.js",
"module": "index.js",
"scripts": {
"start": "nodemon --experimental-modules --es-module-specifier-resolution=node index.js",
"prepare-svgs": "tsx ./src/prepare-svgs.ts",
"build": "pnpm run prepare-svgs && svgr --native --icon --typescript -d ./src/icons src/out && tsc -p tsconfig.json",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"release": "pnpm build && src/pre-release.js && cd lib && pnpm publish --access public"
},
"jest": {
"preset": "react-native",
"transformIgnorePatterns": [
"/node_modules/(?!react-native)",
"/node_modules/(?!@react-native-svg)",
"/node_modules/(?!react-native-svg)"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/sc-mitton/geist-native-icons.git"
},
"keywords": [
"icon",
"react",
"geist",
"react-native",
"native",
"rn",
"icons"
],
"license": "MIT",
"homepage": "https://github.com/sc-mitton/geist-native-icons#readme",
"devDependencies": {
"@babel/core": "^7.24.7",
"@babel/preset-env": "^7.24.7",
"@babel/preset-react": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
"@svgr/babel-plugin-remove-jsx-attribute": "^8.0.0",
"@svgr/cli": "^8.1.0",
"@types/babel__core": "^7.20.5",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.12",
"@types/jsdom": "^21.1.7",
"@types/react": "~18.2.0",
"babel-jest": "^29.7.0",
"fs-extra": "^11.2.0",
"jest": "^29.7.0",
"jsdom": "^24.1.0",
"react": "~18.2.0",
"react-dom": "~18.2.0",
"react-native": "^0.74.2",
"react-native-svg": "^15.3.0",
"react-test-renderer": "~18.2.0",
"svgo": "^3.3.2",
"ts-jest": "^29.1.4",
"tsx": "^4.15.5",
"typescript": "^5.4.5"
}
}
tsconfig.json:
{
"compilerOptions": {
"outDir": "./lib/",
"noImplicitAny": false,
"esModuleInterop": true,
"module": "es2020",
"target": "es2020",
"jsx": "react-jsx",
"declaration": true,
"declarationDir": "./lib",
"skipLibCheck": true,
"moduleResolution": "node",
},
"include": [
"src/**/*",
],
"exclude": [
"node_modules",
"**/*.spec.ts",
"src/*.ts",
]
}
Test:
import * as Icons from '../lib';
test('There are icons', () => {
const length = Object.keys(Icons).length;
expect(length).toBeGreaterThan(2);
})
Full Error:
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:
/Documents/Dev/geist-native-icons/node_modules/.pnpm/@[email protected]/node_modules/@react-native/js-polyfills/error-guard.js:14
type ErrorHandler = (error: mixed, isFatal: boolean) => void;
^^^^^^^^^^^^
SyntaxError: Unexpected identifier 'ErrorHandler'