vercel / next.js

The React Framework
https://nextjs.org
MIT License
126.82k stars 26.96k forks source link

Jest encountered an unexpected token when DynamoDB imported into file under test #38368

Closed herdsothom closed 1 year ago

herdsothom commented 2 years ago

Verify canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 20.6.0: Tue Feb 22 21:10:41 PST 2022; root:xnu-7195.141.26~1/RELEASE_X86_64
Binaries:
  Node: 16.15.1
  npm: 8.11.0
  Yarn: 1.22.5
  pnpm: N/A
Relevant packages:
  next: 12.2.1-canary.3
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

I cannot get Jest to work correctly with my new install of NextJS, when using a test that calls a file that imports DynamoDB (im assuming this will also happen for other packages)

I get the following error:

Test suite failed to run

    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/xxx/Development/next/with-jest-app/node_modules/uuid/dist/esm-browser/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export { default as v1 } from './v1.js';
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1796:14)

This happens when the test calls an exported function that imports DynamoDB.

Of course if i simply remove the import, the test will pass.

Expected Behavior

The test should pass

Link to reproduction

See the reproduce steps

To Reproduce

To reproduce it using a very basic setup, i ran: npx create-next-app --example with-jest with-jest-app

I added to my package.json dependencies: "@aws-sdk/client-dynamodb": "^3.121.0",

I created a new test file example.test.js in the __tests__ directory. Note that in this new test file, a simple test like expect(1).toBe(1) works fine. If I make it:

import {getFromDynamo} from '../lib/ddb'

test('fails', async () => {
    expect(await getFromDynamo()).toBe(3)
})

And in ddb.js:

import {DynamoDBClient} from "@aws-sdk/client-dynamodb";

export const getFromDynamo = async () => {
    return 3
}
ryanhaticus commented 2 years ago

It looks like Jest wasn't expecting the export token from the uuid package (a dependency of DynamoDB). According to issue 488 from the UUID repository, this seems to be related to a "reverse dependency of this package".

Some users have reported this issue solving itself after updating their version of NodeJS.

I'd reckon you create a separate issue in https://github.com/thenativeweb/uuidv4/ as suggested by @ctavan here. This likely isn't an issue with NextJS.

herdsothom commented 2 years ago

It looks like Jest wasn't expecting the export token from the uuid package (a dependency of DynamoDB). According to issue 488 from the UUID repository, this seems to be related to a "reverse dependency of this package".

Some users have reported this issue solving itself after updating their version of NodeJS.

I'd reckon you create a separate issue in https://github.com/thenativeweb/uuidv4/ as suggested by @ctavan here. This likely isn't an issue with NextJS.

Thanks for the reply Ryan. Unfortunately Im already on node16 so dont think I can find a fix there. Howcome you suggest to raise the issue in uuidv4 rather than just uuid? Isnt uuid the package dynamo is using here?

ryanhaticus commented 2 years ago

It looks like Jest wasn't expecting the export token from the uuid package (a dependency of DynamoDB). According to issue 488 from the UUID repository, this seems to be related to a "reverse dependency of this package". Some users have reported this issue solving itself after updating their version of NodeJS. I'd reckon you create a separate issue in https://github.com/thenativeweb/uuidv4/ as suggested by @ctavan here. This likely isn't an issue with NextJS.

Thanks for the reply Ryan. Unfortunately Im already on node16 so dont think I can find a fix there. Howcome you suggest to raise the issue in uuidv4 rather than just uuid? Isnt uuid the package dynamo is using here?

Yes, uuid would be the correct package. Sorry about that.

herdsothom commented 2 years ago

Raising an issue in the uuidjs/uuid project now. Ps heres a codesandbox: https://codesandbox.io/s/boring-resonance-g84z3y?file=/__tests__/example.test.js

github-actions[bot] commented 1 year ago

Please verify that your issue can be recreated with next@canary.

Why was this issue marked with the please verify canary label?

We noticed the provided reproduction was using an older version of Next.js, instead of canary.

The canary version of Next.js ships daily and includes all features and fixes that have not been released to the stable version yet. You can think of canary as a public beta. Some issues may already be fixed in the canary version, so please verify that your issue reproduces by running npm install next@canary and test it in your project, using your reproduction steps.

If the issue does not reproduce with the canary version, then it has already been fixed and this issue can be closed.

How can I quickly verify if my issue has been fixed in canary?

The safest way is to install next@canary in your project and test it, but you can also search through closed Next.js issues for duplicates or check the Next.js releases. You can also use the GitHub template (preferred), or the CodeSandbox or StackBlitz templates to create a reproduction with canary from scratch.

My issue has been open for a long time, why do I need to verify canary now?

Next.js does not backport bug fixes to older versions of Next.js. Instead, we are trying to introduce only a minimal amount of breaking changes between major releases.

What happens if I don't verify against the canary version of Next.js?

An issue with the please verify canary that receives no meaningful activity (e.g. new comments that acknowledge verification against canary) will be automatically closed and locked after 30 days.

If your issue has not been resolved in that time and it has been closed/locked, please open a new issue, with the required reproduction, using next@canary.

I did not open this issue, but it is relevant to me, what can I do to help?

Anyone experiencing the same issue is welcome to provide a minimal reproduction following the above steps. Furthermore, you can upvote the issue using the :+1: reaction on the topmost comment (please do not comment "I have the same issue" without repro steps). Then, we can sort issues by votes to prioritize.

I think my reproduction is good enough, why aren't you looking into it quicker?

We look into every Next.js issue and constantly monitor open issues for new comments.

However, sometimes we might miss one or two due to the popularity/high traffic of the repository. We apologize, and kindly ask you to refrain from tagging core maintainers, as that will usually not result in increased priority.

Upvoting issues to show your interest will help us prioritize and address them as quickly as possible. That said, every issue is important to us, and if an issue gets closed by accident, we encourage you to open a new one linking to the old issue and we will look into it.

Useful Resources

jankaifer commented 1 year ago

It seems that there has been fix in the uuid package.

github-actions[bot] commented 1 year ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.