Jest 是否支持依赖任务?

And*_*ham 9 javascript node.js jestjs

如果之前的测试已经通过,是否可以只在 Jest 中运行测试?我在文档中找不到关于真实回调的任何内容。例如:

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);;
  });
});
Run Code Online (Sandbox Code Playgroud)

一个简单的例子,但是如果“数组长度”测试已经通过,“第一个项目值”测试是否有可能运行,不仅仅是执行以下操作

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);
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

小智 1

这是一种稍微古怪的方法:

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)
  })
})
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,函数/方法“test”取决于您给它的真值,因此如果条件等于 false,它将使用 Jest 中内置的“test.skip”函数,从而忽略测试。如果属实,则仅使用正常的“测试”方法。

它不是最漂亮的,但它可以工作,直到有一天 Jest 制作他们自己的此功能版本。