- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对返回 promise 的函数进行单元测试。我在验证是否模拟函数被调用。这是我所做的。,
// codetotest.js
const { SomeModule, isSomething, isSomethingElse } = require("some-module");
exports.somefunction = (param1, param2)=> {
const someModule = new SomeModule();
someModule.someMethod("aaa", isSomething);
someModule.someMethod("bbb", isSomethingElse);
return (someModule.someOtherMethod(param1)
.then(()=>{someModule.run(param2)}));
}
这是测试文件,测试说模拟函数没有被调用,但我确实看到模拟函数中的控制台语句正在显示。
// codetotest.test.js
const { somefunction} = require("./codetotest.js");
const { SomeModule } = require("some-module");
jest.mock("some-module", () => {
return {
SomeModule: jest.fn().mockImplementation(() => {
return {
someMethod: jest.fn((param, fn) => { console.log("This prints!"); }),
someOtherMethod: jest.fn((param) => { return Promise.resolve(() => { }) }),
run: jest.fn((param) => { return Promise.resolve(() => { return []; }) })
}
})
};
});
afterEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
});
describe("Test codetotest.js", () => {
it("somefunction() - success", async () => {
const someModule = new SomeModule();
let output = await somefunction("param1", "param2");
expect(SomeModule).toHaveBeenCalled();
expect(someModule.someMethod).toHaveBeenCalled(); // This fails
await expect(someModule.someOtherMethod.mock.results[0]).resolves;
expect(someModule.someOtherMethod).toHaveBeenCalled(); // This fails
await expect(someModule.run.mocks.results[0]).resolves;
expect(someModule.run).toHaveBeenCalled(); // This fails
});
});
感谢任何帮助/指点。
谢谢。
P.S:在nodeJs开发和单元测试方面,我还是个初学者。
最佳答案
我在这上面花了很多时间,最后发现实例化的模拟类没有正确返回模拟方法。 This answer 提示我哪里出错了。
因此,我不得不按如下方式更改我的测试文件。
// codetotest.test.js
const { somefunction} = require("./codetotest.js");
const { SomeModule } = require("some-module");
jest.mock("some-module", function() {
return {
SomeModule: jest.fn().mockImplementation(function() { // Arrow function cannot be used as constructor
// Because I was not using the 'this' operator, my constructor always returned empty
this.someMethod = jest.fn((param, fn) => { console.log("This prints!"); });
this.someOtherMethod = jest.fn((param) => { return Promise.resolve(() => { }) });
this.run = jest.fn((param) => { return Promise.resolve(() => { return []; }) });
return {
someMethod: this,someMethod,
someOtherMethod: this.someOtherMethod,
run: this.run
}
})
};
});
afterEach(() => {
jest.restoreAllMocks();
});
describe("Test codetotest.js", () => {
it("somefunction() - success", async () => {
await somefunction("param1", "param2");
expect(SomeModule).toHaveBeenCalled();
expect(SomeModule.mock.instances[0].someMethod).toHaveBeenCalled(); // This works
expect(SomeModule.mock.instances[0].someOtherMethod).toHaveBeenCalled(); // This works
expect(someModule.mock.instances[0].run).toHaveBeenCalled(); // This works
});
});
关于node.js - expect(jest.fn()).toHaveBeenCalled() 失败,即使该函数已被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67538405/
我有一个函数,它只是创建一个写入流并写入它...我想测试该函数,并测试 createWriteStream 的 write() 函数> 被调用。但是,我正在 mock fs 模块,因为我不想在测试时实
据我所知, Jasmine 的 .toHaveBeenCalled() Matcher 返回一个 Promise,该 Promise 在函数被调用时被解析。实际上对我来说,它返回未定义: it('sh
什么时候应该调用这些方法中的每一个,为什么? const spyStart = spyOn(el, 'func1'); expect(spyStart).toHaveBeenCalled(); con
我正在使用 Composition API 和 setup() Hook 运行 vue3 应用程序。 我使用 Vitest 作为单元测试框架。 (v 0.6.1) 我有以下示例组件: // src/c
我在进行这个简单的测试时遇到了很多麻烦。 我在想要测试的 Controller 中有一个 $scope.$on 监听器。我只是想确保它在广播事件之后被调用。 为此,我认为以下代码可以工作: descr
我正在使用 Composition API 和 setup() Hook 运行 vue3 应用程序。 我使用 Vitest 作为单元测试框架。 (v 0.6.1) 我有以下示例组件: // src/c
谁能告诉我为什么以下测试失败。 var Person = function() {}; Person.prototype.helloSomeone = function(toGreet) { re
我正在使用 Enzyme/Jest 为容器帽子上的函数编写测试,该函数是通过复选框组件的 onChange 触发的。我正在尝试模拟“更改”,但是,“更改”以某种方式没有触发调用 onChange 函数
这是测试代码 var list = new List([1, 2, 3, 4]); var list2 = new List([5, 1]); beforeAll(function () { sp
我在 React 中有一个小组件,它在渲染时生成两个随机数,然后要求用户提交这些数字的总和,如果它们正确,则增加他们的分数。 以下代码处理此游戏并在浏览器中按预期运行: import React, {
我是 jasmine 的新手,这是我的 src 文件,我在其中创建了 Auth 类 function Auth() { } Auth.prototype.isEmpty = function(str)
使用 Jest 和 React,我如何监视一个 this.setState()并测试功能 hasBeenCalled() it('shall update targets when engine re
我正在尝试测试是否使用 enzyme 和 jest 调用了 React 组件方法。该函数应该在 时被调用。元素变得没有焦点(模糊)。该组件连接到 Redux 存储,但它也按名称导出,以将 Redux
被测代码: module lib { export class Topic { private _callbacks: JQueryCallback; publ
我目前在监视 typescript 类中调用的继承方法时遇到问题,其中 toHaveBeenCalled() 方法返回 false,即使被监视的方法已被调用。看看下面的场景... 我有两个类,用 Ty
我正在尝试对返回 promise 的函数进行单元测试。我在验证是否模拟函数被调用。这是我所做的。, // codetotest.js const { SomeModule, isSomething,
我的监视方法的示例失败,并显示“预期 spy handle_click已被调用”。当它应该过去的时候。但是,我收到控制台日志“Foo handle_click called!”,所以我知道它正在被调用
一个简单的组件: {{ count }} Increment export default { data () { r
我正在尝试 stub /监视翻译,而不仅仅是 mock 它,即使在这种最基本的情况下,我似乎也无法触发它。 /** * ComponentName.jsx */ import { useTrans
我是一名优秀的程序员,十分优秀!