Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example with React #2

Closed
poteirard opened this issue Sep 18, 2020 · 10 comments
Closed

Example with React #2

poteirard opened this issue Sep 18, 2020 · 10 comments

Comments

@poteirard
Copy link

I'm trying to move from react-scripts to SWC.

The building part is fine with webpack but I'm struggling a bit with jest.

this is the config that I've tried so far in the package.json:

{
"jest": {
    "rootDir": "./src",
    "testMatch": ["<rootDir>/**/*.test.{ts,tsx,js,jsx}"],
    "moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
    "transform": {
      "^.+\\.tsx?$": ["@swc/ject"]
    },
    "testEnvironment": "node",
    "collectCoverageFrom": [
      "src/components/**/*.{ts,tsx}"
    ]
  },
}

Any clue how to configure it properly?

The error I get in every test is:

SyntaxError: Cannot use import statement outside a module

Thank you

@kdy1
Copy link
Member

kdy1 commented Sep 18, 2020

It seems like a bug of swc/jest.

Can you try module.type="commonjs" like shown below?

https://swc-project.github.io/docs/configuring-swc.html#common-js

@poteirard
Copy link
Author

Hey @kdy1 same error 😓

@kdy1
Copy link
Member

kdy1 commented Sep 18, 2020

I'll fix it soon

@kdy1
Copy link
Member

kdy1 commented Sep 18, 2020

I found that it works if rootDir is removed.
Also, if rootDir exists, process is not called.

@kdy1
Copy link
Member

kdy1 commented Sep 18, 2020

Found the root cause.
You need to add "<rootDir>/*.test.{ts,tsx,js,jsx}" if you use rootDir, because <rootDir>/File.tsx does not match **/*.tsx.

I also added an example.

"jest": {
"rootDir": "src",
"testMatch": [
"<rootDir>/**/*.test.{ts,tsx,js,jsx}",
"<rootDir>/*.test.{ts,tsx,js,jsx}"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"transform": {
"^.+\\.(t|j)sx?$": [
"../../.."
]
},
"testEnvironment": "node"
},

@kdy1 kdy1 closed this as completed Sep 18, 2020
@poteirard
Copy link
Author

Hey @kdy1 thanks for your fast reply.

I tried the same config and got this error:

● Validation Error:

  Module ../../.. in the transform option was not found.
         <rootDir> is: /Users/gerard.brull/DEV/react-components/src

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html

@kdy1
Copy link
Member

kdy1 commented Sep 18, 2020

You should use @swc/jest instead of ../../...

Example uses local dependency to check if current version works properly

@poteirard
Copy link
Author

ah yes! I'm stupid 😅 sorry about that!

@kdy1
Copy link
Member

kdy1 commented Sep 18, 2020

No problem

@poteirard
Copy link
Author

I'm spiking on migrating react-scripts with react-stylegudist to SWC in case someone is interested in this ZopaPublic/react-components#591

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants