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

Configure jest in a monorepo #2263

Closed
mann-david opened this issue Nov 6, 2019 · 8 comments
Closed

Configure jest in a monorepo #2263

mann-david opened this issue Nov 6, 2019 · 8 comments

Comments

@mann-david
Copy link

Attempting to run wallaby in a Rush (https://rushjs.io/) monorepo and it is failing trying to use jest.

I had a project running as a plain JS project with jest and wallaby successfully. Then I moved it into a Rush monorepo and it fails trying to use Jest. At this point, I've worked myself in circles and am afraid I've broken more than I've fixed. :-(

When I add testFramework: 'jest' to my wallaby.js config I get an error when starting wallaby: Failed to load configuration file: Test framework [jest@0.4.3] is not supported.

When I try to reference jest in a test (i.e. const z = jest.fn()) it fails saying jest is not defined.

Again, this worked before I moved it into the monorepo, so for some reason wallaby can't seem to find something it needs.

I've updated Wallaby Core to 1.0.778

Sample repo: https://github.com/mann-david/wallaby-error

Thanks.

@smcenlly
Copy link
Member

smcenlly commented Nov 6, 2019

Before attempting to start Wallaby, I tried to make sure that your tests were running outside of Wallaby.

After cloning your repo and running npm install, npx jest fails with:

● Validation Warning:

  Unknown option "presets" with value ["../../jest.config.js", "ts-jest"] was found. Did you mean "preset"?
  This is probably a typing mistake. Fixing it will remove this message.

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

● Validation Error:

  Module ts-jest in the transform option was not found.
         <rootDir> is: C:\Temp\wallaby-error\libs\test-lib

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

It also looks like your package.json is missing from the repo (I expect this is not intentional?).

Could you please update your repo so that fresh clone, npm install and then npx jest run your tests? At that point, we know everything is working outside of Wallaby and can get your tests working with Wallaby.

@mann-david
Copy link
Author

Thanks @smcenlly, I had fixed the "presets" error but must have undone it at some point. I'll fix that, but it didn't change the outcome before.

For the second one, my understanding of the rush monorepo approach is that there is not a package.json in the root - each project has its own. Rush approaches its projects as a series of separate, standalone entities with Rush as a management layer over top. I could be entirely wrong on this as I'm new to Rush, but I don't believe I got a root package.json when I went through their tutorial to create the project.

I also believe that you don't use npm/yarn at the root of the monorepo, but could use it within a given project folder.

I'll look into both of these and get back to you ASAP.

Thanks,

Dave

@mann-david
Copy link
Author

I've just gotten confirmation from the Rush team that it isn't "recommended" to run tests from the top-level of the repo. Getting that to work apparently involves some gyrations. Their comment was "Rush treats each project in the repo as an isolated component"

So, now the question becomes - can Wallaby be configured to just run the tests in a given project folder if I have the whole repo open? Do I just create a wallaby.js config file in the root of each project? Do I need to do anything special in VSCode to make it work?

Thanks.

@smcenlly
Copy link
Member

smcenlly commented Nov 7, 2019

Yes - you can run Wallaby on a given project folder, even if you have the entire repo open. Use the Wallaby.js Select Configuration command, and select Automatic Configuration <custom directory> and then select your project directory.

I'm not sure on the advice from the Rush team, but if you are using Jest within all of your projects, it should not be hard to get up and running as Jest can be configured to treat each test run as an isolated project. If you can provide us with a working Rush sample with multiple projects configured to use jest, we'd be happy to take a look. Please note that this may require package.json and some npm dependencies in the mono root.

@smcenlly smcenlly closed this as completed Nov 7, 2019
@mann-david
Copy link
Author

@smcenlly: here is a new repo: https://github.com/mann-david/wallaby-error2

This one has 2 projects in the libs folder. Individually, each of them works fine with Jest, but neither works for wallaby.

To test this out, you'll need to:

  1. install Rush: yarn global add @microsoft/rush
  2. Run rush update from the root
  3. You can now switch into either folder libs/sample1 or libs/sample2 and run npx jest. It should complete and each project will have 1 failing and 1 succeeding test
  4. Selecting the wallaby.js config file in either project and running the tests fails with [Error] Failed to load configuration file: Test framework [jest@0.4.3] is not supported

Let me know if you need anything else, or if you make any progress.

Thanks!

@smcenlly
Copy link
Member

smcenlly commented Nov 7, 2019

Thanks for the sample repo. Because you are using jest v24+, you are able to use our automatic configuration feature and no configuration files are required.

I have created a pull request that removes your wallaby.js configuration files. I have also added support to run all tests from your mono repo root.

After merging the pull request you will first need to run npm install or yarn install in your mono repo root.

To run all tests in your project, in VS Code run the command Wallaby.js: Select Configuration and then select Automatic Configuration <project directory>. To run tests for only one project, select Automatic Configuration <custom directory> and then select the lib folder that you want to run.

@mann-david
Copy link
Author

Thanks @smcenlly. I just tested this and things look good. There is still an warning in the Wallaby Console ("Unable to find the root of the project where ts-jest has been installed") but everything appears to work just fine.

@smcenlly
Copy link
Member

We hadn't seen this ts-jest warning message before. This isn't a wallaby issue, but a jest configuration problem (you get the same warning running npx jest from the command line).

I found a few ts-jest github issues with the same problem when using ts-jest with mono repos and symlinks. I tried the fix suggested here and the warning goes away. Add the ts-jest global config below to each of your nested jest.config.js files:

globals: {
  'ts-jest': {
    packageJson: 'package.json',
  },
},

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

No branches or pull requests

2 participants