vscode-jest icon indicating copy to clipboard operation
vscode-jest copied to clipboard

Feature Request: Support having multiple test configs

Open yatki opened this issue 6 years ago • 11 comments

Expected Behavior

I have a project that in test folder I have unit, integration and functional tests. Each test folder has its own configuration and setup files.

It would be great to have possibility to map different configuration for different test folders.

Example:

{
  jest.instances: {
    "test/unit/*": "jest --config=./test/unit/config.js",
    "test/integration/*": "jest --config=./test/integration/config.js",
    "test/functional/*": "jest --config=./test/functional/config.js",
  }
}

So this way, for the test files under test/unit folder, extension will execute the corresponding command.

Actual Behavior

It doesn't support this at the moment.

yatki avatar Feb 27 '19 10:02 yatki

Seems like we can achieve this after this https://github.com/jest-community/vscode-jest/issues/129 issue is closed. But i don't see any action on it.

yatki avatar Feb 27 '19 10:02 yatki

I have the exact same setup and would love being able to configure that.

renestalder avatar Mar 01 '19 11:03 renestalder

@yatki I wonder if the issue is order is reversed, that this option could be implemented in way that closes #129.

jsphstls avatar Apr 10 '19 14:04 jsphstls

Any news about this feature?

saeidzebardast avatar Aug 15 '19 10:08 saeidzebardast

I have a very similar problem. In my opinion, the best way to solve this would be to take the config file closest to the current test file as the responsible config file.

FERNman avatar Sep 23 '19 07:09 FERNman

I think this could potentially help with monorepo projects where the CLI is configured to run jest using jest.config.js files in subdirectories.

See https://github.com/nrwl/nx/issues/2803 and https://github.com/nrwl/nx/issues/2344

wrslatz avatar Apr 18 '20 11:04 wrslatz

Done with https://jestjs.io/docs/en/configuration#projects-arraystring--projectconfig like 3+ years ago

askirmas avatar Sep 06 '20 08:09 askirmas

#129 basically supports vscode's multiroot workspaces, which can be used for monorepo project (each package can be configured as a folder).

I can see a few possible ways to do this today:

  1. create a vscode folder for each of the test folder. Then configure each folder to use its own jest.pathToJest or jest.pathToConfig
  2. try to use @askirmas suggestion of --projects to treat them as separate projects. Make sure you can run the CLI in terminal first, then customize the jest.pathToJest to use the same command.

Please let us know if any of the above works for you.

connectdotz avatar Sep 06 '20 15:09 connectdotz

@connectdotz I have some issues settings 'propagation' in multiroot mode. But additional thing to check for 1st point - in launch options use some vscode path variable that points to current (sub) workspace, not root/main

askirmas avatar Sep 06 '20 15:09 askirmas

If jest config is not common for all subprojects, then their all other configs should be different - extensions, settings, launches. So rather natural to supply them own 'scope' with workspace

askirmas avatar Sep 06 '20 16:09 askirmas

Following (as @askirmas suggested) works out of the box with jest. I adapted everything according to @yatki's needs:

const createProject = (type) => ({
  transform: {
    '^.+\\.(t|j)sx?$': 'ts-jest',
  },
  globals: {
    'ts-jest': {
      isolatedModules: true,
    },
  },
  displayName: type,
  preset: 'ts-jest',
  globalSetup: `./__setup__/${type}/jest.global-setup.ts`,
  setupFilesAfterEnv: [`./__setup__/${type}/jest.setup-after-env.ts`],
  testRegex: `(/test/${type}/.*| (\\.| /)(test|spec))\\.[jt]sx?$`,
});

module.exports = {
  projects: [
    createProject('unit'),
    createProject('integration'),
    createProject('functional'),
  ],
};

With that config debug works just as expected on each folder.

JPeer264 avatar Oct 01 '21 13:10 JPeer264