Jest Unit Testing function that calls a second one that returns a promise

Edited Question with vazsonyidl suggestions applied I have to write unit tests for a function similar to this one: import {External} from ‘ExternalModule’; async functionA(){ this.functionB()….

mock post method npm request module with jest

I know that npm request module is now deprecated, but i want to mock a post http call with jest. Here is my function import { post } from ‘request’; export functionToFetch(uriFetching) { return …

Jest change mock implementation for a POJO module between tests

I am trying to mock the import of a Plain Old Javascript Object in a test, where I want different implementations for each test. If I mock at the top of the file it works as expected: import { …

TypeError: jwt(…).unless is not a function when mocking with Jest

I am trying to mock the .unless function of express-jwt and currently have this mock setup to compare a JWT with an admin and non admin. This worked perfect until we added .unless and I am not sure …

Jest tests keep failing for React component that renders various HTML elements based on type by using switch statement

I have a React Component that takes an array and iterates over each node, styling and rendering HTML elements based on the types found within the array. I have everything running properly and now I’m …

How to test method called in promise

I have a function that saves an entity and in the promise i want to check the delete is called. In my jest test it whizzes past and can’t think how to test it Here is the partial snippets, it all …

SyntaxError: Unexpected token ‘

I am running tests with jest and enzyme in my React application, and while my simple sanity check test (2+2 expect 4) works, this error is thrown when I go to shallow render a component. It also …

Testing Cross Browser Extension With Jest, How To Mock Chrome Storage API?

After putting off testing for a while now due to Cypress not allowing visiting chrome:// urls, I decided to finally understand how to unit/integration test my extension – TabMerger. This comes after …

JEST | Assert a function was called inside addEventListener callback

I have the following function: Code to test export default function main() {     const createAndAppendPTag = () => {         const p = document.createElement(‘p’);         document.body.appendChild(…

Jest: How to undo a global mock for certain tests in a file

I want to mock Math.random for certain tests and use its original implementation for other tests. How can I achieve this? I’ve read about using jest.doMock and jest.dontMock, but I’ve bumped into a …