临沂网站建设报价,安卓aso优化工具,如何做汽车团购网站,网站搭建怎么弄的官方文档#xff1a;jest 中文文档
1、模拟某个函数#xff0c;并返回指定的结果
使用Jest测试JavaScript(Mock篇)
有这样一个需求#xff0c;mock掉Math.random方法#xff08;0#xff08;包括#xff09;#xff5e;1之间#xff09;#xff0c;使其返回指定的0…官方文档jest 中文文档
1、模拟某个函数并返回指定的结果
使用Jest测试JavaScript(Mock篇)
有这样一个需求mock掉Math.random方法0包括1之间使其返回指定的0.1
jest.spyOn(global.Math, random).mockReturnValue(0.1);test(when rate is 0~1, will not be sampled randomly, () {jest.spyOn(global.Math, random).mockReturnValue(0.1);const result sampler.shouldSample({...SAMPLE_OPTIONS,options: { rate: 0.5 },});expect(result.decision).toEqual(true);
});public shouldSample(config: Config): api.SamplingResult {const { rateDaily, rateCampaign } this._options;const { options } config;const rate normalize(typeof options?.rate number? options.rate: getCurrentRate(rateDaily, rateCampaign));return { decision: rate ! NOT_SAMPLED_RATE Math.random() rate }; // 希望这里能指定返回true
}