Questions tagged [spyon]

116 questions
4
votes
1 answer

Cannot spyOn on a primitive value; undefined given

I am trying to write the unit test case using jest and nest but getting the below error: In the test case, I am trying to call create credentials method using spyon function but the spyon is itself giving me an error. DeviceSecretService › should…
sweta singh
  • 41
  • 1
  • 1
  • 3
4
votes
2 answers

How jasmine spyOn a generic method

I try to make a spy on a generic method in typescript, but cannot get Jasmine to recognise it. I have code http: HttpClient <- Not proper code, just showing type. ... this.http.get(url); where I want to mock the get<...> method. const…
LosManos
  • 7,195
  • 6
  • 56
  • 107
4
votes
1 answer

How to use jest.spyOn function to test a function from another file

I am trying to run test case for testing entry point of my web service jest I am facing one issue while running the unit test. Cannot spy the async function, because it is a property not a function. I am trying to test if the run function is called…
a_uttav
  • 41
  • 5
4
votes
6 answers

How to unit test / mock a $timeout call?

How do I mock the timeout call, here? $scope.submitRequest = function () { var formData = getData(); $scope.form = JSON.parse(formData); $timeout(function () { $('#submitForm').click(); }, 2000); }; I want to…
GC_
  • 448
  • 4
  • 23
3
votes
2 answers

How to test MatDialog is opened using mock in angular unit test

I am currently using a component, in which on button click a pop-up will be opened to greet user. I am using angular Material Dialog for opening pop-up and the component code block which will call on click is public openMatDialog(): void…
Prem Kumar
  • 39
  • 1
  • 6
3
votes
1 answer

How can I mock multiple different database calls in a unit test with jest?

I am writing a jest unit test for a function that makes multiple different database calls. I am able to mock the first db call just fine but I am having trouble mocking the others. My function: const sample = async () => { const…
John Grayson
  • 1,378
  • 3
  • 17
  • 30
3
votes
0 answers

Jest spy on React useRef hook not being used after updating Jest to 24.9

When I used jest 22.4 I had a test like the following it ('should blah', () => { const spy = jest.spyOn(React, 'useRef').mockReturnValueOnce(blahMock); const comp = shallowWithIntl(); const compShallow = comp.first().shallow(); …
Kenzie Revoyr
  • 261
  • 3
  • 7
3
votes
0 answers

Jasmine spyOn fails after moving to Webpack4

Recently I'm migrating a project from webpack 3 -> 4. There's an issue wit karma/jasmine tests. In my test I want to import a TypeScript namespace object and mock it. spyOn(AlertDialogModule, 'createAlertDialog') .and.returnValue( …
c4off
  • 185
  • 1
  • 10
3
votes
1 answer

Jasmine : How to SpyOn a method callback : method.then()

within my Angular 4.0.0 app , i have this method , called in my component. This method is called within a service : this.myService.myMethod(param).then(any => { console.log("success case") }) .catch(error => { …
firasKoubaa
  • 6,439
  • 25
  • 79
  • 148
3
votes
1 answer

Angularjs: mock location.path() with spyOn for a unit test

I have already read this post (and others) but I don't manage to make this simple unit test work. I'm using the version 2 of Jasmine. My factory is very simple: angular.module('myApp') .factory('detectPath', function ($location, $rootScope) { …
Simon
  • 6,025
  • 7
  • 46
  • 98
3
votes
1 answer

How to spyOn Angular provider factory method?

In Jasmine, you can spyOn(object, 'function'). I am trying to spyOn a provider, which is used as "provider()". How to spyOn it? The provider looks like this: providers.provider('telecom', function() { this.$get = function() { return…
Kyle Xie
  • 722
  • 1
  • 8
  • 28
3
votes
1 answer

Jasmine test using spyon with $httpBackend not working

I am trying to write a jasmine test on some javascript using spyon over a method that uses $http. I have mocked this out using $httpBackend and unfortunately the spy doesn't seem to be picking up the fact the method has indeed been called post $http…
sarin
  • 5,227
  • 3
  • 34
  • 63
2
votes
2 answers

Mocking Nested function calls in Jasmine

I am currently trying to test my typescript functions using Jasmine: //AB.ts export async function A() { } export async function B() { A(); } I am trying to unit test the function B by mocking out A. I want to see if A is called. Here is what…
Sean
  • 592
  • 4
  • 9
  • 19
2
votes
2 answers

Jest unit test to spy on lower-level method (NodeJS)

Trying to spy and override a function two levels down using Jest. The test results say, "Expected mock function to have been called, but it was not called." // mail/index.unit.test.js import mail from './index'; import * as sib from…
nstuyvesant
  • 1,392
  • 2
  • 18
  • 43
2
votes
0 answers

Ability to set up multiple spies on the same method

We are trying to do unit testing with Jasmine to isolate a method or component that we need to test and see how it behaves under a variety of circumstances. return Promise.resolve(referenceObject()) //afraid this might execute out of order so…
Balachander
  • 430
  • 1
  • 3
  • 15