如何使用Jest和JavaScript测试副作用的功能
我正在用玩笑进行测试。
我想测试myFunction(myArray)
没有副作用:
test("that there are no side-effects" ...)
如何编写开玩笑的测试myArray
不会通过myFunction ?
编辑:
我期望以嘲笑测试脚本的形式得到答案,我想对任何功能和任何变量使用该副作用测试算法,而不仅仅是myFunction(myarray)
。
文件:myFunction.js
export default myFunction;
function myFunction( array ) {
//any code goes here
//maybe it changes array
//maybe it doesn't
//maybe it returns a value
//maybe it doesn't
}
文件:myFunction.test.js
import myFunction from "myFunction.js";
test("that there are no side-effects",
//the Jest test code goes here
//where I pass myArray into myFunction
myFunction( myArray )
//myArray should not be changed by myFunction
//what should the test code be?
);
I'm using Jest for testing.
I want to test if myFunction( myArray )
has no side-effects:
test("that there are no side-effects" ...)
How do I write a Jest test that myArray
doesn't get changed by myFunction
?
EDIT:
I'm expecting an answer in the form of a Jest test script, and I want to use that side-effect test algorithm for any function and any variable, not just myFunction( myArray )
.
FILE: myFunction.js
export default myFunction;
function myFunction( array ) {
//any code goes here
//maybe it changes array
//maybe it doesn't
//maybe it returns a value
//maybe it doesn't
}
FILE: myFunction.test.js
import myFunction from "myFunction.js";
test("that there are no side-effects",
//the Jest test code goes here
//where I pass myArray into myFunction
myFunction( myArray )
//myArray should not be changed by myFunction
//what should the test code be?
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在严格模式 href =“ https://developer.mozilla.org/en-us/docs/web/javascript/Reference/global_objects/objects/object/object/freeze/freeze” rel =“ nofollow noreferrer”任何直接突变都会引发一个例外:
In strict mode, you can freeze the array before testing the function, and any direct mutation will throw an exception:
我喜欢@jsejcksn的透彻答案。
这是一个快速的答案:
I like the thorough answer from @jsejcksn.
Here's a quick answer: