redwood: Errors on import while running jest
After upgrading to v0.34.0 I’ve found myself with all my tests failing on this kind of error:
FAIL api api/src/services/products/products.test.ts
● Test suite failed to run
Cannot find module '../../../../src/lib/db' from 'src/services/products/products.test.ts'
> 1 | import { db } from 'src/lib/db'
| ^
2 | import { disciplines } from 'src/services/disciplines/disciplines'
3 | import { levels } from 'src/services/levels/levels'
4 | import {
at Resolver.resolveModule (../node_modules/jest-resolve/build/index.js:306:11)
at Object.<anonymous> (src/services/products/products.test.ts:1:1)
The error in itself is that imports which use to work no longer work, eventhough files have not changed. This happens within tests files but also with imports that are in regular services. Thoses services work well with dev server started.
I’ve ran tests on a new project and it doesn’t seem to be related to v0.34.0 on new installs, but it could happen on upgrading projects.
Here are some relevant files:
// api/jest.config.js
module.exports = require('@redwoodjs/testing/config/jest/api')
// api/jsconfig.json
{
"compilerOptions": {
"noEmit": true,
"allowJs": true,
"esModuleInterop": true,
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"baseUrl": "./",
"rootDirs": [
"./src",
"../.redwood/types/mirror/api/src"
],
"paths": {
"src/*": [
"./src/*",
"../.redwood/types/mirror/api/src/*"
],
"types/*": [
"./types/*"
]
},
"typeRoots": [
"../node_modules/@types",
"./node_modules/@types"
],
"types": [
"jest"
]
},
"include": [
"src",
"../.redwood/types/includes/all-*",
"../.redwood/types/includes/api-*"
]
}
// api/tsconfig.json
{
"compilerOptions": {
"noEmit": true,
"allowJs": true,
"esModuleInterop": true,
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"baseUrl": "./",
"rootDirs": [
"./src",
"../.redwood/types/mirror/api/src"
],
"paths": {
"src/*": [
"./src/*",
"../.redwood/types/mirror/api/src/*"
],
"types/*": [
"./types/*"
]
},
"typeRoots": [
"../node_modules/@types",
"./node_modules/@types"
],
"types": [
"jest"
]
},
"include": [
"src",
"../.redwood/types/includes/all-*",
"../.redwood/types/includes/api-*",
"types/*.d.ts"
]
}
// babel.config.js
module.exports = {
presets: ['@redwoodjs/core/config/babel-preset'],
}
And finally, the test file:
import { db } from 'src/lib/db'
import { disciplines } from 'src/services/disciplines/disciplines'
import { levels } from 'src/services/levels/levels'
import {
attributeDefaultMonitorToCourse,
courseDefaultMonitor,
} from 'src/services/monitors/monitors'
import { productStub } from 'src/services/products/utilities'
import {
createCourse,
createProduct,
product,
products,
updateProduct,
} from './products'
describe('products', () => {
scenario('Retrieve a product', async (scenario) => {
mockCurrentUser(scenario.user.manager)
const p = await product({ id: scenario.product.one.id })
expect(p.label).toMatch('Product label')
expect(p.basePrice).toEqual(30)
})
/** I'll spare you the other tests. */
})
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (9 by maintainers)
I’ve got a fix for this in #2938
This seems to be a problem with the moduleNameMapper. Worked around it by overwriting it. The below is in the api folder. Probably the same in the web folder, as all my tests were failing after update.
@noire-munich thank you for reporting this! I’ve labeled as a bug and queued up for priority.
@peterp done! Thanks.
Thanks @nnennajohn , that one works for me on v0.35.0.
Hi @thedavidprice Thanks, it doesn’t seem to be the same issue. I removed node_modules, installed again, same bug pops up. Once the test process is done ( can’t force quit apparently when it’s buggy ), i’ll try with
install --forcefor the sake of it & report.@thedavidprice if this is the case, please assign to me, and I’ll make sure that it works as expected