I'm working to set up tests for a React app with Jest. The app works, currently, but has no tests.
I set up a test/ folder in the root of the package and followed the sum.test.js example from the documentation, successfully. Then I put a test/ folder in a directory in the app code - in this case app/models/test. models contains a file called user.tsx that contains a single function: insertUser
I created a file in app/models/test called user.test.tsx and put in the following content
import { insertUser } from "../user.server.tsx";
insertUser();
Then I get a test failure with the following message:
Cannot find module '~/utils/db.server' from 'app/models/user.server.ts'
Require stack:
app/models/user.server.ts
app/models/test/insertUser.test.ts
1 | import type { User } from "@prisma/client";
2 | import { MatchStatus } from "@prisma/client";
> 3 | import { db } from "~/utils/db.server";
| ^
4 |
5 | export async function insertUser(email: User["email"]) {
6 | return db.user.insert({
at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:487:11)
at Object.<anonymous> (app/models/user.server.ts:3:1)
at Object.<anonymous> (app/models/test/insertUser.test.ts:1:1)
It looks like I need to tell Jest where the '~' points to. I've been scouring the documentation but I can't see how to do that. Any ideas?
Thank you!