Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

232
Views
Jasmine testing - Error upon spy definition

I have the following classes

export class Init {
   constructor(url: URL) {}
   
   getClient(url: URL): Client {
    return new Client(url);
  }
}

and the client is defined like

export class Client {
   constructor(readonly url: URL) {}

   foo(s: String): Promise<void> {}

   // many other methods
}

and now I am trying to test this like so - with Jasmine

it('foo should be invoked', done => {
   const init = new Init('test-url');
   
   spyOn(init.getClient,'foo');

   ...
}

On the spy definition I get this error

Argument of type 'string' is not assignable to parameter of type 'never'

Why can I resolve this? Init's getClient method returns a Client object. Shouldn't the spy be able to identify this type?

My end result should look like this

it('foo should be invoked', done => {
   const init = new Init('test-url');
   
   spyOn(init.getClient,'foo');

   expect(initCommand.getClient.foo).toHaveBeenCalledTimes(1);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can only spyOn public methods. I would do this:

// mock `getClient` object however you like (some examples below)
// Return the object/value right away
spyOn(init, 'getClient').and.returnValue({ foo: (s: string) => Promise.resolve(s) });
// call a fake function every time init.getClient is called
spyOn(init, 'getClient').and.callFake((url) => return {});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!