admin管理员组

文章数量:1025244

I have a jest test like so

    import * as source from '../source
      ("Spy on a method", ()=>{
        const spy = jest.spyOn(source, 'toBeCalledOn')
        const result = source.entrypoint(0);
        expect(source.toBeCalledOn()).toHaveBeenCalled();
        })

In my srcjs

export const entrypoint = (input)=>{
    toBeCalledOn(input)
}


const toBeCalledOn =(input)=>{
    console.log();
}

I expect 'toBeCalledOn' to pass my jest test but I always get failed -

Expected toBeCalledOn toHaveBeenCalled but wasn't

How can I use jestspy to see if a method was called under certain conditions, like an if statement for example?

Correct me if I'm wrong but what the point to call the method only to check if it had been called, that seems rather obvious to me.

I have a jest test like so

    import * as source from '../source
      ("Spy on a method", ()=>{
        const spy = jest.spyOn(source, 'toBeCalledOn')
        const result = source.entrypoint(0);
        expect(source.toBeCalledOn()).toHaveBeenCalled();
        })

In my srcjs

export const entrypoint = (input)=>{
    toBeCalledOn(input)
}


const toBeCalledOn =(input)=>{
    console.log();
}

I expect 'toBeCalledOn' to pass my jest test but I always get failed -

Expected toBeCalledOn toHaveBeenCalled but wasn't

How can I use jestspy to see if a method was called under certain conditions, like an if statement for example?

Correct me if I'm wrong but what the point to call the method only to check if it had been called, that seems rather obvious to me.

Share Improve this question edited Feb 18, 2019 at 22:16 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Feb 18, 2019 at 15:17 Kai CriticallyAcclaimed CooperKai CriticallyAcclaimed Cooper 9782 gold badges15 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You have to expect the assertion on the spyied function not the actual function.

When you spy on the function, the actual function is never called.

import * as source from '../source'; #import source object or all functions inside of it

describe('Spy on a method', () => {
  it('toBeCalledOn to have been called', () => {
    const spy = jest.spyOn(source, 'toBeCalledOn')
    const result = source.entrypoint(0);
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy).toHaveBeenCalledWith(0);
  })
});

If you want to test a condition, you also have to cover the branches occurred due to that functionality.

If you have a if and else statement, then you will have to write 2 tests. Please see the example below.

export const entrypoint = (input)=>{
  if(input !== 0){
    toBeCalledOn(input);
  }
}

const toBeCalledOn =(input)=>{
    console.log('logs something');
}

import * as source from '../source

describe('Spy on a method', () => {
  it('toBeCalledOn not to be called', () => {
    const spy = jest.spyOn(source, 'toBeCalledOn')
    const result = source.entrypoint(0);
    expect(spy).not.toHaveBeenCalled();
  });
  
  it('toBeCalledOn to have been called', () => {
    const spy = jest.spyOn(source, 'toBeCalledOn')
    const result = source.entrypoint(1);
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy).toHaveBeenCalledWith(1);
  });
});

Answering about your other query, I would say that, we have to assert things that would be obvious. In case if someone makes any change in the code, and if it breaks the previous flow, in this case you can catch the error in the test case.

That's why we write test cases.

Also, try to add strict assertions, so as to do robust testing. E.g. Instead of toHaveBeenCalled you could use toHaveBeenCalledTimes function to make the assertion more strict.

I have a jest test like so

    import * as source from '../source
      ("Spy on a method", ()=>{
        const spy = jest.spyOn(source, 'toBeCalledOn')
        const result = source.entrypoint(0);
        expect(source.toBeCalledOn()).toHaveBeenCalled();
        })

In my srcjs

export const entrypoint = (input)=>{
    toBeCalledOn(input)
}


const toBeCalledOn =(input)=>{
    console.log();
}

I expect 'toBeCalledOn' to pass my jest test but I always get failed -

Expected toBeCalledOn toHaveBeenCalled but wasn't

How can I use jestspy to see if a method was called under certain conditions, like an if statement for example?

Correct me if I'm wrong but what the point to call the method only to check if it had been called, that seems rather obvious to me.

I have a jest test like so

    import * as source from '../source
      ("Spy on a method", ()=>{
        const spy = jest.spyOn(source, 'toBeCalledOn')
        const result = source.entrypoint(0);
        expect(source.toBeCalledOn()).toHaveBeenCalled();
        })

In my srcjs

export const entrypoint = (input)=>{
    toBeCalledOn(input)
}


const toBeCalledOn =(input)=>{
    console.log();
}

I expect 'toBeCalledOn' to pass my jest test but I always get failed -

Expected toBeCalledOn toHaveBeenCalled but wasn't

How can I use jestspy to see if a method was called under certain conditions, like an if statement for example?

Correct me if I'm wrong but what the point to call the method only to check if it had been called, that seems rather obvious to me.

Share Improve this question edited Feb 18, 2019 at 22:16 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Feb 18, 2019 at 15:17 Kai CriticallyAcclaimed CooperKai CriticallyAcclaimed Cooper 9782 gold badges15 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

You have to expect the assertion on the spyied function not the actual function.

When you spy on the function, the actual function is never called.

import * as source from '../source'; #import source object or all functions inside of it

describe('Spy on a method', () => {
  it('toBeCalledOn to have been called', () => {
    const spy = jest.spyOn(source, 'toBeCalledOn')
    const result = source.entrypoint(0);
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy).toHaveBeenCalledWith(0);
  })
});

If you want to test a condition, you also have to cover the branches occurred due to that functionality.

If you have a if and else statement, then you will have to write 2 tests. Please see the example below.

export const entrypoint = (input)=>{
  if(input !== 0){
    toBeCalledOn(input);
  }
}

const toBeCalledOn =(input)=>{
    console.log('logs something');
}

import * as source from '../source

describe('Spy on a method', () => {
  it('toBeCalledOn not to be called', () => {
    const spy = jest.spyOn(source, 'toBeCalledOn')
    const result = source.entrypoint(0);
    expect(spy).not.toHaveBeenCalled();
  });
  
  it('toBeCalledOn to have been called', () => {
    const spy = jest.spyOn(source, 'toBeCalledOn')
    const result = source.entrypoint(1);
    expect(spy).toHaveBeenCalledTimes(1);
    expect(spy).toHaveBeenCalledWith(1);
  });
});

Answering about your other query, I would say that, we have to assert things that would be obvious. In case if someone makes any change in the code, and if it breaks the previous flow, in this case you can catch the error in the test case.

That's why we write test cases.

Also, try to add strict assertions, so as to do robust testing. E.g. Instead of toHaveBeenCalled you could use toHaveBeenCalledTimes function to make the assertion more strict.

本文标签: javascriptJest spyOn methodStack Overflow