This is the function I am testing (stripped down for simplicity’s sake): populate.js-> The function this function calls: populate-template-utilities -> And my unit test (again stripped down): I want createSessionID to return ‘PAX123456’ and think mockID should do it, but it’s erroring with: Answer The spyOn method needs at least two parameters: object and methodName. Try sth like this: It
Tag: mocking
How to test code with Jest that uses readonly fields on mocks of libraries (such as ws socket.readyState)?
I want to unit test this JS code with Jest. It’s using the ws websocket library: Socket.js I want to check the error is thrown. For that to happen, I create a mock for the socket and then need to set the readyState of the socket object to CLOSED. The issue is that this is impossible because it’s a readonly
JavaScript/Node – Why isn’t my jest mock being called?
I am writing a unit test for a pubsub component in my service. My directory structure looks like this: The file publish_to_topic.ts looks like this And is called by publish_event.ts like so: Lastly in my test file publish_event.test.ts, I set up a mock: Alas, my test fails: Can anyone explain where I’ve gone wrong? Thank you. Answer You have your
How to mock instance methods of a class mocked with jest.mock?
How can the instance methods be mocked for a class that is being mocked with jest.mock? For example, a class Logger is mocked: Answer Automatic Mocking Calling jest.mock automatically mocks all the exports from the module being mocked unless a manual mock is specified using the __mocks__ directory. So, this line jest.mock(“./Logger”) has automatically replaced the Logger constructor and all
How to mock a constructor instantiated class instance using jest?
Given a class Person that instantiates & uses another class Logger, how can the Logger’s method be verified to be called when Person is instantiated in the example below? One option is to pass Logger as a constructor argument like below: However, is there any other way without changing the constructor to complete the test? Answer You can use jest.mock(moduleName,
Testing window.location in jest
I am trying to test the window functionality via jest. I have a function which appends a query param to the url and redirects the page to the other page. A rough snippet of the code is below: I am new to jest and I am facing a bit of difficulty simulating the mock test for this, hence can you
How to mock nanoid for testing?
I’m trying to mock nanoid for my testing but it doesn’t seem to be working. my function My test: So basically I’m struggling to figure out how to mock the nanoid which is generated inside the function. I’ve tried the following at the top of the file: however it doesn’t work at all. Any help would be appreciated! Answer You
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
TypeError: nock(…).persist(…).log is not a function
I am trying to get more info on why my nock mock is not right, but I can’t make the persist().log() work. Here is the test : Stackoverflow wants me to add some more details to be able to post this question, but I don’t know what to tell you more than that. Answer .log was removed in Nock v13