Categories
JavaScript Answers

How to get the arguments called in a Jest mock function?

Sometimes, we want to get the arguments called in a Jest mock function.

In this article, we’ll look at how to get the arguments called in a Jest mock function.

How to get the arguments called in a Jest mock function?

To get the arguments called in a Jest mock function, we can use the toHaveBeenCalledWith method to check what a mocked function is called with.

For instance, we write

expect(mockedFunction).toHaveBeenCalledWith("foo", "bar");

to check the mockedFunction is called with 'foo' and 'bar' with toHaveBeenCalledWith.

Conclusion

To get the arguments called in a Jest mock function, we can use the toHaveBeenCalledWith method to check what a mocked function is called with.

Categories
JavaScript Answers

How to fix the SecurityError: localStorage is not available for opaque origins error with Jest?

Sometimes, we want to fix the SecurityError: localStorage is not available for opaque origins error with Jest.

In this article, we’ll look at how to fix the SecurityError: localStorage is not available for opaque origins error with Jest.

How to fix the SecurityError: localStorage is not available for opaque origins error with Jest?

To fix the SecurityError: localStorage is not available for opaque origins error with Jest, we can change our Jest config.

For instance, we add

"jest": {
  "verbose": true,
  "testURL": "http://localhost/"
}

to set testURL to the URL of the app being tested.

If we have multiple projects being tested, we write

module.exports = {
  verbose: true,

  projects: [{
    runner: 'jest-runner',
    testURL: "http://localhost/",

    // ...
  }]
}

to set the testURL for each project in projects to the URL of the app being tested.

Conclusion

To fix the SecurityError: localStorage is not available for opaque origins error with Jest, we can change our Jest config.

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.