James
James

Reputation: 281

Importing Jest Mock Modules from another file

Is there a way to import jest.mock modules from another file since it is used in many test files.

For example:

jestMock.tsx
   jest.mock('lib1',() => {});
   jest.mock('lib2',() => {});
   jest.mock('lib3',() => {});
   and so on

Test1.spec.tsx
   import * from 'jestMock.tsx'
   //call jest mocks here

Test2.spec.tsx
   import {jestMockLib1, jestMockLib2} from 'jestMock.tsx'
   // call jestMockLib1
   // call jestMockLib2

Thanks.

Upvotes: 1

Views: 420

Answers (1)

Jonathan Irwin
Jonathan Irwin

Reputation: 5747

Yes it is possible to re-use your mocks. Just wrap the mocks in an exported function and call that function in your test file.

// jestMock.tsx
export function mockSomething(){
   jest.mock('lib1',() => {});
   jest.mock('lib2',() => {});
   jest.mock('lib3',() => {});
}

// Test1.spec.tsx
import mockSomething from 'jestMock.tsx'

mockSomething()
   ...your tests

Upvotes: 1

Related Questions