vercel / next.js

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

Package does not get transpiled in Jest, like react-markdown #55031

Open weyert opened 1 year ago

weyert commented 1 year ago

Link to the code that reproduces this issue or a replay of the bug

https://github.com/weyert/nextjs-jest-markdown

To Reproduce

  1. Start the application with npm run dev
  2. Navigate to http://127.0.0.1:3000
  3. Note that This is a Markdown paragraph get rendered
  4. Run npm run test
  5. Notice that jest fail forsrc/__tests__/pages/StartPage.test.tsx and src/components/__tests__/PageContent.test.tsx

Current vs. Expected behavior

I would expect that I can write unit tests with packages that get transpiled instead I am getting the 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/weyert/Development/Projects/Reproductions/markdown-jest-next/node_modules/vfile/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export {VFile} from './lib/index.js'
                                                                                      ^^^^^^

    SyntaxError: Unexpected token 'export'

Verify canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.14.0
      npm: 8.19.3
      Yarn: 1.22.19
      pnpm: 8.7.3
    Relevant Packages:
      next: 13.4.20-canary.18
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 4.9.5
    Next.js Config:
      output: N/A

Which area(s) are affected? (Select all that apply)

Jest (next/jest)

Additional context

I made a reproduction repository (see above) for an problem I am experiencing in a work project that uses an older version of Next.js (13.4.12) but planning to upgrade to 13.4.20 once its released to solve my modularize import issue from a previous ticket.

I am trying to write unit tests for components that consume the react-markdown package but fail to do so

weyert commented 1 year ago

If I add vfile to the transpilePackages-configuration in next.config.js I am getting the following error:

Cannot find module '../../src/lib/index.js' from 'node_modules/vfile/index.js'

The code in the vfile/index.js file is:


export {VFile} from './lib/index.js'
``

I am not sure why the path changes to `'../../src/lib/index.js'`?
weyert commented 1 year ago

@shuding Ticket :)