nx-extensions icon indicating copy to clipboard operation
nx-extensions copied to clipboard

fix(react): unitTestRunner failed when jest

Open guyfedwards opened this issue 2 years ago • 8 comments

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.

guyfedwards avatar Aug 12 '22 15:08 guyfedwards

☁️ 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.

nx-cloud[bot] avatar Aug 12 '22 15:08 nx-cloud[bot]

Not sure but does something changes here with the latest version? How does the workspace package is needed as a dep for jest?

DominikPieper avatar Aug 12 '22 17:08 DominikPieper

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

guyfedwards avatar Aug 12 '22 20:08 guyfedwards

Here I’m not sure if it’s more a problem of the test setup. Cause two things here I don’t like:

  1. @newl/workspace should be automatically there in each workspace, so peerDependency should be enough. For unit tests we should have a closer look.
  2. 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

DominikPieper avatar Aug 13 '22 04:08 DominikPieper

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

DominikPieper avatar Aug 13 '22 09:08 DominikPieper

So you are saying @nrwl/workspace should be added to the package.json as part of createTreeWithEmptyWorkspace() testing util?

guyfedwards avatar Aug 14 '22 15:08 guyfedwards

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 avatar Aug 15 '22 07:08 DominikPieper

@DominikPieper added a createTestProject testing helper adding @nrwl/workspace to package.json for the react generator and updated the unit tests to use this

guyfedwards avatar Aug 15 '22 10:08 guyfedwards