question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue :

ts-jest is not out-of-the-box compatible with Next.js, basically because in Next.js there is no need to add import React from "react" in every file.

That effectively means that in ts-jest when you import a .tsx file, all you get is 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.

Expected behavior :

Work correctly out-of-the-box with Next.js

Debug log:

Test suite failed to run

TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
src/pages/index.tsx:2:13 - error TS2686: 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.  

2     return <div>hello world</div>

Minimal repo

I made this repo using all the expected stuff for a testing environment using testing-library https://github.com/PabloSzx/ts-jest-nextjs

Workaround

Import React in all .tsx files, even if it’s not needed for Next.js

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:24

github_iconTop GitHub Comments

4reactions
ahnpnlcommented, Jan 4, 2020

this is not the issue with ts-jest because NextJs is a framework and it applies a “magic” to achieve what you describe. Making ts-jest framework-dependent shouldn’t be done.

3reactions
belgattitudecommented, Jan 1, 2021

@gabrielgrover @ahnpnl Since Typescript 4.1, there’s no more need for a transformer.

Simply add ``“jsx”: “react-jsx”… imports won’t be required anymore.

See a working example here: https://github.com/belgattitude/vercel-monorepo-test/pull/20

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Started | Next.js
System Requirements. Node.js 14.6.0 or newer; MacOS, Windows (including WSL), and Linux are supported. Automatic Setup. We recommend creating a new Next.js ......
Read more >
Supported Browsers and Features - Next.js
js supports modern browsers with zero configuration. Chrome 64+; Edge 79+; Firefox 67+; Opera 51+; Safari 12+. Browserslist.
Read more >
Next.js by Vercel - The React Framework
Next.js gives you the best developer experience with all the features you need for production: hybrid static & server rendering, TypeScript support, ...
Read more >
FAQ - Next.js
Get to know more about Next.js with the frequently asked questions.
Read more >
Basic Features: TypeScript - Next.js
Next.js supports TypeScript by default and has built-in types for pages and the API. You can get started with TypeScript in Next.js here....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found