Mike Taatgen
Mike Taatgen

Reputation: 48

How to test alias import

If I have something like:

import { buildPath as buildSRPPath } from '@/router/srp-routes';

Do I mock it Like:

jest.mock('@/router/srp-routes', () => ({
  buildPath: jest.fn()
}));

Or do I need buildSRPPath?

Upvotes: 2

Views: 364

Answers (1)

Brian Adams
Brian Adams

Reputation: 45780

jest.mock replaces the module with a mock so the mock should contain what the module exports.

Example:

// ---- lib.js ----
export const func = () => {
  return 'func called';
}



// ---- code.js ----
import { func as somethingElse } from './lib';

export const useFunc = () => {
  return somethingElse();
}



// ---- code.test.js ----
import { useFunc } from './code';

jest.mock('./lib', () => ({
  func: () => 'mocked func'  // mock 'func' since that is what lib exports
}));

test('useFunc', () => {
  expect(useFunc()).toBe('mocked func');  // PASSES
});

So in your case you are correct to use buildPath.

Upvotes: 1

Related Questions