Is it possible to only run a test in Jest if a previous test has passed? I can't find anything in the documentation about a truthy callback. For example:
var array = [1, 2, 3, 4];
describe("Test task", () => {
test("array length", () => {
expect(array).toHaveLength(4);;
});
test("first item value", () => {
expect(array[0]).toBe(1);;
});
});
Trivial example, but would it be possible for the "first item value" test to only run if the "array length" test has passed, without just doing the following
var array = [1, 2, 3, 4];
describe("Test task", () => {
test("array length", () => {
expect(array).toHaveLength(4);;
});
if(array.length == 4){
test("first item value", () => {
expect(array[0]).toBe(1);
});
}
});
Here is a slightly wacky way of doing this:
var array = [1, 2, 3]
describe("Test task", () => {
const conditionalTest = bool => (bool ? test : test.skip)
test("array length", () => {
expect(array).toHaveLength(4)
})
conditionalTest(array.length === 4)("first item value", () => {
expect(array[0]).toBe(1)
})
})
As you can see, the function/method "test" is dependent on a truthy value you give it, so if the condition equates to false, it uses the "test.skip" function built into Jest, thus ignoring the test. If its true, it just uses the normal "test" method.
It is not the prettiest, but it works until Jest someday makes their own version of this functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With