Jestjs Jest测试初始条件
假设我有一个文件Jestjs Jest测试初始条件,jestjs,Jestjs,假设我有一个文件file1.js,其中包含: const a = true; let b; if (a) { b = c; } if (!a) { b = d; } 现在,当我在这个文件上运行测试用例时,我的第一个条件就被覆盖了。是否有任何方法可以通过将a设置为false来覆盖第二个条件,或者我是否应该更改代码,以便调用具有不同值的方法来测试每种情况,例如: const a = true; getBVal(a) { return a ? c : d; } let b
file1.js
,其中包含:
const a = true;
let b;
if (a) {
b = c;
}
if (!a) {
b = d;
}
现在,当我在这个文件上运行测试用例时,我的第一个条件就被覆盖了。是否有任何方法可以通过将a
设置为false来覆盖第二个条件,或者我是否应该更改代码,以便调用具有不同值的方法来测试每种情况,例如:
const a = true;
getBVal(a) {
return a ? c : d;
}
let b = getBVal(a);
更新:
下面是我为requestAnimationFrame编写的代码,其中包含旧浏览器的回退:
let lastTime = 0;
const vendors = ["ms", "moz", "webkit", "o"];
let rAF = window.requestAnimationFrame;
if (!rAF) {
rAF = vendors.find(prefix => window[`${prefix}RequestAnimationFrame`]);
}
if (!rAF) {
rAF = cB => {
const currTime = new Date().getTime();
const timeToCall = Math.max(0, 16 - (currTime - lastTime));
const id = setTimeout(() => {
cB(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
function requestAnimationFrame(callback) {
return rAF(callback);
}
export default requestAnimationFrame;
我在窗口对象的设置中使用jsdom。现在,如果我必须为window.requestAnimationFrame=null
测试用例,以我编写代码的方式是不可能的
现在将其更改为:
import { requestAnimationFrameVendor } from "../constants";
let lastTime = 0;
const customFn = cB => {
const currTime = new Date().getTime();
const timeToCall = Math.max(0, 16 - (currTime - lastTime));
const id = setTimeout(() => {
cB(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
function requestAnimationFrame(callback) {
const rAF = window.requestAnimationFrame;
return rAF && rAF(callback) || requestAnimationFrameVendor && requestAnimationFrameVendor(callback) || customFn(callback);
}
export default requestAnimationFrame;
然后,如果我编写如下测试:
import * as constants from "../../constants";
describe("animationFrame", () => {
let requestAnimationFrame;
let cancelAnimationFrame;
beforeAll(() => {
requestAnimationFrame = global.window.requestAnimationFrame;
cancelAnimationFrame = global.window.cancelAnimationFrame;
});
test("requestAnimationFrame", done => {
global.window.requestAnimationFrame = null;
global.window.cancelAnimationFrame = null;
const requestId1 = Utils.requestAnimationFrame(jest.fn());
constants.requestAnimationFrameVendor = jest.fn(() => {
return requestAnimationFrame;
});
const requestId2 = Utils.requestAnimationFrame(jest.fn());
setTimeout(() => {
Utils.cancelAnimationFrame(requestId1);
Utils.cancelAnimationFrame(requestId2);
done();
}, 300);
});
afterEach(() => {
global.window.webkitRequestAnimationFrame = null;
global.window.webkitCancelAnimationFrame = null;
});
});
那么它涵盖了所有的条件 我会选择第二条路线(
getBVal()
),因为它使您的接口更易于测试
通常,删除全局状态(如常量a
或let b
)将使代码和接口更易于测试。如果不能完全删除全局状态,可以引入抽象,这样测试就不需要知道全局状态(如建议的getBVal()
)
也许您可以更进一步,删除全局
b
:而是始终调用getBVal()
?在大多数情况下,性能影响可以忽略不计,您的代码变得更加可测试,耦合性更低……因此,使用jest,我无法更改文件中常量的值?我不知道有任何方法。即使有一个API,我也不会使用它,因为这会使测试与文件的实现过于紧密地结合在一起,我认为在许多情况下,拥有额外的API是一个好主意。将等待一周的任何其他回复。如果没有更好的答案,我会接受你的答案。ThanksHow这甚至可以在生产代码中调用吗?@AndreasKöberle只是一个例子。我只是想知道我们是否可以在一个文件中更改常量值以覆盖测试用例。这就是我的观点,当您不能在生产环境中这样做时,您可以/不应该在测试中这样做。@AndreasKöberle我添加了一个更新。请检查一下。如果有任何更好的建议或任何错误的测试,请添加一个答复。这会有很大帮助。注意:我知道我需要编写expect func来调用,但是现在我只编写了测试来查看覆盖率