Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单元测试新手-您将如何使用Jest测试来测试此代码?_Javascript_Unit Testing_Mocking_Jestjs - Fatal编程技术网

Javascript 单元测试新手-您将如何使用Jest测试来测试此代码?

Javascript 单元测试新手-您将如何使用Jest测试来测试此代码?,javascript,unit-testing,mocking,jestjs,Javascript,Unit Testing,Mocking,Jestjs,我是单元测试的新手。 我想学习如何正确地使用Jest进行测试,在文章、演示和关于Udemy的课程之后,我仍然很困惑(尤其是使用Jest mocking)。 我应该提到的是,我们所有的代码都在react之外,所以它似乎让我更加困惑 我已经为我的公司写了一个AB测试,并且已经意识到它应该很容易测试,但我认为我做得不对。看起来我只是在复制我的原始代码来进行测试,虽然我觉得这个函数是经过验证的,但对我来说似乎是错误的 这是我的问题代码和测试。你们这些经验丰富的人怎么能做到“正确” 谢谢 JS: 让ABT

我是单元测试的新手。 我想学习如何正确地使用Jest进行测试,在文章、演示和关于Udemy的课程之后,我仍然很困惑(尤其是使用Jest mocking)。 我应该提到的是,我们所有的代码都在react之外,所以它似乎让我更加困惑

我已经为我的公司写了一个AB测试,并且已经意识到它应该很容易测试,但我认为我做得不对。看起来我只是在复制我的原始代码来进行测试,虽然我觉得这个函数是经过验证的,但对我来说似乎是错误的

这是我的问题代码和测试。你们这些经验丰富的人怎么能做到“正确”

谢谢

JS:

让ABTestValue=30;
ABTest();
导出函数ABTest(){
const roll=Math.floor(Math.random()*100);
如果(调试模式){
log('TimedRefresh-AB Test Issue-831(90及以上版本的刷新时间发生更改)随机化值=',roll);
}
常量ABTestMappings=[
{
滚动:90,
ABTestValue:40
},
{
登记:91,
ABTestValue:40
},
{
滚动:92,
ABTestValue:50
},
{
登记:93,
ABTestValue:50
},
{
滚动:94,
ABTestValue:60
},
{
登记:95,
ABTestValue:60
},
{
登记:96,
ABTestValue:70
},
{
滚动:97,
ABTestValue:70
},
{
滚动:98,
ABTestValue:80
},
{
滚动:99,
ABTestValue:80
}
];
如果(滚动>89和滚动<100){
ABTestValue=ABTestMappings.filter((a)=>a.roll==roll)[0]。ABTestValue;
}
window.rg_version=`time${ABTestValue}`;
如果(调试模式){
log('TimedRefresh-ABTestValue-刷新时间的变化(默认为30秒)-',ABTestValue);
}
}
test.js:

import { ABTest } from './index';

describe('ABTEST test', () => {
  it('ABTest function', () => {
    expect(ABTest).toBeDefined();
  })

  describe('test different rolls', () => {
    const ABTestMappings = [
      {
        roll: 90,
        ABTestValue: 40
      },
      {
        roll: 91,
        ABTestValue: 40
      },
      {
        roll: 92,
        ABTestValue: 50
      },
      {
        roll: 93,
        ABTestValue: 50
      },
      {
        roll: 94,
        ABTestValue: 60
      },
      {
        roll: 95,
        ABTestValue: 60
      },
      {
        roll: 96,
        ABTestValue: 70
      },
      {
        roll: 97,
        ABTestValue: 70
      },
      {
        roll: 98,
        ABTestValue: 80
      },
      {
        roll: 99,
        ABTestValue: 80
      }
    ];

    const roll = 92;
    const roll2 = 95;
    const roll3 = 99;
    const roll4 = 80;
    let ABTestValue = 30;
    let ABTestValue2 = 30;
    let ABTestValue3 = 30;
    let ABTestValue4 = 30;

    if (roll > 89 && roll < 100) {
      ABTestValue = ABTestMappings.filter((a) => a.roll === roll)[0].ABTestValue;
    }

    if (roll2 > 89 && roll2 < 100) {
      ABTestValue2 = ABTestMappings.filter((a) => a.roll === roll2)[0].ABTestValue;
    }

    if (roll3 > 89 && roll3 < 100) {
      ABTestValue3 = ABTestMappings.filter((a) => a.roll === roll3)[0].ABTestValue;
    }

    if (roll4 > 89 && roll4 < 100) {
      ABTestValue4 = ABTestMappings.filter((a) => a.roll === roll4)[0].ABTestValue;
    }

    it('if roll is 92', () => {
      if (roll) {
        expect(ABTestValue).toEqual(50);
      }
    })

    it('if roll is 95', () => {
      if (roll2) {
        expect(ABTestValue2).toEqual(60);
      }
    })

    it('if roll is 99', () => {
      if (roll3) {
        expect(ABTestValue3).toEqual(80);
      }
    })

    it('if roll is less than 90', () => {
      if (roll4) {
        expect(ABTestValue4).toEqual(30);
      }
    })
  })
})
从“/index”导入{ABTest};
描述('ABTEST测试',()=>{
它('ABTest函数',()=>{
expect(ABTest).toBeDefined();
})
描述('测试不同的辊',()=>{
常量ABTestMappings=[
{
滚动:90,
ABTestValue:40
},
{
登记:91,
ABTestValue:40
},
{
滚动:92,
ABTestValue:50
},
{
登记:93,
ABTestValue:50
},
{
滚动:94,
ABTestValue:60
},
{
登记:95,
ABTestValue:60
},
{
登记:96,
ABTestValue:70
},
{
滚动:97,
ABTestValue:70
},
{
滚动:98,
ABTestValue:80
},
{
滚动:99,
ABTestValue:80
}
];
常数辊=92;
常数2=95;
常数3=99;
常数4=80;
设ABTestValue=30;
设ABTestValue2=30;
设ABTestValue3=30;
设ABTestValue4=30;
如果(滚动>89和滚动<100){
ABTestValue=ABTestMappings.filter((a)=>a.roll==roll)[0]。ABTestValue;
}
如果(roll2>89&&roll2<100){
ABTestValue2=ABTestMappings.filter((a)=>a.roll==roll2)[0]。ABTestValue;
}
如果(roll3>89&&roll3<100){
ABTestValue3=ABTestMappings.filter((a)=>a.roll==roll3)[0]。ABTestValue;
}
如果(滚动4>89&&滚动4<100){
ABTestValue4=ABTestMappings.filter((a)=>a.roll==roll4)[0]。ABTestValue;
}
它('如果滚动是92',()=>{
如果(滚动){
期望值(ABTestValue),toEqual(50);
}
})
它('如果滚动是95',()=>{
if(roll2){
期望值(ABTestValue2)、toEqual(60);
}
})
它('如果滚动是99',()=>{
如果(滚动3){
期望值(ABTestValue3)、toEqual(80);
}
})
它('如果滚动小于90',()=>{
如果(滚动4){
期望值(ABTestValue4)、toEqual(30);
}
})
})
})

