9

TL;DR

How to translate a node script like this:

"test": "NODE_ENV=test riteway -r @babel/register 'src/**/*.test.js' | tap-nirvana",

to use SWC instead of Babel?


Context

We recently upgraded our Next.js version. Next.js now supports SWC instead of Babel.

The unit tests for React in our project are written with RITEway. The test command is:

"test": "NODE_ENV=test riteway -r @babel/register 'src/**/*.test.js' | tap-nirvana",

It transforms the files with Babel first because otherwise import statements and JSX would cause errors.

During our attempts to migrating to SWC, we had no luck with the CLI. However, we found the @swc-node/register package. It allowed us to transform our command like this:

"test": "riteway -r @swc-node/register src/**/*.test.js | tap-nirvana"

which fixes new syntax like the import statement and a test like this would run:

import { describe } from 'riteway';

describe('my test', async assert => {
  assert({
    given: 'true',
    should: 'be true',
    actual: true,
    expected: true,
  });
});

However, tests for React components like this

import { describe } from 'riteway';
import render from 'riteway/render-component';

import Authentication from './user-authentication-component';

describe('user authentication component', async assert => {
  const $ = render(<Authentication />);

  assert({
    given: 'just rendering',
    should: "render 'Authentication'",
    actual: $('*:contains("Authentication")').text(),
    expected: 'Authentication',
  });
});

still throw the following error:

$ yarn test
yarn run v1.22.17
$ riteway -r @swc-node/register src/**/*.test.js | tap-nirvana
/Users/user/dev/learning-flow/node_modules/@swc/core/index.js:135
        return bindings.transformSync(isModule ? JSON.stringify(src) : src, isModule, toBuffer(newOptions));
                        ^

Error: error: Expression expected
 
  |
7 |   const $ = render(<Authentication />);
  |                                    ^

error: Expression expected
 
  |
7 |   const $ = render(<Authentication />);
  |                                     ^

error: Unexpected token `)`. Expected this, import, async, function, [ for array literal, { for object literal, @ for decorator, function, class, null, true, false, number, bigint, string, regexp, ` for template literal, (, or an identifier
 
  |
7 |   const $ = render(<Authentication />);
  |                                      ^



Caused by:
    0: failed to process js file
    1: Syntax Error
    at Compiler.transformSync

How can we create that command so that it runs with SWC?

Simon
  • 15
  • 4
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
  • I also have hundreds of tests file with `.js` extension, all with JSX (testing React code) and am looking for a way for SWC to treat them as `jsx`... – vsync Jan 18 '23 at 15:00
  • I've [opened a ticket/question on Github](https://github.com/swc-project/swc/issues/6834) regarding `.js` extension – vsync Jan 18 '23 at 15:21
  • Same issue with `vite-plugin-react-swc` – hungify Mar 03 '23 at 08:15

2 Answers2

6

After some experimentation I found out that it works if you import React from 'react'; in every file (both the component as well as the test) and change the file extensions to .jsx as described in the docs.

However, this is unsatisfying, as we'd like to use React 17's JSX transform feature to avoid having to import React in every file. Additionally, we'd like to keep all file endings .js.

We tried setting .swcrc without any luck so far.

I'm posting this answer here in case no way to configure .swcrc can be found.

J. Hesters
  • 13,117
  • 31
  • 133
  • 249
  • Did you manage to find a way to do this without having to import? – Charklewis Mar 03 '22 at 10:21
  • @Charklewis yes, see this question https://stackoverflow.com/questions/70899604/swc-with-javascript-how-to-handle-css-imports-and-how-to-absolute-imports?noredirect=1&lq=1 – J. Hesters Mar 06 '22 at 00:55
4

I'll assume your question is only about jest and not about the webpack setup for swc.

I've never used swc myself in jest so this is just a shot in the dark, but I found a package for jest called @swc-node/jest which allows you to use a transformer like:

module.exports = {
  transform: {
    '^.+\\.(t|j)sx?$': ['@swc-node/jest'],
  },
}

Which should allow you to transpile all [jt]sx? imports.

There's also a different one called @swc/jest which seems to do the same thing.

I'd start with trying those.

Your issue is that jest isn't able to parse your code, for example if you were using typescript you'd need something like ts-jest in order to parse your TS for you, I think in this case you need a swc/jest transpiler which should first compile your code and output it to memory, then funnel it to jest to run it.

The alternative is that you could compile the tests before hand using swc, then run jest on the compiled files as a separate step. At least with TS you can do that, first compile everything using tsc and then run jest on the .js output. I'm sure swc should work the same.

As to why the @swc-node/register package you're using isn't working, I have no idea.

Roman Mkrtchian
  • 2,548
  • 1
  • 17
  • 24
  • 1
    Hi Matriarx Thank you for your help! Unfortunately, I'm using RITEway instead of Jest. So while I'm sure what you're saying helps with Jest, I'm looking for ways to make it work with RITEway. – J. Hesters Nov 16 '21 at 10:18
  • Ah alright. Even if you are using something else the concept stays the same, you'll need something to transpile your code to javascript before you can use it. If you can't find a package that exists for the one you're using like ````swc/jest````, then I guess the only thing I can think of is for you to run ````swc````, compile your code, then run your tests as a second step. –  Nov 16 '21 at 10:21