nx-extensions
                                
                                 nx-extensions copied to clipboard
                                
                                    nx-extensions copied to clipboard
                            
                            
                            
                        fix(react): unitTestRunner failed when jest
when passing jest as the unitTestRunner there was no
@nrlw/workspace version in paackage.json to base the version off for
@nrwl/jest.
This fixes that by adding a package.json to the test setup so that there
is always @nrwl/workspace: 0.0.0 present which mimics the non-test
behaviour.
☁️ Nx Cloud Report
CI is running/has finished running commands for commit fa6bf8a663f378bae8cf96bb71aabc67ec081cbb. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.
📂 See all runs for this branch
| 🟥 Failed Commands | 
|---|
| nx affected --target=e2e --parallel 1 | 
✅ Successfully ran 5 targets
Sent with 💌 from NxCloud.
Not sure but does something changes here with the latest version? How does the workspace package is needed as a dep for jest?
Not sure if this is something that has changed in a recent version @DominikPieper.
We were having issues with our own generator tests and looking at the source code in packages/react/src/generators/init/init.spec.ts noticed that there was not a test case for having jest enabled, just two cases for having it disabled.
Adding that test then saw the same failure we were seeing in our codebase and so I added the fix for the test and a new testcase.
You can see the logic for why the workspace package is needed in packages/react/src/generators/init/lib/add-jest-plugin.ts and packages/react/src/generators/init/lib/util.ts.
The logic seems to be getting the current version of @nrwl/workspace to work out the version of @nrwl/jest to add. In the test file tree, there is no @nrwl/workspace in the package.json
Here I’m not sure if it’s more a problem of the test setup. Cause two things here I don’t like:
- @newl/workspace should be automatically there in each workspace, so peerDependency should be enough. For unit tests we should have a closer look.
- with this change the @nrwl/workspace is added in a specific version. If the user has a different Nx version then there will be mixed versions what could be lead to problems
I took a look at the areas in code. It should be enough to define the workspace version in the unit test setup for that. No need to add workspace here
So you are saying @nrwl/workspace should be added to the package.json as part of createTreeWithEmptyWorkspace() testing util?
You can just add a working package.json like here: https://github.com/nxext/nx-extensions/blob/main/packages/solid/src/generators/utils/testing.ts That should work
@DominikPieper added a createTestProject testing helper adding @nrwl/workspace to package.json for the react generator and updated the unit tests to use this