如何使用Jest测试多个返回值

huangapple go评论103阅读模式
英文:

How to test multiple return values, using Jest

问题

I'm looking manipulate values from a byte stream. I will receive values in an array, that I'm looking to parse. I'm looking to return a dictionary with the new formatted results. My formatting code is called index.js

  1. //index.js
  2. function calculateNumbers(numbers)
  3. {
  4. results={};
  5. //Summing two values
  6. sumOfTwo = numbers[0] + numbers[1]; //should be 6
  7. results.valTwo = sumOfTwo;
  8. //Summing three values
  9. sumOfThree = numbers[0] + numbers[1] + numbers[3]; //should be 9
  10. results.valThree = sumOfThree;
  11. return{
  12. total:results
  13. };
  14. }
  15. module.exports = calculateNumbers;

For testing I want create a test script to validate my responses (Shown in index.test.js.)

In my function I'm returning {value:total}; When returning my values in this format my test code returns saying:

  1. Expected: 3
  2. Received: {"value": {"valThree": NaN, "valTwo": 3}}
  1. //index.test.js
  2. const calculateNumbers= require("./index");
  3. test("Returns calculations",() => {
  4. //expect(0.2 + 0.1).toBeCloseTo(0.3, 5);
  5. expect(calculateNumbers([1,2,6])).toBe(3,9);
  6. });

How can I modify my unit test to parse the new returned value ?

英文:

I'm looking manipulate values from a byte stream. I will receive values in an array, that I'm looking to parse. I'm looking to return a dictionary with the new formatted results. My formatting code is called index.js

  1. //index.js
  2. function calculateNumbers(numbers)
  3. {
  4. results={};
  5. //Summing two values
  6. sumOfTwo = numbers[0] + numbers[1]; //should be 6
  7. results.valTwo = sumOfTwo;
  8. //Summing three values
  9. sumOfThree = numbers[0] + numbers[1] + numbers[3]; //should be 9
  10. results.valThree = sumOfThree;
  11. return{
  12. total:results
  13. };
  14. }
  15. module.exports = calculateNumbers;

For testing I want create a test script to validate my responses (Shown in index.test.js.)

In my function I'm returning {value:total}; When returning my values in this format my test code returns saying:

  1. Expected: 3
  2. Received: {"value": {"valThree": NaN, "valTwo": 3}}
  1. //index.test.js
  2. const calculateNumbers= require("./index");
  3. test("Returns calculations",()=> {
  4. //expect(0.2 + 0.1).toBeCloseTo(0.3, 5);
  5. expect(calculateNumbers([1,2,6])).toBe(3,9);
  6. });

How can I modify my unit test to parse the new returned value ?

答案1

得分: 0

函数的结果是一个对象。你需要断言所有对象:

  1. expect(calculateNumbers([1,2,6])).toStrictEqual({
  2. total: {
  3. valTwo: 3,
  4. valThree: 9,
  5. },
  6. });
英文:

Result of your function is object. You need to assert all object:

  1. expect(calculateNumbers([1,2,6])).toStrictEqual({
  2. total: {
  3. valTwo: 3,
  4. valThree: 9,
  5. },
  6. });

huangapple
  • 本文由 发表于 2023年3月31日 19:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定