javascript - 如何用 Jest stub 类的所有方法?

标签 javascript jestjs sinon

我正在尝试重新构建自己以使用 Jest,但我发现从 sinon 迁移到它有点困难。

使用 sinonjs,如果我有一个类,我可以执行 sinon.createStubInstance 并且 sinon 会给我该类的一个 stub 实例,这是一个所有方法都是 stub 的对象实例。

用jest来说,在网上搜索了很多次之后,我似乎要么需要使用__mocks__文件夹抽象并重新输入整个类,要么单独 stub 方法。

还有更好的办法吗? Jest 有没有类似于 sinon 的 createStubInstance 的方法?

最佳答案

您可以使用jest.doMock()在单个单元测试用例中使用 jest stub 类的所有方法。

例如

index.ts:

export class SomeClass {
  find() {
    console.log('find');
  }
  findById(id) {
    console.log('findById');
  }
}

.find().findById的真正实现是调用console.log。 现在,我们使用 jest.doMock 来 stub 它们。

index.spec.ts:

describe('57649917', () => {
  it('should mock all methods of SomeClass', () => {
    jest.doMock('./');
    const { SomeClass } = require('./');
    const logSpy = jest.spyOn(console, 'log');
    const mInstance = new SomeClass();
    expect(jest.isMockFunction(mInstance.find)).toBeTruthy();
    expect(jest.isMockFunction(mInstance.findById)).toBeTruthy();
    mInstance.find();
    mInstance.findById(1);
    expect(mInstance.find).toBeCalledTimes(1);
    expect(mInstance.findById).toBeCalledTimes(1);
    expect(logSpy).not.toBeCalled();
  });

  it('should call the real methods of SomeClass', () => {
    jest.unmock('./');
    const logSpy = jest.spyOn(console, 'log');
    const { SomeClass } = require('./');
    const instance = new SomeClass();
    instance.find();
    instance.findById(1);
    expect(logSpy.mock.calls[0]).toEqual(['find']);
    expect(logSpy.mock.calls[1]).toEqual(['findById']);
  });
});

单元测试结果:

 PASS  src/stackoverflow/57649917/index.spec.ts (8s)
  57649917
    ✓ should mock all methods of SomeClass (9ms)
    ✓ should call the real methods of SomeClass (9ms)

  console.log node_modules/jest-mock/build/index.js:860
    find

  console.log node_modules/jest-mock/build/index.js:860
    findById

Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        9.182s

源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/57649917

对于sinon.js,我们可以使用sinon.createStubInstance来创建一个带有类的所有 stub 方法的 stub 实例。

index.ts:

export class SomeClass {
  find() {
    console.log('find');
  }
  findById(id) {
    console.log('findById');
  }
}

index.spec.ts:

import { SomeClass } from './';
import sinon from 'sinon';
import { expect } from 'chai';

describe('57649917', () => {
  it('should stub all methods of SomeClass', () => {
    const stubInstance = sinon.createStubInstance(SomeClass);
    stubInstance.find();
    stubInstance.findById(1);
    expect(stubInstance.find.calledOnce).to.be.true;
    expect(stubInstance.findById.calledWith(1)).to.be.true;
  });
});

单元测试结果:

 57649917
    ✓ should stub all methods of SomeClass


  1 passing (13ms)

源代码:https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/57649917

关于javascript - 如何用 Jest stub 类的所有方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57649917/

相关文章:

javascript - webpack + 传单插件

node.js - jest localStorage 实现从何而来?

unit-testing - vue-test-utils:如何在 mounted() 生命周期 Hook (使用 vuex)中测试逻辑?

javascript - 使用 .env 文件对 jest 进行单元测试

typescript - 在 TypeScript 中进行单元测试时, stub 依赖项的正确方法是什么?

unit-testing - 使用Sinon、Mocha、Enzyme 和 React 模拟窗口

node.js - 断言错误: expeced to be called once but was called 0 times

javascript - 将 err 传递到express.js Router、Node.js

javascript - 如果窗口高度 < 更改 css

javascript - 如何获取表中的输入值?