Wlad
Wlad

Reputation: 25

Jest does not recognize external typescript module

I have some runtime importable module imports via AMD define. Everything is working as expected but I just cannot mock any runtime module inside the tests.

Error message without mocks: Cannot find module 'MyModule' from ... I also get the same error message when I try to jest.mock the module. just the error happens inside the test file then.

// globals.d.ts
declare module 'config' {
    const val: IConfig;
    export default val;
}
// main.ts
/// <reference path="./globals.d.ts" />
import config from 'config';
[...]
// main.spec.ts
import { main } from "./main.ts"
[...]

Upvotes: 0

Views: 745

Answers (1)

Estus Flask
Estus Flask

Reputation: 222309

A module that is mocked with jest.mock is expected to exist. Non-existent mocked modules should be labeled as virtual:

jest.mock('MyModule', () => ..., {virtual: true});

Upvotes: 3

Related Questions