Next.js 12.1 SWC + Relay + Jest not transpiling `graphql` tags
See original GitHub issueVerify canary release
- I verified that the issue exists in Next.js canary release
Provide environment information
Next 12.1
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Thank you for porting the relay-babel plugin to SWC! One issue i’m now running into is that my graphql
tags in jest test files are not being automatically transpiled, leading to this error:
Invariant Violation: graphql: Unexpected invocation at runtime. Either the Babel transform was not set up, or it failed to identify this call site. Make sure it is being used verbatim as `graphql`. Note also that there cannot be a space between graphql and the backtick that follows.
8 | const { renderWithRelay } = setupTestWrapper({
9 | Component: ArtistName,
> 10 | query: graphql`
Expected Behavior
I would expect that the same transforms applied to Next js app source code would also automatically be applied to tests.
In another post, it looks like a dev is switching back to babel-jest in tests in order to compile these bits as a work around.
To Reproduce
Setup automatic next.js jest support.
Then enable Relay compiler support in Next.js config:
const nextConfig = {
compiler: {
relay: {
src: "...",
artifactDirectory: "...",
language: "typescript",
},
},
}
module.exports = nextConfig
Write a test that includes the graphql
tag, then run test.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:12
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Advanced Features: Next.js Compiler
Jest. The Next. js Compiler transpiles your tests and simplifies configuring Jest together with Next. js including:
Read more >How to properly configure NextJS 12.2 w/ SWC, Jest, Eslint ...
I am using NextJS with SWC and have "extends": "next" in my .eslintrc file. Parsing error: Cannot find module 'next/babel'. This below is...
Read more >next: Versions | Openbase
Update react next tag: #43617; fix(jest): pattern when detecting packages to ... 12 days ago ... feat(next-swc/relay): Add javascript to language: #42894 ...
Read more >Next.js 12.1: On-demand ISR, SWC support for ... - Hacker News
Next.js 12.1: On-demand ISR, SWC support for styled-components, Relay, Jest ... my Next.js to version 12.1: https://github.com/ixartz/Next-js-Boilerplate.
Read more >facebook Code Example - Code Grepper
I am hoping that you will use it's positive side to make your self proud in future. facebook. javascript by Batman · Batman...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@wonu thanks for having a look at this 👏 I’ve opened a PR that adds
pagesDir
to the transformer: #36599Let me share my research.
Before
v12.1.6-canary.13
, js files were transformed, if using swc, in two ways (details are omitted):through
jest-transformer.js
swc/index.js::transformSync()
transform.rs::transform_sync()
transform.rs::exec_transform()
through
next-swc-loader.js
swc/index.js::transform()
transform.rs::transform()
transform.rs::schedule_transform()
lib.rs::custom_before_pass()
relay.rs
(`graphql`
TaggedTemplates is checked here)I can’t read Rust code, so don’t know what is the difference between
transform()
andtransform_sync()
. But the problem was there:jest-transformer.js
didn’t invokerelay.rs
.Now things have changed. Since
v12.1.6-canary.13
, theexec_transform()
has been removed and the two transformations are both usingschedule_transform()
, which meansjest-transformer.js
may work as expected.However there is another problem:
relay.rs
requirespages_dir
parameter (see #33918).next-swc-loader.js
setspagesDir
usingget-project-dir.ts
, so it is okay.But
jest-transformer.js
not setspagesDir
and it even cannot simply useget-project-dir.ts
:get-project-dir.ts
has been depending onnext.ts
since #34836, so just importingget-project-dir.ts
starts dev(default command) server, which is an unexpected side effect.getProjectDir()
needsdir
parameter passed in<dir>
argument in cli commands. If passing in<dir>
explicitly in cli, there’s no way to telljest-transformer.js
what<dir>
is.It is not smooth, but adding
pagesDir
parameter lets`graphql`
be processed correctly with Jest anyway. And it’s just an experiment like POC, so don’t try this at work 😃Hope this helps you understand what’s going on behind the scenes.