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

Nested projects are ignored #5463

Closed
deftomat opened this issue Feb 5, 2018 · 13 comments
Closed

Nested projects are ignored #5463

deftomat opened this issue Feb 5, 2018 · 13 comments
Labels

Comments

@deftomat
Copy link

deftomat commented Feb 5, 2018

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
Running jest in monorepo ignores projects in packages.

Minimal repo: https://github.com/deftomat/jest-nested-projects-issue

What is the expected behavior?
Should run all projects in all packages. In another words: Should run nested projects.

Environment
Jest: v22.1.4
Node: v8.9.4
OS: macOS High Sierra

@ranyitz
Copy link
Contributor

ranyitz commented Feb 5, 2018

Hey @deftomat

You can also configure jest to run different configurations in different directories, for example, to achieve the desired behavior in your example project, put this as the main jest.config.js file:

module.exports = {
  projects: [
    {
      displayName: "test",
      testMatch: ["<rootDir>/packages/**/*.js"]
    },
    {
      displayName: "lint",
      runner: "jest-runner-eslint",
      testMatch: ["<rootDir>/packages/first/**/*.js"]
    }
  ]
};

If you want to run jest in each repo individually you can use a tool like lerna. Configure a test script in your package.json and use lerna run test to run the test command in each directory.

@thymikee
Copy link
Collaborator

thymikee commented Feb 5, 2018

Whoa, didn't expect such usage to be honest. Projects nested inside other projects, projectception!
It's definitely not a bug, but a lack of feature. We should first discuss if we want projects to be used this way.

@cpojer
Copy link
Member

cpojer commented Feb 7, 2018

I think I'd be happy for this to be supported. We just need to change the project merging to be recursive.

@rolandjitsu
Copy link

I was under the impression that when a jest root config has projects and you run it, it will actually use a different configuration file/json for each project if the project has one:

E.g.:

// Run jest at the root where the config is {projects: ['<rootDir>/packages/*']}
> jest

// jest will go through each project and find a different config file and use that instead
packages/package-a
    jest.config.js
packages/package-b
    jest.config.js

Is my assumption wrong?

@deftomat
Copy link
Author

deftomat commented Feb 22, 2018

@rolandjitsu

Well, kind of. We, for example, using TypeScript in a few packages and to make it works, these packages have a necessary config. Global Jest's config doesn't know anything about TS.

So, it looks like Jest is using a package's config, however, it ignores some parts. Like in this issue, it ignores projects properties and also, as mentioned here, it ignores global setup/teardown.

Jest is awesome and probably the best test framework but projects feature is highly unpredictable. At least, for me 😉

@brad-decker
Copy link

When i do something similar to this:

module.exports = {
  projects: [
    {
      displayName: "test",
      testMatch: ["<rootDir>/packages/**/*.js"]
    },
    {
      displayName: "lint",
      runner: "jest-runner-eslint",
      testMatch: ["<rootDir>/packages/first/**/*.js"]
    }
  ]
};

in my own package with the exception of using multiple runners (i'm interested in running tests in multiple projects from the root dir), jest runs matching every single file in the entire repo. It also applies the babel version of root instead of using each packages

@lehni
Copy link

lehni commented Jul 9, 2018

@brad-decker as a workaround for a related problem I have created fork of babel-jest. Please let me know if this works for you: https://github.com/lehni/babel-jest-nested

There is also a pending PR to get this merged into babel-jest that I am hoping will get some attention: #6617

@nathanforce
Copy link

nathanforce commented Apr 6, 2020

@cpojer @thymikee This is an old one but is there still interest in supporting nested projects?

My specific use case is a monorepo with a structure like:

apps
   - app-one
      - jest.config.json
   - app-two
      - jest.config.json
packages
   - shared-package # used in both apps
      - jest.config.json
   - design-system # used in both apps
      - jest.config.json
   - utils # use in app-one and shared-package (transitive dep of app-two via shared-package)
      - jest.config.json

We are wanting to have our apps each define jest.config.json with projects set to include all local dependencies of the app. This would allow us to test the app and any direct/transitive dependencies.

I'd be fine to take on the work but might need somebody to point me in the direction of Jest's config resolution code.

@JoelEinbinder
Copy link
Contributor

I would very much like this feature as well.

@nathanforce the code in question looks to be here: https://github.com/facebook/jest/blob/aa64672e728738926571746a85c57d1658bdaf55/packages/jest-config/src/index.ts#L280
It appears to be taking the project list from the command line args or top level projects array, but doesn't expand this array with subprojects.

@SimenB
Copy link
Member

SimenB commented Apr 18, 2020

Still happy to merge a PR with this. Link above is a good start - recursively going through all globalConfig.projects until they're empty arrays seems like it should work?

@nihalgonsalves
Copy link

I wrote a small root config generator that creates a meta-project-list and sets the appropriate <rootDir> for each project.

Perhaps this is something that could be adapted slightly and incorporated into Jest? There are limitations of course, but for my simple Jest config this works perfectly.

// root jest.config.js

const path = require('path');
const glob = require('glob');

const WORKSPACE_GLOB = './packages/*/jest.config.js';

const projects = glob.sync(path.join(__dirname, WORKSPACE_GLOB)).flatMap((jestConfigPath) => {
  const packageDir = path.dirname(jestConfigPath);
  const packageName = path.basename(packageDir);

  const config = require(jestConfigPath);

  // If there are no subprojects, use the entire config as a project
  const subprojects = config.projects ?? [config];

  return subprojects.map(({ displayName, ...projectConfig }) => ({
    displayName: `${packageName}${displayName ? `-${displayName}` : ''}`,
    rootDir: packageDir,
    ...projectConfig,
  }));
});

module.exports = {
  projects,
};

Some open questions and limitations:

  • The WORKSPACE_GLOB could come from the root jest.config.js, which could then look like this:

    module.exports = {
      projects: ['./packages/*/jest.config.js'],
    }
  • For my use case, I simply generated the displayName by prefixing the original name with the name of the directory that contains the sub-jest.config.js. Perhaps there's a better way to do this?

  • Limitations with how this works currently:

    • If one of the sub-jest.config.js files uses a plain config, it must be a config that's a subset of a project config
    • For older node environments, .flatMap will have to be replaced with an imperative forEach / push, or another implementation

@github-actions github-actions bot added the Stale label Feb 25, 2022
@github-actions
Copy link

This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@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 Apr 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests