Monorepo Testing with Jest: Package Dependency Verification
Monorepos, where multiple packages reside within a single repository, are increasingly common for managing complex projects. Ensuring that packages within a monorepo correctly interact and depend on each other is crucial for stability and maintainability. This challenge asks you to implement a Jest testing setup that verifies package dependencies within a simulated monorepo environment.
Problem Description
You are tasked with creating a Jest testing environment for a simplified monorepo consisting of two TypeScript packages: package-a and package-b. package-a depends on package-b. The goal is to write Jest tests that verify:
package-acan successfully import and use functions exported frompackage-b.- The import path used in
package-acorrectly resolves to the location ofpackage-bwithin the monorepo structure. - That the
package.jsonfiles correctly declare the dependencies.
The monorepo structure is as follows:
monorepo/
├── package-a/
│ ├── src/
│ │ └── index.ts
│ └── package.json
├── package-b/
│ ├── src/
│ │ └── utils.ts
│ └── package.json
└── package.json (monorepo root)
package-a/src/index.ts will import a function from package-b/src/utils.ts. The tests should confirm this import works as expected.
Key Requirements:
- Use Jest and TypeScript.
- Configure Jest to resolve module paths correctly within the monorepo.
- Write tests that import
package-aand assert that the imported functions frompackage-bare correctly called and return expected values. - The tests should run from the monorepo root.
- The tests should not require any external dependencies beyond Jest and TypeScript.
Expected Behavior:
The tests should pass if:
package-acan successfully import and call functions frompackage-b.- The import path in
package-ais correctly configured to resolve topackage-b. - The
package.jsonfiles correctly declare the dependency relationship betweenpackage-aandpackage-b.
Edge Cases to Consider:
- Incorrect import paths.
- Missing dependencies in
package.json. - Typographical errors in module names.
- Circular dependencies (though this challenge doesn't explicitly require handling them, consider how your setup would behave).
Examples
Example 1:
// package-b/src/utils.ts
export function add(a: number, b: number): number {
return a + b;
}
// package-a/src/index.ts
import { add } from './../package-b/src/utils';
console.log(add(2, 3)); // Expected output: 5
Example 2:
// package-a/package.json
{
"name": "package-a",
"version": "1.0.0",
"dependencies": {
"package-b": "1.0.0"
}
}
// package-b/package.json
{
"name": "package-b",
"version": "1.0.0"
}
// package.json (root)
{
"name": "monorepo",
"version": "1.0.0",
"private": true,
"workspaces": [
"packages/*"
]
}
Constraints
- The solution must be written in TypeScript.
- The solution must use Jest.
- The tests must be runnable from the monorepo root directory.
- The solution should be relatively concise and easy to understand.
- The solution should not introduce any unnecessary dependencies.
- The solution should handle relative paths correctly.
Notes
- Consider using Jest's
moduleNameMapperconfiguration option to help Jest resolve module paths within the monorepo. - You may need to adjust the
tsconfig.jsonfiles in each package to ensure proper TypeScript compilation. - Think about how to structure your Jest configuration file (
jest.config.jsor similar) to accommodate the monorepo structure. - Start with a simple test case and gradually add complexity.
- The
workspacesfield in the rootpackage.jsonis crucial for tools like Yarn or npm to understand the monorepo structure. While not directly tested, ensure your setup is compatible with this structure.