如何监视 fakerjs 方法调用
How to spy on a fakerjs method call
Faker.js 允许您使用以下示例轻松创建伪造数据:
import * as faker from 'faker'
console.log(faker.lorem.text())
所以我试图模拟这个库来监视 faker.lorem.text()
:
的使用
import * as faker from 'faker'
const mockFakerLoremText = jest.fn()
jest.mock('faker', () => ({
lorem: {
text: mockFakerLoremText
}
}))
it('should have called lorem.text() method', () => {
faker.lorem.text()
expect(mockFakerLoremText).toHaveBeenCalledTimes(1)
})
但是我得到了以下错误:
ReferenceError: Cannot access 'mockFakerLoremText' before initialization
有人知道我如何监视此方法的调用吗.lorem.text()
?
来自文档 Calling jest.mock() with the module factory parameter
A limitation with the factory parameter is that, since calls to jest.mock()
are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory
这就是您收到错误的原因。
使用 "jest": "^26.6.3"
的工作示例:
index.test.js
:
import * as faker from 'faker';
jest.mock('faker', () => ({
lorem: {
text: jest.fn(),
},
}));
it('should have called lorem.text() method', () => {
faker.lorem.text();
expect(faker.lorem.text).toHaveBeenCalledTimes(1);
});
单元测试结果:
PASS examples/65924623/index.test.ts
√ should have called lorem.text() method (3 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 6.913 s, estimated 7 s
Faker.js 允许您使用以下示例轻松创建伪造数据:
import * as faker from 'faker'
console.log(faker.lorem.text())
所以我试图模拟这个库来监视 faker.lorem.text()
:
import * as faker from 'faker'
const mockFakerLoremText = jest.fn()
jest.mock('faker', () => ({
lorem: {
text: mockFakerLoremText
}
}))
it('should have called lorem.text() method', () => {
faker.lorem.text()
expect(mockFakerLoremText).toHaveBeenCalledTimes(1)
})
但是我得到了以下错误:
ReferenceError: Cannot access 'mockFakerLoremText' before initialization
有人知道我如何监视此方法的调用吗.lorem.text()
?
来自文档 Calling jest.mock() with the module factory parameter
A limitation with the factory parameter is that, since calls to
jest.mock()
are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory
这就是您收到错误的原因。
使用 "jest": "^26.6.3"
的工作示例:
index.test.js
:
import * as faker from 'faker';
jest.mock('faker', () => ({
lorem: {
text: jest.fn(),
},
}));
it('should have called lorem.text() method', () => {
faker.lorem.text();
expect(faker.lorem.text).toHaveBeenCalledTimes(1);
});
单元测试结果:
PASS examples/65924623/index.test.ts
√ should have called lorem.text() method (3 ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 0 | 0 | 0 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 6.913 s, estimated 7 s