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

Add support for yarn2 #2351

Closed
ImBrek opened this issue Feb 21, 2020 · 20 comments
Closed

Add support for yarn2 #2351

ImBrek opened this issue Feb 21, 2020 · 20 comments

Comments

@ImBrek
Copy link

ImBrek commented Feb 21, 2020

Issue description or question

Hi, do wallaby works with yarn2 and PnP API?. If yes, can you get me some info about config?

@smcenlly
Copy link
Member

Wallaby doesn't currently provide first-class support for yarn's PnP feature but does allow you to specify your node_modules locations (described in our docs). If configuring your node_modules location doesn't help you, please let us know what error(s) you're getting and provide some details of your PnP config/structure and we'll be able to help you out.

@drptbl
Copy link

drptbl commented Feb 26, 2020

I've tested yarn v2 with so many projects.. none of them work at it's current state.

@smcenlly
Copy link
Member

As of today, we have set up and spent a little time investigating yarn2 and its PnP API and can confirm that Wallaby doesn't support yarn2 at this point in time. We will update the title of the issue and mark this as a feature request.

We were looking for a few simple projects that use yarn2 and have tests configured and couldn't find much in the way of examples or instructions for configuring. If you have a sample project (e.g. that uses yarn2 with jest), we'd appreciate you sharing with us.

@smcenlly smcenlly changed the title Configure wallaby with yarn 2 Add support for yarn2 Feb 27, 2020
@smcenlly
Copy link
Member

Last week, we spent the last 3 days completing our investigation on how to add yarn2 support to Wallaby.

We were hoping we would be able to implement this in a couple of days but it looks like it will take at least 1-2 weeks for us to get this working properly.

We have existing short term commitments/plans that we need to complete before starting this work. We expect to know where this will fit on our schedule this in about 3-4 weeks time.

If you're reading this comment, please +1 the issue description to let us know you're interested in this feature (and have others you know who are interested do so as well).

@mdreizin
Copy link

@smcenlly Do you have any updates to share?

@smcenlly
Copy link
Member

@mdreizin - at this stage, we have some other features and enhancements planned out until mid-December and don't expect to revisit yarn2 support until then.

@mdreizin
Copy link

@smcenlly Thank you for sharing this.

@zombopanda
Copy link

zombopanda commented Nov 9, 2020

If anyone is looking, I was able to use yarn 2 pnp in my Create React App/Craco/Typescript project:

  • Make sure all peer dependencies of Jest is installed.
  • Add to .yarnrc.yml
packageExtensions:
  jest-config@*:
    dependencies:
      jest-runtime: "*"
  • Create yarn wrapper for NodeJS (e.g. ./bin/node.sh), make executable (chmod +x).
    • If you are using NVM:
#!/bin/bash
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

node -r ./.pnp.js "$@"
    • If you are using global NodeJS:
#!/bin/bash
node -r ./.pnp.js "$@"
  • Add to package.json (or wallaby.config.js):
 "wallaby": {
    "autoDetect": true,
    "testFramework": {
      "configFile": "./jest.config.js"
    },
    "env": {
      "type": "node",
      "params": {
        "runner": "-r ./.pnp.js"
      }
    }
  },
  • In Jest config set:
    testRunner: require.resolve("jest-jasmine2/build/index")
  • Then select wrapper as Wallaby NodeJS path in your IDE
    • WebStrorm: Set Node.js path in the Wallaby run config.
    • VSCode: Put following in settings.json (for user or for workspace): (thanks to @smcenlly and @Saeris)
      "wallaby.node": "./bin/node.sh"

@smcenlly
Copy link
Member

smcenlly commented Nov 9, 2020

@zombopanda - thanks for sharing

@schickling
Copy link

I can confirm @zombopanda's solution.

@smcenlly @ArtemGovorov do you have any update on the timeline for supporting Yarn 2/PnP out of the box? Would be greatly appreciated!

@Saeris
Copy link

Saeris commented Jan 4, 2021

I'm unable to get @zombopanda's solution working. Still getting the following in my VSCode output:

‌[Error] Automatic Jest configuration error: Module jest-cli is not found in '.'. 

I have the following in ./bin/node.sh:

#!/bin/bash
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

node -r ../.pnp.js "$@"

and this in ./package.json:

  "wallaby": {
    "autoDetect": true,
    "testFramework": {
      "configFile": "./jest.config.js"
    },
    "env": {
      "type": "node",
      "runner": "./bin/node.sh",
      "params": {
        "runner": "-r ./.pnp.js"
      }
    }
  }

Followed the other instructions as well, just listing what I've specifically changed. It wasn't entirely clear where I should be placing the shell script, nor do I know how to select the NodeJS wrapper in the Wallaby run config (is this something specific to the WebStorm plugin? Doesn't seem to be an equivalent for VSCode...). Would really love to get this working!

@smcenlly
Copy link
Member

smcenlly commented Jan 4, 2021

The final step:

  • Then select wrapper as NodeJS path in the Wallaby run config (I'm using WebStorm).

is a little different in VS Code. In VS Code, you will need to set the node path in VS Code's Wallaby Configuration, >Preferences: Open User Settings (or workspace if you prefer) and then search for wallaby.node

image

A relative path should work but if it does not, please try providing the absolute path to your shell script. Please also ensure that your shell script has executable permissions (chmod +x).

@Saeris
Copy link

Saeris commented Jan 4, 2021

Thanks! This was what I needed. Also useful to note that this can be saved to ./.vscode/settings.json like the following:

{
  // ... other settings...
  "wallaby.node": "./bin/node.sh"
}

so that you can share this essential config with your team.

@doublemarked
Copy link

I was able to get this working without so much added configuration. I did only two things,

  1. Configured in .vscode/settings.json: "wallaby.node": ".vscode/node.sh"
  2. Created a file at .vscode/node.sh with the following contents:
#!/bin/sh
yarn node "$@"

This relies on yarn to stand up the correct environment.

Step #2 could be skipped if the wallaby.node would allow you to configure a command with parameters, like yarn node.

@andrew-w-ross
Copy link

andrew-w-ross commented Mar 7, 2021

@doublemarked Thanks so much that worked for me.

To anyone trying to figure this out in the future two things to watch out for:

Looks like wallaby.node setting is relative to the configuration file so if you're using a mono repo and attempt to run a specific package with Select Configuration -> Automatic Configuration it's not going to find the file. Quickest fix is to give it a absolute path with ~/.node.sh just make sure the setting for it is saved as a user setting.

If you get an error like jest-config tried to access jest-runtime, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound. To fix this you'll have to set pnpMode to loose in .yarnrc,yml. After changing the pnpMode run yarn command for the changes to take effect.

@Saeris
Copy link

Saeris commented Mar 18, 2021

This solution appears to no longer be working for me. Haven't worked in my yarn 2 repo for a few weeks, settings haven't changed AFAIK since I'm the only one on my team that uses Wallaby at the moment. When I try to start it up, I now get the following error in VSCode:

Can not start node.js process (./bin/node.sh), make sure your system has node.js installed.

node -v shows v12.19.0, Wallaby runs in my other, non-yarn 2 repos without any hiccups. Has there been a recent change to how the wallaby.node VSCode setting resolves files? That's the only thing I can think of that would have suddenly introduced this problem.

As @doublemarked pointed out, I believe if we could just opt into using yarn node this would solve the resolution errors specific to yarn 2 and eliminate the need to create a shell script that aliases calls to node in the first place.

Edit: It works again if I use an absolute path to node.sh but this isn't a viable solution as I need to be able to easily share this with my team, meaning relative paths must work for wallaby.node.

@smcenlly
Copy link
Member

@Saeris - this is working for me. Perhaps try setting the absolute path to your node.sh to see if that solves your problem?

Also, can you share your node.sh contents? If you are using nvm then please be sure to use the version of the config specified in the original post.

@smcenlly
Copy link
Member

We have added native support for yarn2. If you have customized your node path and node runner env settings as per the original post above, you do not need to make any changes. If you wish to undo those changes, please do so and then read through docs.

Please note that depending on your project type, you may need to resolve dependency issues by modifying your .yarnrc.yml (details for doing this are specified in our docs). This is certainly required for jest, with our current recommended settings shown below:

yarnPath: ".yarn/releases/yarn-berry.cjs"
packageExtensions:
  jest-config@*:
    dependencies:
      jest-runtime: "*"
  jest-cli@*:
    dependencies:
      "@jest/reporters": "*"
      "@jest/console": "*"

@arcanis
Copy link

arcanis commented May 28, 2021

Out of curiosity, are those extensions needed because Wallaby injects code into the jest-config source codes themselves (if so, are those changes open-source?)? Or are those valid requires that Jest should list?

@smcenlly
Copy link
Member

The package extensions are required because Yarn 2 requires dependencies used by a particular module to be explicitly listed. When Wallaby runs your code using jest as a testing framework, it imports various functions from jest, including getting the jest configuration. Normally for an application you would simply list the dependencies that you require but Wallaby is dynamically loading your dependencies (depending on your project/configuration/etc.) which is why the setting is required.

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

No branches or pull requests

10 participants