NestJS Jest 监视一个回调函数
NestJS Jest spy a callback function
所以我有一个 class 有一个功能。在这个函数中,我从库的 class 对象执行另一个函数,该对象具有回调函数。在那个回调函数中,有一个我需要窥探的方法。有什么办法吗?这是粗略的细节:
export class Consume {
private readonly consumer: Consumer; // class from the library
private readonly randomService: RandomService; // class that it's method I want to spy on
constructor() {
this.consumer = new Consumer();
this.randomService = new RandomService();
}
consume(): void {
this.consumer.consume(async data => {
...
this.randomService.doSomething('withParam'); // I need to spy here on this method
...
})
}
}
我试过模拟,但似乎没有用,这是我试过的方法:
it('should do something', () => {
const consumerSpy = jest.spyOn(consumer, 'consume');
consumerSpy.mockImplementation(cb => {
cb(['data1', 'data2'])
}); // mocking the library's function
const randomServiceSpy = jest.spyOn(randomService, 'doSomething')
expect(randomServiceSpy).toBeCalledTimes(1); // Number of times called: 0 returned
});
还有这个:
it('should do something', () => {
jest.mock('path/to/consume-class', () => {
consume: jest.fn()
}); // mocking Consume's consume function
consume.consume(); // calling consume function from Consume class above
const randomServiceSpy = jest.spyOn(randomService, 'doSomething')
expect(randomServiceSpy).toBeCalledTimes(1); // Number of times called: 0 returned
});
如有任何帮助,我们将不胜感激! :)
您可以通过 jest.spyOn()
在 Class.prototype.method
上使用 install spy。
例如
consume.ts
:
import { Consumer } from './lib';
import { RandomService } from './random.service';
export class Consume {
private readonly consumer: Consumer;
private readonly randomService: RandomService;
constructor() {
this.consumer = new Consumer();
this.randomService = new RandomService();
}
consume(): void {
this.consumer.consume(async (data) => {
this.randomService.doSomething('withParam');
});
}
}
lib.ts
:
export class Consumer {
consume(fn) {
fn();
}
}
random.service.ts
:
export class RandomService {
doSomething(param) {}
}
consume.test.ts
:
import { Consume } from './consume';
import { Consumer } from './lib';
import { RandomService } from './random.service';
describe('71246273', () => {
afterEach(() => {
jest.restoreAllMocks();
});
test('should pass', () => {
jest.spyOn(Consumer.prototype, 'consume').mockImplementation((fn) => fn());
const doSomethingSpy = jest.spyOn(RandomService.prototype, 'doSomething');
const consume = new Consume();
consume.consume();
expect(doSomethingSpy).toBeCalledWith('withParam');
});
});
测试结果:
PASS Whosebug/71246273/consume.test.ts (8.765 s)
71246273
✓ should pass (3 ms)
-------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files | 90.91 | 100 | 83.33 | 90 |
consume.ts | 100 | 100 | 100 | 100 |
lib.ts | 50 | 100 | 0 | 50 | 3
random.service.ts | 100 | 100 | 100 | 100 |
-------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 8.879 s
所以我有一个 class 有一个功能。在这个函数中,我从库的 class 对象执行另一个函数,该对象具有回调函数。在那个回调函数中,有一个我需要窥探的方法。有什么办法吗?这是粗略的细节:
export class Consume {
private readonly consumer: Consumer; // class from the library
private readonly randomService: RandomService; // class that it's method I want to spy on
constructor() {
this.consumer = new Consumer();
this.randomService = new RandomService();
}
consume(): void {
this.consumer.consume(async data => {
...
this.randomService.doSomething('withParam'); // I need to spy here on this method
...
})
}
}
我试过模拟,但似乎没有用,这是我试过的方法:
it('should do something', () => {
const consumerSpy = jest.spyOn(consumer, 'consume');
consumerSpy.mockImplementation(cb => {
cb(['data1', 'data2'])
}); // mocking the library's function
const randomServiceSpy = jest.spyOn(randomService, 'doSomething')
expect(randomServiceSpy).toBeCalledTimes(1); // Number of times called: 0 returned
});
还有这个:
it('should do something', () => {
jest.mock('path/to/consume-class', () => {
consume: jest.fn()
}); // mocking Consume's consume function
consume.consume(); // calling consume function from Consume class above
const randomServiceSpy = jest.spyOn(randomService, 'doSomething')
expect(randomServiceSpy).toBeCalledTimes(1); // Number of times called: 0 returned
});
如有任何帮助,我们将不胜感激! :)
您可以通过 jest.spyOn()
在 Class.prototype.method
上使用 install spy。
例如
consume.ts
:
import { Consumer } from './lib';
import { RandomService } from './random.service';
export class Consume {
private readonly consumer: Consumer;
private readonly randomService: RandomService;
constructor() {
this.consumer = new Consumer();
this.randomService = new RandomService();
}
consume(): void {
this.consumer.consume(async (data) => {
this.randomService.doSomething('withParam');
});
}
}
lib.ts
:
export class Consumer {
consume(fn) {
fn();
}
}
random.service.ts
:
export class RandomService {
doSomething(param) {}
}
consume.test.ts
:
import { Consume } from './consume';
import { Consumer } from './lib';
import { RandomService } from './random.service';
describe('71246273', () => {
afterEach(() => {
jest.restoreAllMocks();
});
test('should pass', () => {
jest.spyOn(Consumer.prototype, 'consume').mockImplementation((fn) => fn());
const doSomethingSpy = jest.spyOn(RandomService.prototype, 'doSomething');
const consume = new Consume();
consume.consume();
expect(doSomethingSpy).toBeCalledWith('withParam');
});
});
测试结果:
PASS Whosebug/71246273/consume.test.ts (8.765 s)
71246273
✓ should pass (3 ms)
-------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files | 90.91 | 100 | 83.33 | 90 |
consume.ts | 100 | 100 | 100 | 100 |
lib.ts | 50 | 100 | 0 | 50 | 3
random.service.ts | 100 | 100 | 100 | 100 |
-------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 8.879 s