【问题标题】:Execution of code in other describe blocks - jest在其他描述块中执行代码 - 开玩笑
【发布时间】:2022-02-18 15:49:01
【问题描述】:

我有以下测试设置:

describe(`my tests`, () => {
    describe(`Should not run`, () => {
        console.log(`Should not be printed`);
        describe(`Should also not run`, () => {
            console.log(`Should also not be printed`);
            test(`Then an exception is thrown`, () => {
                console.log(`Should definitely not be printed`);
                throw new Error(`whoops`);
            })
        })

        describe(`Should run`, () => {
            console.log(`Should run`)
            test(`Then I should get this list in the format I expect`, () => {
                console.log(`Should definitely be printed`);
            })
        })

        describe(`Should not run`, () => {
            console.log(`Should not be printed`);
            test(`Then I should get an empty list`, () => {
                throw new Error(`whoops`);
            })
        })
    })
})

当我要求 IntelliJ 运行测试用例 test('Then I should get this list in the format I expect') 时,我仍然得到以下输出:

> Should not be printed
> Should also not be printed
> Should run
> Should not be printed
> Should definitely be printed

其他describe 块的代码怎么会被执行?我认为describe 块允许您确定测试设置的范围,以便它仅针对该特定块内的测试运行。

【问题讨论】:

  • 使用describe.only
  • 无法在 2021.3.2 中重现 - 仅运行选定的测试(使用装订线中的图标开始测试时)。用 jest@25 和 jest@27 测试。您使用哪些 IDE 和 Jest 版本?
  • 使用 jest 24.9.0 和 IDE IntelliJ IDEA 2021.2.3(终极版)
  • 在使用 jest 27 的项目中运行相同的测试时,我仍然得到相同的行为

标签: javascript node.js intellij-idea jestjs


【解决方案1】:

看来这是standard and documented behaviour

Jest 在执行任何实际测试之前执行测试文件中的所有描述处理程序。这是在 before* 和 after* 处理程序内部而不是在 describe 块内部进行设置和拆卸的另一个原因。一旦描述块完成,默认情况下,Jest 按照它们在收集阶段遇到的顺序依次运行所有测试,等待每个测试完成并在继续之前进行整理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 2020-09-11
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    相关资源
    最近更新 更多