Skip to content

Jest fails with universal-cookie error when frontend-base is in packages #287

Description

@bradenmacdonald

When I have frontend-base installed into the packages directory of frontend-app-instructor-dashboard, its tests fail with:

 FAIL  src/Main.test.tsx
  ● Test suite failed to run

    TypeError: _universalCookie.default is not a constructor

       8 | import { publish } from '../subscriptions';
       9 |
    > 10 | const cookies = new Cookies();
         |                 ^

Reason (thanks Claude!):

createConfig('test') sets rootDir: process.cwd() and leaves roots at its default ([rootDir]), so jest-haste-map crawls your whole app directory — including packages/frontend-base/. (It skips node_modules, so when frontend-base comes from npm as usual, its __mocks__ folders are invisible). Now that it's a real workspace directory, two of them get registered:

  • packages/frontend-base/runtime/__mocks__/universal-cookie.js
  • packages/frontend-base/shell/__mocks__/universal-cookie.js
    (the dist/ copies are excluded by modulePathIgnorePatterns, which is why it's exactly two.)

For a node module, a registered manual mock is applied automatically — no jest.mock() call needed. So every one of your test suites silently gets frontend-base's mock of universal-cookie, and that mock is:

module.exports = () => mockCookiesImplementation;

An arrow function has no [[Construct]], so runtime/i18n/lib.ts:10's new Cookies() throws _universalCookie.default is not a constructor.

A solution is to add roots: ['<rootDir>/src'], to jest.config.js, or to fix the mock(s) to support new properly like the code they're mocking.


Side node: jest-haste-map is always throwing warnings about duplicate mocks, and I'm unclear why we're using it at all.

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