question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using with Yarn workspaces

See original GitHub issue

It’s great that vscode-jest now supports VS Code’s Workspaces.
The problem is, the way VS Code Workspaces currently works is, one has to define each of the services in one’s monorepo as a VS Code workspace.

It’s a pretty tedious process for a project with 15+ services, and counting…
It’s obviously not vscode-jest’s fault, but I think there might be a rather simple way to support running jest in the right monorepo service without having to configure VS Code for Workspaces.

There are two key aspects to vscode-jest:

  1. Running all tests (+watch mode)
  2. Debugging a single test

1. Running all tests

For running all tests, I was able to run all my services tests, each with each own jest configuration, without having to add define each as workspace.

If you’re using Yarn Workspaces (or Lerna), all I had to do is change the run configuration like so:

"jest.pathToJest": "yarn workspaces run test"

That’s it!
Running this will make Yarn run yarn test in each of its workspaces, running jest all tests for services. So I think that part just works out of the box.

Here’s what needs tweaking to work -

2. Debugging a single test

OK so the idea here is similar, run the right Yarn workspace for the file we’re trying to debug:

// launch.json
    "configurations": [
        {
            "type": "node",
            "name": "vscode-jest-tests",
            "request": "launch",
            "runtimeArgs": ["workspace","${command:extension.yarnWorkspaceName}", "run", "test"], // ${yarnWorkspaceName} is what we're missing
            "args": [
                "--runInBand"
            ],
            "runtimeExecutable": "yarn"   // ...along with this one,
                                          // will run each Yarn workspace test script
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen",
            "disableOptimisticBPs": true,
        }
    ]

My initial I thought was, maybe we can have vscode-jest resolve that variable workspace via a command that would be added?

Would love your ideas here @connectdotz , wether that’s an approach that’s seems valid to you, or possibly help me think of alternatives? If you think this might be a good way to go I can have a look at implementing it in a PR.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:13
  • Comments:12

github_iconTop GitHub Comments

5reactions
shlomiassafcommented, May 3, 2020

I’ve managed to run debug with some help when running the process…

The issue I see here is that the runner will run the tests from the workspace root dir thus skipping any existing configuration specific for the project the file in context belongs to.

So, instead of running jest, I run a script and I send the ${fileDirname} to it so I can locate the proper configuration file and point to it and then I pass it on to jest…

 {
      "type": "node",
      "name": "vscode-jest-tests",
      "request": "launch",
      "args": [
        "--runInBand",
        "--fileDirname", // ADDING THE CURRENT FILENAME DIR FOR THE SCRIPT
        "${fileDirname}"
      ],
      "cwd": "${workspaceFolder}",
      "console": "integratedTerminal",
      "internalConsoleOptions": "neverOpen",
      "disableOptimisticBPs": true,
      "program": "${workspaceFolder}/tools/vscode-jest-tests/index.js" // THE SCRIPT
    }

tools/vscode-jest-tests/index.js (the script):

const Path = require('path');
const findUp = require('find-up'); // yarn add -D -W find-up

if (process.env.CI === 'vscode-jest-tests') {
  const index = process.argv.indexOf('--fileDirname');
  if (index > 0) {
    const [command, fileDirname] = process.argv.splice(index, 2);
    const match = findUp.sync(['jest.config.js', 'jest.config.json'], { cwd: fileDirname, type: 'file' });
  
    // if match and it's not outside the workspace:
    if (match && Path.dirname(match) >= process.cwd()) {
      process.argv.push('--config', match)
    }
  }
}

require('jest-cli/bin/jest');

This is simple, find the nearest jest configuration and make jest use it…

Works perfectly for me (monorepo, learn, yarn workspaces (1.2x))

2reactions
roni-frantchicommented, May 17, 2020

@tettoffensive here’s what works for us:

yarn lerna -- run test --parallel --no-bail -- --passWithNoTests --runInBand

Where

# package.json
"scripts": {
  "lerna": "lerna"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Workspaces - Yarn
Workspaces are a new way to set up your package architecture that's available by default starting from Yarn 1.0. It allows you to...
Read more >
Yarn Workspaces: Organize Your Project's Codebase Like A Pro
Yarn workspaces let you organize your project codebase using a monolithic repository (monorepo). The idea is that a single repository would ...
Read more >
Yarn workspaces — monorepo beginner's guide - Medium
Yarn Workspaces is a feature that allows users to install dependencies from multiple package.json files in subfolders of a single root package.json file,...
Read more >
Scaling out JavaScript Monorepos with Yarn Workspaces
Workspaces : Yarn can now manage dependencies across many independent projects in the same repository. Instead of having several big ...
Read more >
Yarn workspace tutorial, introduction and cheatsheet
It allows you to setup multiple packages in such a way that you only need to run yarn install once to install all...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found