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

jest 'setupFiles' with <rootDir> breaks libs/apps tests #2344

Closed
4 tasks done
Hotell opened this issue Jan 20, 2020 · 8 comments · Fixed by #3766
Closed
4 tasks done

jest 'setupFiles' with <rootDir> breaks libs/apps tests #2344

Hotell opened this issue Jan 20, 2020 · 8 comments · Fixed by #3766
Assignees
Labels
outdated scope: react Issues related to React support for Nx type: bug

Comments

@Hotell
Copy link
Contributor

Hotell commented Jan 20, 2020

Prerequisites

  • I am running the latest version
  • I checked the documentation (nx.dev) and found no answer
  • I checked to make sure that this issue has not already been filed
  • I'm reporting the issue to the correct repository (not related to React, Angular or any dependency)

Expected Behavior

Properly resolve rootDir when setupFiles are being used for jest config

Current Behavior

Let's say my root jest.config.js is using setupFiles for various reasons like

const rootConfig = {
  rootDir: '.',
  setupFiles: ['<rootDir>/test/jest/global.js']
}

now if we wanna run test for particular package which uses root as a preset, setupFiles are not found as jest sets root dir to that particular directory from where are those test executed (so instead of having root ../.. - workspace root, it's ./libs/my-lib

Failure Information (for bugs)

● Validation Error:

  Module <rootDir>/test/jest/global.js in the setupFiles option was not found.
         <rootDir> is: /Users/hotell/Projects/devel/workspace/tools
@wilsonparson
Copy link

I've also been trying to figure out how to get around this. We want to use the same setup for all of our Angular libraries and apps, so I've tried doing something like the following:

Global Jest config

// jest.config.js

module.exports = {
  // Configuration common to all Jest tests
}

Global Jest config for Angular projects

// jest.config.angular.js

module.exports = {
  // Configuration common to all Angular tests
  preset: './jest.config.js',
  setupFilesAfterEnv: [ './jest.setup.angular.js' ]
}

Global Jest setup file for Angular tests

// jest.setup.angular.ts

import 'zone.js/dist/zone.js'
import 'zone.js/dist/proxy'
import 'zone.js/dist/sync-test'
import 'zone.js/dist/async-test.js'
import 'zone.js/dist/proxy.js'

import 'jest-zone-patch'
import 'jest-preset-angular'

Project-level Jest config

// jest.config.js

module.exports = {
  preset: '../../../jest.config.angular.js',
  // other configurations for this particular project
}

When I run tests for the project I get the same error, where Jest is looking for the setup file from the context of the project, rather than at the root of the monorepo:

An unhandled exception occurred: ● Validation Error:

  Module ./jest.setup.angular.ts in the setupFilesAfterEnv option was not found.
         <rootDir> is: /Users/wparson/Work/monorepo/libs/components-library/payment-calculator

Is there a way I can tell Jest to use the root of the monorepo as the rootDir but only when it's looking for the setup file? Obviously I can reference the global setup file directly from the individual project-level Jest configs, but I wanted to comment here to see if there's a way to reference a setup file from the top-level Jest config.

@wilsonparson
Copy link

Just discovered Nx's Jest builder options: https://nx.dev/web/api/jest/builders/jest#jest

Adding the setupFile property to the project angular.json seems like the best solution for our use case:

"test": {
  "builder": "@nrwl/jest:jest",
  "options": {
    "jestConfig": "libs/components-library/payment-calculator/jest.config.js",
    "tsConfig": "libs/components-library/payment-calculator/tsconfig.spec.json",
    "setupFile": "jest.setup.angular.ts"
 }
}

We're still stuck with referencing the setup file in each individual project, but at least it's all in one file (angular.json) instead of spread across all of our project-level Jest configs.

@wrslatz
Copy link
Contributor

wrslatz commented Apr 16, 2020

See #2314 (comment) as I believe they are related.

I believe the ability to use setupFilesAfterEnv option in root jest.config.js is broken now due to jestjs/jest#9495... Apps or libs that provide an override to setupFilesAfterEnv will now fail if the file name or path differ between between the app/lib jest config and root jest config.

This breaks IDEs that use https://github.com/jest-community/vscode-jest as they need the setup file to pull in required setup resources because that extension is not able to run the underlying jest configs for each app / lib when running tests

@github-actions
Copy link

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs.
If we missed this issue please reply to keep it active.
Thanks for being a part of the Nrwl community! 🙏

@github-actions github-actions bot added the stale label May 29, 2020
@wrslatz
Copy link
Contributor

wrslatz commented May 29, 2020

We are still experiencing this issue in our monorepo

@vsavkin vsavkin removed the stale label May 29, 2020
@vsavkin
Copy link
Member

vsavkin commented May 29, 2020

Sorry. The issue was marked by mistake. I removed the label.

@joshfarrant
Copy link

Is there any progress on this? I'm still seeing the issue but I'm assuming I must be missing a simple fix.

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: react Issues related to React support for Nx type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants