`yarn rw test` Error: command doesn't run as of Apollo v3 upgrade
See original GitHub issueHi 👋 , I have a problem with a new redwood project.
I run the following commands:
yarn create redwood-app test
cd test
yarn rw g component Test
yarn rw test
And I get the next error:
Jest encountered an unexpected token
...
/home/rodrigo/workspace/test/node_modules/@apollo/client/react/hooks/useSubscription.js:1
import { __assign } from "tslib";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at compileFunction (<anonymous>)
at Runtime._execModule (../node_modules/jest-runtime/build/index.js:1179:56)
This is the output of yarn rw info
System:
OS: Linux 5.8 Arch Linux
Shell: 5.8 - /bin/zsh
Binaries:
Node: 12.18.3 - /tmp/yarn--1601732607280-0.7441083359073166/node
Yarn: 1.22.10 - /tmp/yarn--1601732607280-0.7441083359073166/yarn
Databases:
SQLite: 3.33.0 - /usr/bin/sqlite3
Browsers:
Firefox: 81.0
npmPackages:
@redwoodjs/core: ^0.19.1 => 0.19.1
Let me know if I can help somehow. Thanks in advance.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:9 (9 by maintainers)
Top Results From Across the Web
`yarn rw setup <cmd>`s are broken - RedwoodJS Community
I have alias rw="yarn rw" in my .zshrc . Either command is the same.
Read more >Migrating to Apollo Client 3.0 - Apollo GraphQL Docs
This article walks you through migrating your application to Apollo Client 3.0 from previous versions of Apollo Client. To illustrate the migration process, ......
Read more >error Command failed with exit code 1. when I try to run yarn
what you need to do is just simple: follow these steps: rm -rf node_modules; yarn cache clean; yarn; yarn start.
Read more >apollo-server-express - npm
This package has been deprecated. Author message: The `apollo-server-express` package is part of Apollo Server v2 and v3, which are now ...
Read more >Quickstart: RedwoodJS - Supabase
We won't use Prisma to connect to the Supabase Postgres database or Prisma ... from running any yarn rw prisma migrate commands and...
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 Free
Top 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
We did some diggin’ with @thedavidprice and found the underlying cause. The solution, however, requires making some decisions that affect parts of the codebase that I don’t quite understand at this point. I can contribute some clarity and hope that this advances the discussion.
Here’s what’s going on:
*.cjs.js
. Everything else is ESM.So, if they are following best practices, why do our tests break? Because we’re doing some weird stuff that assumes a few things that are no longer true. This is hard to explain in the abstract, so I’ll just explain it step by step, starting from the error.
This is the error we get:
browser/jest.setup.js
.jest.setup.js
. The fact that there is a vanilla Node.js process at the top is important to keep in mindtest/dist/index.js
viarequire()
web/dist/index.js
node_modules/@apollo/client/react/hooks/useSubscription.js
package.json
to find the “main” field (which is the CJS entry point) because our require specifier is “deep” (it goes beyond the top level “@apollo/client” and points directly to a folder “@apollo/client/react/hooks/…”)At this point, we could fix this by changing
web/dist/index.js
so it requires the CJS version of the file, like so:And this will work (actually, we need to make a few changes to other parts of that file for that variable to be exported correctly, but that’s not important at this point).
Of course, we know
web/dist/index.js
comes fromweb/src/index.ts
, so we should change this in the source file:And this will now lead to a transpiled
web/dist/index.js
code that won’t break when the jest process tries to load it.There might be a few issues with this approach (we need to validate this):
dist
, so ESM-modularity is already lost).web/dist/index.js
) is used in several contexts: Testing, the actual app (and maybe storybook?). I don’t know enough to know if modifying it this way will break thingsNow, there is one more place where Apollo imports must be changed. And this one is trickier:
web/src/graphql/withCell.tsx
Following the same logic above, we would need to replace it with something that, once transpiled, allows a Node.js process to correctly resolve it to a CJS file. For example, something like the following:
Unfortunately, Apollo Client does not export “Query” at the top level. The closest CJS file in the package is:
@apollo/client/react/components/components.cjs.js
.So the final (transpiled) code we need is something like this:
Which means that we would need to introduce some sort of try/catch or conditional check to know whether we import from
@apollo/client/react/components/components.cjs
or from@apollo/client/react/components/Query
Anyway. I can see ways of solving this, but I have some blind spots when it comes to the codebase and the different contexts in which files are loaded/transpiled/bundled, so at this point the best thing I can do is just share my findings.
My best “let’s get this fixed quickly” bet right now would be to teach the node process that ends up running Jest to handle ESM syntax when calling
require()
(this is different than the transpilation process that runs before Jest starts testing). Something like ts-node at the top-level might work.I thought @peterp mentioned to me during a conversation this week he could run the tests by removing all the Apollo Client imports. I’ll check. And I’m going to experiment with a few things now to see if I can isolate the error. There’s also a lot of related packages with upgrades available — possible we’re encountering a bug that’s been fixed.