Hone logo
Hone
Problems

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:

  1. package-a can successfully import and use functions exported from package-b.
  2. The import path used in package-a correctly resolves to the location of package-b within the monorepo structure.
  3. That the package.json files 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-a and assert that the imported functions from package-b are 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-a can successfully import and call functions from package-b.
  • The import path in package-a is correctly configured to resolve to package-b.
  • The package.json files correctly declare the dependency relationship between package-a and package-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 moduleNameMapper configuration option to help Jest resolve module paths within the monorepo.
  • You may need to adjust the tsconfig.json files in each package to ensure proper TypeScript compilation.
  • Think about how to structure your Jest configuration file (jest.config.js or similar) to accommodate the monorepo structure.
  • Start with a simple test case and gradually add complexity.
  • The workspaces field in the root package.json is crucial for tools like Yarn or npm to understand the monorepo structure. While not directly tested, ensure your setup is compatible with this structure.
Loading editor...
typescript