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

SyntaxError: Unexpected token 'export' #395

Closed
melvinwallerjr opened this issue Oct 4, 2022 · 7 comments
Closed

SyntaxError: Unexpected token 'export' #395

melvinwallerjr opened this issue Oct 4, 2022 · 7 comments

Comments

@melvinwallerjr
Copy link

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.

transformIgnorePatterns: ['/node_modules/(?!nanoid)'],

The error is basic and repeats:

Test suite failed to run
    Jest encountered an unexpected token

    Details:

    C:\projects\shared-ui-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 React, { useState } from 'react';
    > 2 | import { nanoid } from 'nanoid';
        |                        ^

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1678:14)
@ai
Copy link
Owner

ai commented Oct 5, 2022

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).

@ai ai closed this as completed Oct 5, 2022
@melvinwallerjr
Copy link
Author

Thanks for the reply. Project was using "uuid": "8.3.2" and now replacing it with "nanoid": "3.3.4". Deleted node_modules and started fresh as a double check. Still getting the same errors. Any other suggestions?

@ai
Copy link
Owner

ai commented Oct 10, 2022

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.

@SunXinFei
Copy link

jest.mock("nanoid", () => { return {
  nanoid : ()=>{}
} });

use mock in setupTests.js of jest

@bencoullie
Copy link

bencoullie commented Mar 27, 2023

I ran into the same issue as OP. I'm on nanoid 4.0.1, jest 29.4.3 and node 18.12.1.

I also tried transformIgnorePatterns: ['node_modules/(?!nanoid)'] in my jest.config.js. I assumed that would tell the babel compiler 'compile these files for testing based off jest.config.js and my babel config (set by next.js via presets: ['next/babel'] in babel.config.js).

However I still got the SyntaxError: Unexpected token 'export' error.

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',
}))

@gabycperezdias
Copy link

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?

@taro-ishihara
Copy link

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 }
})

bgturner added a commit to getflywheel/local-components that referenced this issue Oct 2, 2023
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
bgturner added a commit to getflywheel/local-components that referenced this issue Oct 2, 2023
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
bgturner added a commit to getflywheel/local-components that referenced this issue Oct 3, 2023
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
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

6 participants