最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - How to spyOn an exported standalone function using javascript jest? - Stack Overflow

programmeradmin4浏览0评论

It is a very simple scenario but I've struggled to find an answer for it.

helpers.ts:

export function foo() {
    bar();
}

export function bar() {
    // do something
}

helpers.spec.ts:

import { foo, bar } from "./helpers";

describe("tests", () => {
    it("example test", () => {
        const barSpy = // how can i set this up?
        foo();
        expect(barSpy).toHaveBeenCalled();
    });
});

I can't do const spy = jest.spyOn(baz, 'bar'); because I don't have a module/class to put in place of "baz". It is just an exported function.

Edit: Jest mock inner function has been suggested as a duplicate but unfortunately it doesn't help with my scenario.

Solutions in that question:

  • Move to separate module: I cannot do this for my scenario. If I am testing every function in my application, this would result in me creating 10s of new files which is not ideal. (To clarify, I think this solution would work but I cannot use it for my scenario. I am already mocking a separate file function successfully in this test file.)

  • Import the module into itself:

helpers.spec.ts:

import * as helpers from "./helpers";

describe("tests", () => {
    it("example test", () => {
        const barSpy = jest.spyOn(helpers, 'bar');
        foo();
        expect(barSpy).toHaveBeenCalled();
    });
});

results in:

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0

It is a very simple scenario but I've struggled to find an answer for it.

helpers.ts:

export function foo() {
    bar();
}

export function bar() {
    // do something
}

helpers.spec.ts:

import { foo, bar } from "./helpers";

describe("tests", () => {
    it("example test", () => {
        const barSpy = // how can i set this up?
        foo();
        expect(barSpy).toHaveBeenCalled();
    });
});

I can't do const spy = jest.spyOn(baz, 'bar'); because I don't have a module/class to put in place of "baz". It is just an exported function.

Edit: Jest mock inner function has been suggested as a duplicate but unfortunately it doesn't help with my scenario.

Solutions in that question:

  • Move to separate module: I cannot do this for my scenario. If I am testing every function in my application, this would result in me creating 10s of new files which is not ideal. (To clarify, I think this solution would work but I cannot use it for my scenario. I am already mocking a separate file function successfully in this test file.)

  • Import the module into itself:

helpers.spec.ts:

import * as helpers from "./helpers";

describe("tests", () => {
    it("example test", () => {
        const barSpy = jest.spyOn(helpers, 'bar');
        foo();
        expect(barSpy).toHaveBeenCalled();
    });
});

results in:

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls:    0
Share Improve this question edited Jan 7, 2021 at 15:58 Kemal Tezer Dilsiz asked Jan 7, 2021 at 2:36 Kemal Tezer DilsizKemal Tezer Dilsiz 4,0195 gold badges26 silver badges46 bronze badges 2
  • Does this answer your question? Jest mock inner function – buzatto Commented Jan 7, 2021 at 4:47
  • Unfortunately that doesn't answer my question. Mocking in that scenario works within the spec.ts file but not in the original file. The original file still calls the actual bar() rather than the mocked bar() – Kemal Tezer Dilsiz Commented Jan 7, 2021 at 16:02
Add a ment  | 

1 Answer 1

Reset to default 9

This is the closed solution:

export function bar() {
  // do something
}

export function foo() {
  exports.bar();   // <-- have to change to exports.bar() instead of bar()
  // or this.bar(); would also work.
}

import * as utils from './utils';

describe('tests', () => {
  it('example test', () => {
    const barSpy = jest.spyOn(utils, 'bar');
    utils.foo();
    expect(barSpy).toHaveBeenCalled();
  });
});

Or take a look this duplicated question

发布评论

评论列表(0)

  1. 暂无评论