Update 02-build-system.md

documentation how to run jest tests in intellij

Signed-off-by: Gasan <465806+gusega@users.noreply.github.com>
This commit is contained in:
Gasan
2025-01-22 12:02:55 +01:00
committed by GitHub
parent 08cb6ef94c
commit b8d72515b6
+27 -6
View File
@@ -598,14 +598,35 @@ For your productivity working with unit tests it's quite essential to have your
#### IntelliJ IDEA
1. Update Jest configuration template by:
At the moment it's not possible to run jest tests via jest command line. That is, if you run the following command from the project root:
```bash
NODE_OPTIONS='--no-node-snapshot --experimental-vm-modules' node node_modules/jest-cli/bin/jest.js --config packages/cli/config/jest.js
```
- Click on "Edit Configurations" on top panel
- In the modal dialog click on link "Edit configuration templates..." located in the bottom left corner.
- In "Jest package" you have to point to relative path of jest module (it will be suggested by IntelliJ), i.e. `~/proj/backstage/node_modules/jest`
- In "Jest config" point to your jest configuration file, use absolute path for that, i.e. `--config /Users/user/proj/backstage/packages/cli/config/jest.js --runInBand`
You will get an error `Unexpected token 'export'` complaining about one of the imports from the `jest.js` config file. But why does jest work then when running `backstage-cli repo test`?
The reason is that `packages/cli/bin/backstage-cli` first executes `require('@backstage/cli/config/nodeTransform.cjs');` that probably transforms config files so that jest can use them.
Even simply adding `require('@backstage/cli/config/nodeTransform.cjs');` to the beginning of `node_modules/jest/bin/jest.js`, will make jest command line above work.
2. Now you can run any tests by clicking on green arrow located on `describe` or `it`.
Happily, there is a better way to do it, because `backstage-cli repo test` is simply a starter of the `jest-cli` with extra parameters. That is, you can run jest from the command line via `backstage-cli repo test` like this:
```bash
NODE_OPTIONS='--no-node-snapshot --experimental-vm-modules' node node_modules/@backstage/cli/bin/backstage-cli repo test
```
`backstage-cli` adds `--passWithNoTests --watch --workerIdleMemoryLimit=1000M --config node_modules/@backstage/cli/config/jest.js` jest options.
So, to run it in intellij do the following:
1. Update Jest configuration template by:
1. Click on "Edit Configurations" on top panel
2. In the modal dialog click on link "Edit configuration templates..." located in the bottom left corner.
3. "Configuration file": leave empty (`backstage-cli` adds the config)
4. "Node options": `--no-node-snapshot --experimental-vm-modules`
5. "Jest package": `~/workspace/backstage/node_modules/@backstage/cli` - the location of the backstage cli package.
6. "Working directory": `~/workspace/backstage`
7. "Jest Options": `repo test --runInBand`
8. "Environment variables": `backstage-cli` adds `--watch` jest option, so if you want your tests to stop after you run them, add `CI=true`
2. Another issue is that if you right click on the test, intellij will create a playwright configuration, see [WEB-67720](https://youtrack.jetbrains.com/issue/WEB-67720/Jest-test-runs-as-playwright-test). So, to run jest tests create a configuration manually, the intellij will pick up the configuration template, provide there the test file. After intellij run tests in the file you can click on the individual tests in the run panel and re-run them, intellij will create a correct jest run configuration.
#### VS Code