Allow jest-cli to run jest with a specific project(s) configuration(s) declared from the projects key.
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.
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"
]
},
],
};
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
Related: #6189
What would the API look like?
I have a quick proposal:
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.--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?
@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.
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.
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
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 (:
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.
@yacinehmito Did start to look into this? Would be a great feature to have.
@thernstig Sorry I did not answer earlier. Yes I did look into it. A PR is currently open: #8612.
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?
Most helpful comment
@thernstig Sorry I did not answer earlier. Yes I did look into it. A PR is currently open: #8612.