Questions tagged [spyon]
116 questions
0
votes
1 answer
How to jest.spyOn an instance method called in the constructor
Simplified problem case:
export class MyClass {
constructor() {
this.myMethod();
}
myMethod() {
console.log(42);
}
}
Testing the constructor:
describe('CLASS: MyClass', () => {
let sut: MyClass;
…

Anders Bernard
- 541
- 1
- 6
- 19
0
votes
0 answers
Vitest issue with vi.spyOn() when used with clearInterval
I've created a Countdown timer component and am writing out tests. I want to test that the setInterval gets cleared when the distance is less than 0.
I've set up a test as follows using vi.spyOn(global, 'clearInterval'):
test('should clear the…

Matt Wendzina
- 33
- 5
0
votes
1 answer
How to check if alert has been called?
I am testing my Angular application, I currently I want to check if alert has showed up after clicking on a button. I have the following code:
it('should display alert when clicking on icon', () => {
const button =…

user18974278
- 21
- 5
0
votes
1 answer
How to spyOn class instantiated on constructor in jest
I have the next code:
async function(paramA?: string): Promise {
if (paramA === undefined) {
paramA = this.randomString();
}
this.funnel.loginFunnel(Status.Pending);
await this.tracker.flush();
…

Juanma Perez
- 97
- 4
- 14
0
votes
1 answer
Angular unit testing with Jest Mat paginator test case getting failed :- TypeError: Cannot read property 'page' of undefined
I have a component which uses Mat paginator for pagination purpose, the component is working fine but while running the command npx jest -- layout.component.spec.ts (I am are using jest framework)the below test case is getting failed with below is…

Aakash Thakur
- 19
- 1
- 10
0
votes
1 answer
Jest with React testing library fireEvent.keydown not triggering
I am facing issues in triggering the Enter event on button and testing the method associated with the event. I have two buttons refine search and close button on my component, so trying to implement the same associated click functionality with Enter…

Jayesh Ambali
- 211
- 6
- 24
0
votes
2 answers
Test the call to method into a subscribe on Jamine (Isolated Test)
I try to test a call to method inside on a subscriber, but I can't. Always that I call to method that has a subscribe, console send to me the error "Error: Expected spy saveMug to have been called". Also fail with reContent method.
My test should be…

Ivan Martin Blanco
- 27
- 6
0
votes
1 answer
What is the proper way to spy on a method call using Jest in Angular for a single exported function from an ES6 file?
helpers.ts
export function doThing() {
// some stuff
}
myClass.ts
___
import { doThing } from './helpers.ts'
class myClass {
function someFunction(firstArg, secondArg) {
const thing = someThirdThing;
doThing(firstArg, secondArg,…

Kyle Westendorf
- 11
- 3
0
votes
1 answer
Jest Enzyme Cannot spy the property because it is not a function; undefined given instead when spyOn testing a function in component
I am testing on the execution of functions inside a React component that's connected to redux store.
I was only able to spy on some of the functions, the rest all returned:
Cannot spy the setEmail(or other function names) property because it is not…

DollyBeeBee
- 83
- 3
- 8
0
votes
0 answers
Testing react with Enzyme: Spyon is not covering the code
I very new to react, I am trying to improve the code coverage. I used some of the link to use spyon to test my hooks but test passes but there is no coverage improvement. request your help.
Here us the sample code
import SimplePage from…

Anuj Priyadarshi
- 347
- 4
- 16
0
votes
1 answer
Jasmine spy never getting called
I set up a Jasmine spy to test that when the user clicks a button (triggering the goBack() function), it opens a dialog component, or as far as the test is concerned, calls a thing. It's a really simple test, and I have virtually identical tests…

PaulBunion
- 346
- 2
- 18
0
votes
1 answer
Angular Karma JUnit test - SpyOn does not work within private method
I try to test my component, I know the component works fine but my test gives error since Angular version has been updated to 12.
This is my component:
ngOnInit() {
if (versonA) {
this.doThis();
} else {
this.doThat();
…

dorcsi
- 295
- 2
- 6
- 24
0
votes
1 answer
NestJS, TypeScript, Jest -> TypeError: Cannot read property 'pipe' of undefined
The case is to write a unit test for uploadFile which should upload file to Google Storage bucket using Jest and to mock createReadStream function on the File object.
my-service.ts:
async uploadFile(file: FileUpload): Promise<{
url: string
path:…

grzegorz_l
- 1
- 2
0
votes
1 answer
How to test property values receiving Observable from service
I would like to ask what is the common practice of testing properties which receives data from services inline.
I am not able to test my property which is being receiving values from service - defined inline.
here is my property
readonly…

PhobossFO
- 17
- 3
0
votes
2 answers
Jest mock nested function by using spyOn, but still call the actual function
getInfo calls getColor in the same file. My intention is to mock getColor function, I import the func.js as a module and spyOn getColor. The mocked getColor is supposed to return "Red", but it still calls the actual function and returns…

Luke
- 468
- 1
- 5
- 21