Properly resolve rootDir when setupFiles are being used for jest config
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
● Validation Error:
Module <rootDir>/test/jest/global.js in the setupFiles option was not found.
<rootDir> is: /Users/hotell/Projects/devel/workspace/tools
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:
// jest.config.js
module.exports = {
// Configuration common to all Jest tests
}
// jest.config.angular.js
module.exports = {
// Configuration common to all Angular tests
preset: './jest.config.js',
setupFilesAfterEnv: [ './jest.setup.angular.js' ]
}
// 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'
// 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.
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.
See https://github.com/nrwl/nx/issues/2314#issuecomment-614826694 as I believe they are related.
I believe the ability to use setupFilesAfterEnv option in root jest.config.js is broken now due to https://github.com/facebook/jest/pull/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
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! 🙏
We are still experiencing this issue in our monorepo
Sorry. The issue was marked by mistake. I removed the label.
Most helpful comment
Sorry. The issue was marked by mistake. I removed the label.