Skip to content

Resolve alias in dependecy monorepo package #8568

@draftitchris

Description

@draftitchris

💬 Questions and Help

Right now we are developing a react application that makes use of a monorepo using yarn workspaces and typescript paths.

I have two packages both whom have jest tests. Package A depends on Package B and B is independent.

Running tests on package B (a component library) is no problems and within package B I have typescript aliases aka compilerOptions.paths. Jest has no problems handling these with moduleNameMapper.

However the issue arises when I try to run jest tests on Package A which imports components from Package B. When the component in B is imported it has its own local dependencies and jest seems unable to resolve Package B alias mappings when running tests in A.

Example:

Root jest.config.js:

module.exports = {
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
  testRegex: "(/tests/.*|(\\.|/)(test|spec))\\.tsx?$",
  roots: ['<rootDir>/tests'],
  moduleNameMapper: {
    "@someAlias/(.+)$": "<rootDir>../src/$1",
    "@foo/packageB(.*)$": "<rootDir>/../packageB/src/$1",
  },
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
};

Package B.

./packages/packageB/src/components/Component1.tsx

export const Component1 = () => <div>helloWorld </div>

src/components/Component2.tsx

import Component1 from '@someAlias/components'
export const Component2 = () => <div><Component1/></div>

./packages/packageB/jest.config.js

module.exports = {
  ...baseConfig,
  displayName: 'packageB',
  moduleNameMapper: {
    ...baseConfig.moduleNameMapper,
  },
  rootDir: './'
};

Running tests for Package B is NO PROBLEMS :D

Package A

./packages/packageA/src/components/Component1.tsx

import {Component2} from '@foo/packageB'
export const Component1 = () => <div><Component2/></div>

./packages/packageA/jest.config.js

module.exports = {
  ...baseConfig,
  displayName: 'packageA',
  moduleNameMapper: {
    ...baseConfig.moduleNameMapper,
  },
  rootDir: './'
};

When running tests from here I get an error

 ● Test suite failed to run

  TypeScript diagnostics (customize using `[jest-config].globals.ts-jest.diagnostics` option):
  ../packageB/src/components/Component2/Component2.tsx:5:26 - error TS2307: Cannot find module '@someAlias/components'.

  5 import { ListItem } from '@someAlias/components';

So it appears as though jest is not able to resolve module name aliases outside of its own rootDirectory. If this is the case, is there a canonical way of resolving this issue? How do i get a dependency packages own aliases to resolve in order to run my tests?

Please note that this issue tracker is not a help forum and this issue will be closed.

For questions or help please see:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions