NagRock / ts-mockito

Mocking library for TypeScript
MIT License
974 stars 93 forks source link

Mocking imports #180

Closed karlbennett closed 4 years ago

karlbennett commented 4 years ago

Is it possible to mock imports with this library? I can do this with Jest mock as follows: MyModule.ts

import { someFunction } from "some-module";

export const myFunction = (): string => someFunction();

MyModule.test.js

import { someFunction } from 'some-module';
import { myFunction } from './MyModule';

jest.mock('some-module');

beforeEach(() => {
  someFunction.mockClear();
});

test('Can mock an imported function.', () => {
  const expected = 'some string';

  // Given
  someFunction.mockReturnValueOnce(expected);

  // When
  const actual = myFunction();

  // Then
  expect(actual).toBe(expected);
});
NagRock commented 4 years ago

Not possible. And probably will not be possible as ts-mockito is not a test runner and can't override imports.