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.

Next.js 12.1 SWC + Relay + Jest not transpiling `graphql` tags

See original GitHub issue

Verify 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:closed
  • Created 2 years ago
  • Reactions:12
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
timneutkenscommented, May 1, 2022

@wonu thanks for having a look at this 👏 I’ve opened a PR that adds pagesDir to the transformer: #36599

2reactions
wonucommented, May 1, 2022

Let me share my research.

Before v12.1.6-canary.13, js files were transformed, if using swc, in two ways (details are omitted):

I can’t read Rust code, so don’t know what is the difference between transform() and transform_sync(). But the problem was there: jest-transformer.js didn’t invoke relay.rs.

Now things have changed. Since v12.1.6-canary.13, the exec_transform() has been removed and the two transformations are both using schedule_transform(), which means jest-transformer.js may work as expected.

However there is another problem: relay.rs requires pages_dir parameter (see #33918).

next-swc-loader.js sets pagesDir using get-project-dir.ts, so it is okay.

But jest-transformer.js not sets pagesDir and it even cannot simply use get-project-dir.ts:

  • get-project-dir.ts has been depending on next.ts since #34836, so just importing get-project-dir.ts starts dev(default command) server, which is an unexpected side effect.
  • getProjectDir() needs dir parameter passed in <dir> argument in cli commands. If passing in <dir> explicitly in cli, there’s no way to tell jest-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.

Read more comments on GitHub >

github_iconTop 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 >

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