At the moment, my Next.js project utilizes automatic imports configured through jsconfig.json
in the main directory:
{
"typeAcquisition": {
"include": ["jest"]
},
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/components/*": ["components/*"],
"@/functions/*": ["functions/*"],
"@/styles/*": ["styles/*"],
"@/pages/*": ["pages/*"]
}
},
"exclude": ["node_modules"]
}
When attempting to incorporate Jest testing within a test
directory at the root level, the tests do not reference the root directory. I have experimented with:
- creating a
jest.config.js
file that points to the root directory - adding
typeAcquisition
to the existingjsconfig.js
- including a separate
jsconfig.js
within thetests
directory.
I am uncertain about the correct approach, or how to effectively configure this setup as none of these methods have been successful for me. In order for the tests to execute, I must eliminate imports altogether and resort to using ../../
for navigating through the directories, necessitating changes throughout the nested files as well - for instance: Within pages/api/budget
, a call is made to functions/api/fetchBudget
. To ensure accessibility for Jest testing, import statements in both files must be switched to the standard ../../
syntax instead of relying on @pages/..
or @functions
from the setup.
TL;DR: How can I establish Jest testing to comply with the root directory specified in jsconfig.json
? Alternatively, what would be the appropriate process for enabling Jest testing by utilizing its own dedicated jsconfig.json
?