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

Expose a way for jest-cli to run jest with a specific project(s) #7542

Closed
natealcedo opened this issue Dec 21, 2018 · 11 comments · Fixed by #8612
Closed

Expose a way for jest-cli to run jest with a specific project(s) #7542

natealcedo opened this issue Dec 21, 2018 · 11 comments · Fixed by #8612

Comments

@natealcedo
Copy link
Contributor

natealcedo commented Dec 21, 2018

🚀 Feature Proposal

Allow jest-cli to run jest with a specific project(s) configuration(s) declared from the projects key.

Motivation

The motivation for this is to allow for different jest runners to be executed along with its own configuration. Currently this is only possible if different directories have it's own jest config. But in the case that you only have one jest config and your different configurations exist through the projects key, this can't be done.

Example

Given a jest config, I want to be able to run jest with the configuration using jest-runner-eslint and jest-runner-prettier.

jest.config.js

module.exports = {
  projects: [
    {
      displayName: 'SPEC',
      clearMocks: true,
      coverageDirectory: 'coverage',
      coveragePathIgnorePatterns: ['/node_modules/', '/constants/'],
      moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
      testEnvironment: 'node',
      testPathIgnorePatterns: ['/node_modules/'],
      testMatch: ['<rootDir>/src/**/*.(spec|test).js'],
    },
    {
      displayName: 'E2E',
      clearMocks: true,
      coverageDirectory: 'coverage',
      coveragePathIgnorePatterns: ['/node_modules/', '/constants/'],
      moduleFileExtensions: ['js', 'json', 'jsx', 'node'],
      testEnvironment: 'node',
      testPathIgnorePatterns: ['/node_modules/'],
      testMatch: ['<rootDir>/test/**/*.(e2e).js'],
    },
    {
      displayName: 'LINT',
      runner: 'jest-runner-eslint',
      testMatch: ['<rootDir>/src/**/*.js', '<rootDir>/test/**/*.js'],
    },
    {
      displayName: 'PRETTIFY',
      "runner": "jest-runner-prettier",
      "moduleFileExtensions": [
        "js",
        "jsx",
        "json",
        "ts",
        "tsx",
        "css",
        "less",
        "scss",
        "graphql",
        "md",
        "markdown"
      ],
      "testMatch": [
        "**/*.js",
        "**/*.jsx",
        "**/*.json",
        "**/*.ts",
        "**/*.tsx",
        "**/*.css",
        "**/*.less",
        "**/*.scss",
        "**/*.graphql",
        "**/*.md",
        "**/*.markdown"
      ]
    },
  ],
};

Pitch

I believe that this feature should bring good value to jest as a whole since it ties into the idea of having jest as a platform and takes advantage of the capabilities of the different jest runners

@SimenB
Copy link
Member

SimenB commented Dec 21, 2018

Related: #6189

@yacinehmito
Copy link
Contributor

yacinehmito commented Jun 26, 2019

What would the API look like?

I have a quick proposal:

  • Introduce projectName in the config. If displayName is not specified, it uses the value of projectName. If projectName is not specified, then it is project_$N where $N is the index in the array.
  • Introduce the --runProject CLI argument. It takes any number of projects as parameters. They are matched using projectName. We could define -p as a possible shorthand for --runProject.

Example:

module.exports = {
  projects: [{
    projectName: 'unit'
    ...<some stuff>
  }, {
    projectName: 'e2e',
    ...<some stuff>
  }]
}

To only run the unit tests, we would execute jest --runProject unit or jest -p unit.

This is a very quick proposal to get the discussion started. I haven't thought through all the implications.

UPDATE: projectName doesn't seem required as there's already a name attribute for project config. However it is not documented and I can't find where it's used in the codebase. Do you have a clue?

@yacinehmito
Copy link
Contributor

yacinehmito commented Jun 26, 2019

@SimenB From #6189 I understand that you would be up for it (or at least something similar). I'll try something out immediately. I won't mind some help in figuring out a suitable API and possible edge cases that need to be considered.

@conartist6
Copy link
Contributor

Cannot wait for this. I always have to go edit the jest config just to run a damn test in the debugger. Then the edited config gets checked in sometimes. There has got to be a better way.

@SimenB
Copy link
Member

SimenB commented Aug 20, 2019

You can use https://github.com/jest-community/jest-watch-select-projects in watch mode. Not sure if it helps in your case or not, though

@niieani
Copy link
Contributor

niieani commented Aug 22, 2019

In our case, we have a jest.config.js and switch the preset we run on our monorepo with a process environment variable:

module.exports = {
  preset: process.env.JEST_PROJECT === 'integration' ? 'jest-test-integration' : 'jest-test-code',
}

Then we run jest like:

JEST_PROJECT=integration jest

to select the project (:

@jonaskello
Copy link

This would be great :-). I want to have a centralised jest.config.js with projects in my monorepo but be able to run each project separately if needed. Today I guess the only way is to put separate jest.config.js in every package. To me, one of the benefits of monorepo is that you can centralize at lot of the development config to the top level instead of having it in every package.

@thernstig
Copy link
Contributor

@yacinehmito Did start to look into this? Would be a great feature to have.

@yacinehmito
Copy link
Contributor

@thernstig Sorry I did not answer earlier. Yes I did look into it. A PR is currently open: #8612.

@dyaa
Copy link

dyaa commented Jan 31, 2020

Was searching around and I found this thread, So thanks to everyone here, Even though I have a small proposal regarding the --runProjects CLI argument

What about have --ignoreProjects as well so that if want to skip a project by project name? any opinions?

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants