Edited Question with vazsonyidl suggestions applied I have to write unit tests for a function similar to this one: As functionC belongs to another module, I placed a mock of it in the _mocks_folder: I have mocked functionB in two diferent ways for testing the then and the catch : What I am trying to do is expect that functionC
Tag: jestjs
SyntaxError: Cannot use import statement outside a module, how to run test unit with react-native-svg for the Web?
I have the following error when I run yarn test: My jest.config.js: I expect when babel to transpile what’s in node_modules/react-native-svg-web. Instead, babel does not transpile and I have the casual transpilation error for dependencies that are not transformed: SyntaxError: Cannot use import statement outside a module Why is the transformation not working? How can I test my web app
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 And in try this in my test : But it returns TypeError: Cannot set property ‘post’ of undefined I want to mock that method to handle error and response to test methods inside them Answer Use
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: But I cannot find any combination of doMock, default over named exports, mockImplementation to get the following to work: Edit 1 Based on
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 the best way to approach the issue. That is the mock. I have tried moving that into a const and adding: mockFun.unless
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 trying to write a test using Jest to check that: The component doesn’t render anything when it receives an empty array The component renders the
Preventing “not wrapped in act(…)” Jest warning when state update doesn’t affect UI
I’m trying to figure out if there is a way to prevent the “not wrapped in act(…)” warning thrown by Jest/testing-library when I have nothing to assert after the state update that causes the warning happens, or if I should just ignore this warning. Suppose I have this simple component: Suppose I want to simply test that this component renders
Jest failing async test
I am getting the current error I think the issue comes down to this line of code If I change that code to this I no longer get the error. I need this function to return null so I can test the failing code. Here is the method I am testing I have seen a few posts online stating that
Jest expect to array contains element
Let’s suppose that I have the following code: And I want to check if id is in arrayOfIds Something like this How can I do this with Jest? Answer Use .toContain when you want to check that an item is in an array. For testing the items in the array, this uses ===, a strict equality check.
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 works, Promises called etc. just can’t figure how to test that ‘deleteMessage’ is called In my test i use