小编min*_*god的帖子

在 beforeEach() 中调用 userEvent.setup() - 这是好还是坏模式?

在需要在多个测试中调用 userEvent.setup() 的测试套件中,有时我会在 beforeEach 中执行 userEvent.setup() ,然后user在多个测试中引用相同的 var

let user: UserEvent
beforeEach(() => {
     user = userEvent.setup()
})

describe('SomeTests', () => {
    it('should test something', async () => {
        await user.click(someButton)
        ...
    })

    it('should test some other thing', async () => {
        await user.click(someButton)
        ...blahblahblah
    })
})
Run Code Online (Sandbox Code Playgroud)

但是,我担心这可能会导致并行运行测试出现问题,最好总是这样做:

describe('SomeTests', () => {
    it('should test something', async () => {
        const user = userEvent.setup()
        await user.click(someButton)
        ...blahblahblah
    })

    it('should test some other thing', async () => {
        const user …
Run Code Online (Sandbox Code Playgroud)

jestjs testing-library user-event

6
推荐指数
1
解决办法
1026
查看次数

标签 统计

jestjs ×1

testing-library ×1

user-event ×1