Categories
JavaScript Answers

How to mock dependency in Jest with TypeScript?

Sometimes, we want to mock dependency in Jest with TypeScript.

In this article, we’ll look at how to mock dependency in Jest with TypeScript.

How to mock dependency in Jest with TypeScript?

To mock dependency in Jest with TypeScript, we can use the jest.mock method.

For instance, we write:

import * as dep from '../dependency';
jest.mock('../dependency');

const mockedDependency = <jest.Mock<typeof dep.default>>dep.default;

it('should do what I need', () => {
  mockedDependency.mockReturnValueOnce('return');
})

to call jest.mock to mock the ../dependency module.

And we set mockedDependency to dep.default with type <jest.Mock<typeof dep.default>>.

typeof dep.default sets the data type of mockedDependency to the type of the ../dependency module automatically.

Then we call mockedDependency.mockReturnValueOnce to mock the return value of mockedDependency.

Conclusion

To mock dependency in Jest with TypeScript, we can use the jest.mock method.

Categories
JavaScript Answers

How to use actual function in a module when module is mocked in Jest?

Sometimes, we want to use actual function in a module when module is mocked in Jest.

In this article, we’ll look at how to use actual function in a module when module is mocked in Jest.

How to use actual function in a module when module is mocked in Jest?

To use actual function in a module when module is mocked in Jest, we can use the jest.requireActual method.

For instance, we write:

import {
  otherFn
} from './myModule.js'

jest.mock('./myModule.js', () => ({
  ...(jest.requireActual('./myModule.js')),
  otherFn: jest.fn()
}))

describe('test category', () => {
  it('tests something about otherFn', () => {
    otherFn.mockReturnValue('foo')
    expect(otherFn()).toBe('foo')
  })
})

to mock myModule with jest.mock but use the actual members in myModule to with jest.requireActual.

Then we mock otherFn in the same module by setting otherFn to jest.fn.

And then in our test, we call otherFn.mocklReturnValue to mock otherFn‘s return value.

Finally, we check otherFn‘s return value with toBe.

Conclusion

To use actual function in a module when module is mocked in Jest, we can use the jest.requireActual method.

Categories
JavaScript Answers

How to fix console.log statements output nothing in Jest?

Sometimes, we want to fix console.log statements output nothing in Jest.

In this article, we’ll look at how to fix console.log statements output nothing in Jest.

How to fix console.log statements output nothing in Jest?

To fix console.log statements output nothing in Jest, we can set the silent option to false.

For instance, we run

npm run test -- --silent=false

to set silent to false given that test is a script in package.json that runs jest.

Conclusion

To fix console.log statements output nothing in Jest, we can set the silent option to false.

Categories
JavaScript Answers

How to change mock implementation for a single test with Jest?

Sometimes, we want to change mock implementation for a single test with Jest.

In this article, we’ll look at how to change mock implementation for a single test with Jest.

How to change mock implementation for a single test with Jest?

To change mock implementation for a single test with Jest, we can call the mockImplementation method on the function we want to mock in our test.

For instance, we write

import {
  funcToMock
} from './module';
jest.mock('./module');

beforeEach(() => {
  funcToMock.mockImplementation(() => {
    /* default implementation */
  });
});

test('with new mock function', () => {
  funcToMock.mockImplementation(() => {
    /* implementation specific to this test */
  });
  // ...
});

to call funcToMock.mockImplementation with the mock function as a callback to mock funcToMock in the beforeEach callback to provide a mocked version of funcToMock for all tests.

Then in the 'with new mock function' test, we call funcToMock.mockImplementation again with another function to mock the function for this specific test.

Conclusion

To change mock implementation for a single test with Jest, we can call the mockImplementation method on the function we want to mock in our test.

Categories
JavaScript Answers

How to disable console inside unit tests with Jest?

Sometimes, we want to disable console inside unit tests with Jest.

In this article, we’ll look at how to disable console inside unit tests with Jest.

How to disable console inside unit tests with Jest?

To disable console inside unit tests with Jest, we can use the silent option or disable console output in the config.

We run

jest --silent

to run tests without showing console output.

In jest setup file, we can add

global.console = {
  log: jest.fn(),
  error: console.error,
  warn: console.warn,
  info: console.info,
  debug: console.debug,
};

to replace console.log with a stubbed function by setting global.console.log to jest.fn.

The other console methods are set to the regular console methods.

Conclusion

To disable console inside unit tests with Jest, we can use the silent option or disable console output in the config.