【问题标题】:Does Jest support dependent tasks?Jest 是否支持依赖任务?
【发布时间】:2018-06-30 11:45:24
【问题描述】:

如果之前的测试通过了,是否可以只在 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);;
  });
});

简单的例子,但是如果“数组长度”测试通过了,“第一项值”测试是否可以运行,没有只需执行以下操作

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);
    });
  }
});

【问题讨论】:

  • 还没玩过 Jest 但你不能只嵌套 tests 吗?
  • 谢谢@NicholasKyriakides 你是对的,不知道为什么我没有考虑到这一点。
  • 如果有效,请将其作为答案发布并自我接受,这样问题就会从未回答的队列中消失。
  • 不幸的是,它实际上似乎不起作用。我收到错误“无法嵌套测试。测试child test name 无法运行,因为它嵌套在parent test name 中。”我现在将使用 if 语句方法,并进一步深入挖掘。不幸的是,截止日期优先。
  • 可读性好,重复代码少。

标签: javascript node.js jestjs


【解决方案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)
  })
})

如你所见,函数/方法“test”依赖于你给它的真实值,所以如果条件等于 false,它使用 Jest 中内置的“test.skip”函数,从而忽略测试.如果它是真的,它只是使用正常的“测试”方法。

它不是最漂亮的,但它一直有效,直到有一天 Jest 制作了自己的此功能版本。

【讨论】:

    猜你喜欢
    • 2016-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 2011-01-05
    相关资源
    最近更新 更多