-
-
Notifications
You must be signed in to change notification settings - Fork 789
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
SyntaxError: Unexpected token 'export' #395
Comments
Try Nano ID 3.x. The 4.x branch is only for ESM projects (looks like you have CommonJS project and compile ESM to CJS). |
Thanks for the reply. Project was using |
Nano ID is dual ESM/CJS package. Jest is hacking the Node.js loading mechanism. You may use old Jest and configure it in a wrong way, so it loads ESM-files into CJS package for some reason. You need to ask Jest community since they use non-standard hacks which I do not know. |
jest.mock("nanoid", () => { return {
nanoid : ()=>{}
} }); use mock in setupTests.js of jest |
I ran into the same issue as OP. I'm on nanoid I also tried However I still got the I feel like there is something I don't understand here... that should be compiling nanoid and the error should disappear but it never. Any clues welcome. My solution: @SunXinFei's solution works for me (though I tweaked it to return a predictable actual string to test against): // Mock nanoid to return a fake ID when called in a test
jest.mock('nanoid', () => ({
nanoid: () => 'this-is-a-fake-nano-id',
})) |
Facing the same issues, I cannot mock this since this is used as a dependency of a dependency of mine... I tried with both nanoid 4 and 3... Any tips? |
Only I can do is like this. But this way, you can't change the fakeKey dinamically in each test. const fakeKey = 'ABCD5678'
jest.mock('nanoid', () => {
return { customAlphabet: () => () => fakeKey }
}) |
Shortid has been deprecated and they recommend nanoid [1] While upgrading, I tried running the ContextMenu.test.tsx tests without mocking nanoid, but kept getting an error: ``` FAIL src/components/menus/ContextMenu/ContextMenu.test.tsx ● Test suite failed to run Jest encountered an unexpected token Jest failed to parse a file.. ... Details: /Users/ben.turner/src/local/local-components/node_modules/nanoid/index.browser.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { urlAlphabet } from './url-alphabet/index.js' ^^^^^^ SyntaxError: Unexpected token 'export' 1 | import * as React from 'react'; 2 | import classnames from 'classnames'; > 3 | import { nanoid } from 'nanoid'; | ^ 4 | import { useEffect, useRef, useState } from 'react'; 5 | import styles from './ContextMenu.scss'; 6 | import { FunctionGeneric } from '../../../common/structures/Generics'; at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14) at Object.<anonymous> (src/components/menus/ContextMenu/ContextMenu.tsx:3:1) ``` Nanoid uses ESM for newer versions, which has some issues with running in Jest and the `jsdom` environment. [2] There were a few things in the comments for that thread that might have worked, but since I was only changing one implementation which didn't really need to test a fully random thing, I opted to just mock nanoid and be done with it. [^1] https://www.npmjs.com/package/shortid [^2] ai/nanoid#395
Shortid has been deprecated and they recommend nanoid [^1] While upgrading, I tried running the ContextMenu.test.tsx tests without mocking nanoid, but kept getting an error: ``` FAIL src/components/menus/ContextMenu/ContextMenu.test.tsx ● Test suite failed to run Jest encountered an unexpected token Jest failed to parse a file.. ... Details: /Users/ben.turner/src/local/local-components/node_modules/nanoid/index.browser.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { urlAlphabet } from './url-alphabet/index.js' ^^^^^^ SyntaxError: Unexpected token 'export' 1 | import * as React from 'react'; 2 | import classnames from 'classnames'; > 3 | import { nanoid } from 'nanoid'; | ^ 4 | import { useEffect, useRef, useState } from 'react'; 5 | import styles from './ContextMenu.scss'; 6 | import { FunctionGeneric } from '../../../common/structures/Generics'; at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14) at Object.<anonymous> (src/components/menus/ContextMenu/ContextMenu.tsx:3:1) ``` Nanoid uses ESM for newer versions, which has some issues with running in Jest and the `jsdom` environment. [^2] There were a few things in the comments for that thread that might have worked, but since I was only changing one implementation which didn't really need to test a fully random thing, I opted to just mock nanoid and be done with it. [^1]: https://www.npmjs.com/package/shortid [^2]: ai/nanoid#395
Shortid has been deprecated and they recommend nanoid [^1] While upgrading, I tried running the ContextMenu.test.tsx tests without mocking nanoid, but kept getting an error: ``` FAIL src/components/menus/ContextMenu/ContextMenu.test.tsx ● Test suite failed to run Jest encountered an unexpected token Jest failed to parse a file.. ... Details: /Users/ben.turner/src/local/local-components/node_modules/nanoid/index.browser.js:1 ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { urlAlphabet } from './url-alphabet/index.js' ^^^^^^ SyntaxError: Unexpected token 'export' 1 | import * as React from 'react'; 2 | import classnames from 'classnames'; > 3 | import { nanoid } from 'nanoid'; | ^ 4 | import { useEffect, useRef, useState } from 'react'; 5 | import styles from './ContextMenu.scss'; 6 | import { FunctionGeneric } from '../../../common/structures/Generics'; at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14) at Object.<anonymous> (src/components/menus/ContextMenu/ContextMenu.tsx:3:1) ``` Nanoid uses ESM for newer versions, which has some issues with running in Jest and the `jsdom` environment. [^2] There were a few things in the comments for that thread that might have worked, but since I was only changing one implementation which didn't really need to test a fully random thing, I opted to just mock nanoid and be done with it. [^1]: https://www.npmjs.com/package/shortid [^2]: ai/nanoid#395
I'm looking to replace UUID with NanoID and getting the above error when running Jest tests. Each place where I've applied NanoID in place of UUID I get the export error. The project uses Node 14.20.0, React 17.0.2, Typescript 4.82, and Jest 29.0.3
I've tried suggestions to add to to
jest.config.js
with no improvement.The error is basic and repeats:
The text was updated successfully, but these errors were encountered: