gpt4 book ai didi

javascript - Jest 在第一次失败后停止测试套件

转载 作者:可可西里 更新时间:2023-11-01 01:54:07 26 4
gpt4 key购买 nike

我正在使用 Jest用于测试。

我想要的是在当前测试套件中的测试失败时停止执行该测试套件。

--bail option这不是我需要的,因为它会在一个测试套件失败后停止其他测试套件。

最佳答案

感谢this comment on github我能够使用自定义 testEnvironment 解决此问题。为此,jest-circus 需要通过 npm/yarn 安装。
值得注意的是 jest will set jest-circus to the default runner with jest v27 .

首先需要调整jest配置:

jest.config.js

module.exports = {
rootDir: ".",
testRunner: "jest-circus/runner",
testEnvironment: "<rootDir>/NodeEnvironmentFailFast.js",
}

然后你需要实现一个自定义环境,上面的配置已经引用了它:

NodeEnvironmentFailFast.js

const NodeEnvironment = require("jest-environment-node")

class NodeEnvironmentFailFast extends NodeEnvironment {
failedDescribeMap = {}
registeredEventHandler = []

async setup() {
await super.setup()
this.global.testEnvironment = this
}

registerTestEventHandler(registeredEventHandler) {
this.registeredEventHandler.push(registeredEventHandler)
}

async executeTestEventHandlers(event, state) {
for (let handler of this.registeredEventHandler) {
await handler(event, state)
}
}

async handleTestEvent(event, state) {
await this.executeTestEventHandlers(event, state)

switch (event.name) {
case "hook_failure": {
const describeBlockName = event.hook.parent.name

this.failedDescribeMap[describeBlockName] = true
// hook errors are not displayed if tests are skipped, so display them manually
console.error(`ERROR: ${describeBlockName} > ${event.hook.type}\n\n`, event.error, "\n")
break
}
case "test_fn_failure": {
this.failedDescribeMap[event.test.parent.name] = true
break
}
case "test_start": {
if (this.failedDescribeMap[event.test.parent.name]) {
event.test.mode = "skip"
}
break
}
}

if (super.handleTestEvent) {
super.handleTestEvent(event, state)
}
}
}

module.exports = NodeEnvironmentFailFast

注意

我添加了 registerTestEventHandler 功能,这不是快速失败功能所必需的,但我认为它非常有用,特别是如果您之前使用过 jasmine.getEnv() 并且它与 async/await 一起工作!
您可以像这样在测试的内部注册自定义处理程序(例如 beforeAll Hook ):

// testEnvironment is globally available (see above NodeEnvironmentFailFast.setup)
testEnvironment.registerTestEventHandler(async (event) => {
if (event.name === "test_fn_failure") {
await takeScreenshot()
}
})

当一个test 失败时,同一describe 中的其他test 语句将被跳过。这也适用于嵌套的 describe block ,但 describe block 必须具有不同的名称。

执行以下测试:

describe("TestJest 3 ", () => {
describe("TestJest 2 ", () => {
describe("TestJest 1", () => {
beforeAll(() => expect(1).toBe(2))
test("1", () => {})
test("1.1", () => {})
test("1.2", () => {})
})

test("2", () => expect(1).toBe(2))
test("2.1", () => {})
test("2.2", () => {})
})

test("3", () => {})
test("3.1", () => expect(1).toBe(2))
test("3.2", () => {})
})

会产生如下日志:

 FAIL  suites/test-jest.spec.js
TestJest 3
✓ 3
✕ 3.1 (1 ms)
○ skipped 3.2
TestJest 2
✕ 2
○ skipped 2.1
○ skipped 2.2
TestJest 1
○ skipped 1
○ skipped 1.1
○ skipped 1.2

● TestJest 3 › TestJest 2 › TestJest 1 › 1

expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: 1

2 | describe("TestJest 2 ", () => {
3 | describe("TestJest 1", () => {
> 4 | beforeAll(() => expect(1).toBe(2))
| ^
5 | test("1", () => {})
6 | test("1.1", () => {})
7 | test("1.2", () => {})

at suites/test-jest.spec.js:4:33

● TestJest 3 › TestJest 2 › TestJest 1 › 1.1

expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: 1

2 | describe("TestJest 2 ", () => {
3 | describe("TestJest 1", () => {
> 4 | beforeAll(() => expect(1).toBe(2))
| ^
5 | test("1", () => {})
6 | test("1.1", () => {})
7 | test("1.2", () => {})

at suites/test-jest.spec.js:4:33

● TestJest 3 › TestJest 2 › TestJest 1 › 1.2

expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: 1

2 | describe("TestJest 2 ", () => {
3 | describe("TestJest 1", () => {
> 4 | beforeAll(() => expect(1).toBe(2))
| ^
5 | test("1", () => {})
6 | test("1.1", () => {})
7 | test("1.2", () => {})

at suites/test-jest.spec.js:4:33

● TestJest 3 › TestJest 2 › 2

expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: 1

8 | })
9 |
> 10 | test("2", () => expect(1).toBe(2))
| ^
11 | test("2.1", () => {})
12 | test("2.2", () => {})
13 | })

at Object.<anonymous> (suites/test-jest.spec.js:10:31)

● TestJest 3 › 3.1

expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: 1

14 |
15 | test("3", () => {})
> 16 | test("3.1", () => expect(1).toBe(2))
| ^
17 | test("3.2", () => {})
18 | })
19 |

at Object.<anonymous> (suites/test-jest.spec.js:16:31)

Test Suites: 1 failed, 1 total
Tests: 2 failed, 6 skipped, 1 passed, 9 total
Snapshots: 0 total
Time: 0.638 s, estimated 1 s

关于javascript - Jest 在第一次失败后停止测试套件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com