单元测试工作流程非常简单

  • 模拟要测试的方法/函数的依赖项及其返回值。为它创造一个隔离的环境。原则是单元测试不应该依赖于任何外部服务,例如发送真实的HTTP请求、查询真实的数据库、访问真实的文件系统

  • 执行要测试的方法/函数,测试每个分支(
    if…else
    switch…case
    ),语句、函数和行。这是报道的摘要

  • 断言(
    expect
    )结果,以及是否调用被测试的方法/函数所依赖的方法/函数。为什么?因为我们应该确保代码按预期执行

  • 在每个软件arch层中按照上述工作流测试每个方法/功能

    软件架构层意味着,例如,数据访问层、模型层、服务层、控制器/路由器层

    以下是正确的单元测试:

    index.ts

    (任何窗口)。rg_版本=“”;
    导出函数ABTest(debugMode=false){
    const roll=Math.floor(Math.random()*100);
    设ABTestValue=30;
    如果(调试模式){
    console.log(
    'TimedRefresh-AB测试问题-831(90及以上版本更改刷新时间)随机化值=',
    卷
    );
    }
    常量ABTestMappings=[
    {
    滚动:90,
    ABTestValue:40
    },
    {
    登记:91,
    ABTestValue:40
    },
    {
    滚动:92,
    ABTestValue:50
    },
    {
    登记:93,
    ABTestValue:50
    },
    {
    滚动:94,
    ABTestValue:60
    },
    {
    登记:95,
    ABTestValue:60
    },
    {
    登记:96,
    ABTestValue:70
    },
    {
    滚动:97,
    ABTestValue:70
    },
    {
    滚动:98,
    ABTestValue:80
    },
    {
    滚动:99,
    ABTestValue:80
    }
    ];
    如果(滚动>89和滚动<100){
    ABTestValue=ABTestMappings.filter(a=>a.roll===roll)[0]。ABTestValue;
    }
    (窗口与任何窗口一样).rg_version=`time${ABTestValue}`;
    如果(调试模式){
    log('TimedRefresh-ABTestValue-change in
    
    import { ABTest } from './index';
    
    describe('ABTEST test', () => {
      it('ABTest function', () => {
        expect(ABTest).toBeDefined();
      })
    
      describe('test different rolls', () => {
        const ABTestMappings = [
          {
            roll: 90,
            ABTestValue: 40
          },
          {
            roll: 91,
            ABTestValue: 40
          },
          {
            roll: 92,
            ABTestValue: 50
          },
          {
            roll: 93,
            ABTestValue: 50
          },
          {
            roll: 94,
            ABTestValue: 60
          },
          {
            roll: 95,
            ABTestValue: 60
          },
          {
            roll: 96,
            ABTestValue: 70
          },
          {
            roll: 97,
            ABTestValue: 70
          },
          {
            roll: 98,
            ABTestValue: 80
          },
          {
            roll: 99,
            ABTestValue: 80
          }
        ];
    
        const roll = 92;
        const roll2 = 95;
        const roll3 = 99;
        const roll4 = 80;
        let ABTestValue = 30;
        let ABTestValue2 = 30;
        let ABTestValue3 = 30;
        let ABTestValue4 = 30;
    
        if (roll > 89 && roll < 100) {
          ABTestValue = ABTestMappings.filter((a) => a.roll === roll)[0].ABTestValue;
        }
    
        if (roll2 > 89 && roll2 < 100) {
          ABTestValue2 = ABTestMappings.filter((a) => a.roll === roll2)[0].ABTestValue;
        }
    
        if (roll3 > 89 && roll3 < 100) {
          ABTestValue3 = ABTestMappings.filter((a) => a.roll === roll3)[0].ABTestValue;
        }
    
        if (roll4 > 89 && roll4 < 100) {
          ABTestValue4 = ABTestMappings.filter((a) => a.roll === roll4)[0].ABTestValue;
        }
    
        it('if roll is 92', () => {
          if (roll) {
            expect(ABTestValue).toEqual(50);
          }
        })
    
        it('if roll is 95', () => {
          if (roll2) {
            expect(ABTestValue2).toEqual(60);
          }
        })
    
        it('if roll is 99', () => {
          if (roll3) {
            expect(ABTestValue3).toEqual(80);
          }
        })
    
        it('if roll is less than 90', () => {
          if (roll4) {
            expect(ABTestValue4).toEqual(30);
          }
        })
      })
    })