Questions tagged [spyon]

116 questions
74
votes
7 answers

TypeError during Jest's spyOn: Cannot set property getRequest of # which has only a getter
I'm writing a React application with TypeScript. I do my unit tests using Jest. I have a function that makes an API call: import { ROUTE_INT_QUESTIONS } from "../../../config/constants/routes"; import { intQuestionSchema } from…
J. Hesters
  • 13,117
  • 31
  • 133
  • 249
62
votes
5 answers

jest spyOn not working on index file, cannot redefine property

I have UserContext and a hook useUser exported from src/app/context/user-context.tsx. Additionally I have an index.tsx file in src/app/context which exports all child modules. If I spyOn src/app/context/user-context it works but changing the import…
Code Spirit
  • 3,992
  • 4
  • 23
  • 34
40
votes
4 answers

Jasmine spyOn with specific arguments

Suppose I have spyOn($cookieStore,'get').and.returnValue('abc'); This is too general for my use case. Anytime we call $cookieStore.get('someValue') --> returns 'abc' $cookieStore.get('anotherValue') --> returns 'abc' I want to setup a spyOn so…
eeejay
  • 5,394
  • 8
  • 29
  • 30
19
votes
4 answers

Unit testing with private service injected using jasmine angular2

I have a problem trying to unit test an angular service. I want to verify that this service is properly calling another service that is injected into it. Lets say I have this ServiceToTest that injects ServiceInjected: ServiceToTest…
RjHiruma
  • 193
  • 1
  • 1
  • 4
13
votes
3 answers

Jasmine.js Testing - spy on window.navigator.userAgent

I need to find the way to change userAgent value. I tried to spyOn the window.navigator.userAgent. But that's not helping. JS: @Injectable() export class DetectBrowserService { browserIE: boolean; constructor() { this.browserIE =…
user3319778
  • 281
  • 1
  • 2
  • 9
11
votes
2 answers

Jest spy on component's property method

I'm trying to test if an event has been added in the init method called by componentDidMount, but that event is going to be added only if a component's attribute is set in "true" so I want to spy on the addEventHandler method and call the…
Leonardo Raygoza
  • 469
  • 2
  • 5
  • 16
8
votes
2 answers

Cannot spyOn on a primitive value; undefined given in nestJS

I know this question is asked before but I'm having this error in nestjs when I run service test function here is my service code user.service.ts import { Injectable,HttpException, HttpCode, HttpStatus } from '@nestjs/common'; import {…
mayur.pancholi
  • 446
  • 1
  • 8
  • 18
7
votes
1 answer

Cannot spyOn functions inside of Vue 3 setup()

How can I write a test using Jest that invokes resetTimer and checks that startTimer is also invoked? Code: setup () { const startTimer = () => { // ... }; const resetTimer = () => { startTimer(); }; return { …
Chateau
  • 71
  • 4
7
votes
1 answer

How to mock jest.spyOn for a specific axios call

How do I mock a specific axios call? Imagine 2 GET calls: await axios.get('api/numbers'); await axios.get('api/letters'); Then this is going on in the test: const mockGet = jest.spyOn(axios, 'get'); mockGet.mockReturnValueOnce(Promise.resolve({…
Tudor Morar
  • 3,720
  • 2
  • 27
  • 25
6
votes
2 answers

Vue checking if action calls other action with spyOn

In Vue, I want to check if an action in my store is correctly calling another action using Jest's spyOn, I tried it different ways but it doesn't seem to work, here's my code: // index.js getRecipes ({ dispatch }) { const fruits = ['apple',…
nicojonck
  • 291
  • 4
  • 9
5
votes
1 answer

vitest vi.spyOn does not work on side effects?

I would like to use `vi.spyOn` to monitor the call to an sideEffect function in a module to make sure is being called by another function in a module. I did this on jest without problems but it does not seem to work on vitest. Here is a simplified…
micurs
  • 543
  • 4
  • 8
5
votes
1 answer

Function is never called inside a switchMap with unit test angular + jasmine and SpyOn

i'm trying do a test to know if a function of a service is called, but always it returns me that is not calling. I do not know what I'm doing wrong. The function that i want know if it's called this inside a switchMap, this function is a service.…
5
votes
2 answers

how to get arguments on an event emitter in jasmine test

I have a unit test as below it('billing information is correct', () => { fixture.detectChanges(); spyOn(component.myEventEmitter, 'emit').and.callThrough(); component.form.controls['size'].setValue(12); fixture.detectChanges(); …
Josf
  • 776
  • 1
  • 8
  • 21
5
votes
1 answer

Why spyOn "stops all execution of a function" in Jasmine (asking for clarification on Jasmine 2.2 Documentation on Spies)

In the Jasmine 2.2 documentation I cannot understand the last spec that demonstrates the basic use of Spies. In the beforeEach() section we set bar = null, then we spy on foo.setBar and then we call foo.setBar twice. I can't understand why bar ===…
ilonabudapesti
  • 903
  • 1
  • 6
  • 9
4
votes
0 answers

Jest spyOn toHaveBeenCalled() isn't returning as expected

I'm trying to spyOn exported function that another function in the same file calls. I can't seem to get Jest to use the spy at all. Tried with a mixture of mockImplementation and mockReturnValue. utils.ts: export const func1 = (x: number) => { …
Eli Nathan
  • 1,020
  • 2
  • 13
  • 35
1
2 3 4 5 6 7 8