如何使用Jest和JavaScript测试副作用的功能

发布于 2025-02-12 16:11:20 字数 859 浏览 0 评论 0原文

我正在用玩笑进行测试。
我想测试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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑,眼淚并存 2025-02-19 16:11:21

严格模式 href =“ https://developer.mozilla.org/en-us/docs/web/javascript/Reference/global_objects/objects/object/object/freeze/freeze” rel =“ nofollow noreferrer”任何直接突变都会引发一个例外:

请注意,下面代码中数组的元素是原始数字,但是防止嵌套对象的突变(包括数组)将要求这些对象也被冻结。

<script type="module">

function test (name, fn) {
  try {
    fn();
    console.log('✅', name);
  }
  catch (ex) {
    console.log('❌', name);
    console.log(String(ex));
  }
}

function doubleArrayValues (array) {
  for (let i = 0; i < array.length; i += 1) {
    array[i] *= 2;
  }
}

function myFunctionPure (array) {
  const copy = [...array];
  doubleArrayValues(copy);
  return copy;
}

function myFunctionImpure (array) {
  doubleArrayValues(array);
  return array;
}

test('myFunctionPure: has no side-effects', () => {
  const myArray = Object.freeze([1, 2, 3]);
  myFunctionPure(myArray);
});

test('myFunctionImpure: has no side-effects', () => {
  const myArray = Object.freeze([1, 2, 3]);
  myFunctionImpure(myArray);
});

</script>

In strict mode, you can freeze the array before testing the function, and any direct mutation will throw an exception:

Note that the elements of the array in the code below are primitives, but that preventing mutations of nested objects (including arrays), will require that those objects are also frozen.

<script type="module">

function test (name, fn) {
  try {
    fn();
    console.log('✅', name);
  }
  catch (ex) {
    console.log('❌', name);
    console.log(String(ex));
  }
}

function doubleArrayValues (array) {
  for (let i = 0; i < array.length; i += 1) {
    array[i] *= 2;
  }
}

function myFunctionPure (array) {
  const copy = [...array];
  doubleArrayValues(copy);
  return copy;
}

function myFunctionImpure (array) {
  doubleArrayValues(array);
  return array;
}

test('myFunctionPure: has no side-effects', () => {
  const myArray = Object.freeze([1, 2, 3]);
  myFunctionPure(myArray);
});

test('myFunctionImpure: has no side-effects', () => {
  const myArray = Object.freeze([1, 2, 3]);
  myFunctionImpure(myArray);
});

</script>

此岸叶落 2025-02-19 16:11:21

我喜欢@jsejcksn的透彻答案。
这是一个快速的答案:

import myFunction from "myFunction.js";

test("that there are no side-effects", () => {
  const myArray = [1,2,3];
  const myArrayCopy = [...myArray];
  const result = myFunction( myArray );
  
  //If the function returns an array,
  //it should not be the same reference
  expect( result ).not.toBe( myArray );
  
  //The original array values should remain the same
  expect( myArray ).toEqual( myArrayCopy );
});

I like the thorough answer from @jsejcksn.
Here's a quick answer:

import myFunction from "myFunction.js";

test("that there are no side-effects", () => {
  const myArray = [1,2,3];
  const myArrayCopy = [...myArray];
  const result = myFunction( myArray );
  
  //If the function returns an array,
  //it should not be the same reference
  expect( result ).not.toBe( myArray );
  
  //The original array values should remain the same
  expect( myArray ).toEqual( myArrayCopy );
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文