我正在使用react- testing -library测试一个应用程序,并且我正在使用一个模拟的服务工作者。我的所有测试都通过了,直到最后一个测试在标题中出现了这个错误。当只测试给出错误隔离(test.only)的部分时,它不会抛出错误。错误指向localhost:5000,这是我的数据服务器(我的应用程序在3000上运行)
这是我的最后一个测试,只有在单独运行时才有效:
import { findByRole, getByRole, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import Layout from '../../layout'
describe('tests for headers and content of the table', () => {
test('to check headers in inital state', async () => {
render(<Layout />)
const headerFirstCell = await screen.findByRole('columnheader', {name: /name/i, })
expect(headerFirstCell).toHaveTextContent('Name')
const headerSecondCell = await screen.findByRole('columnheader', {name: /courses/i,}) //second
expect(headerSecondCell).toHaveTextContent('Courses')
})同样奇怪的是,当我只运行最后一段代码的一部分+所有其余部分时,fex:
const headerFirstCell = await screen.findByRole('columnheader', {name: /name/i, })
expect(headerFirstCell).toHaveTextContent('Name')或者其他部分+所有其他部分:
const headerSecondCell = await screen.findByRole('columnheader', {name: /courses/i,}) //second
expect(headerSecondCell).toHaveTextContent('Courses')然后所有的测试都会通过。看起来我在那之后添加的任何一行代码都会抛出错误。
我知道这可能不容易从这条线索中看出I'm lost...Any info...but??
以防这是我的setupTests.js:
import '@testing-library/jest-dom'
import { server } from './mocks/server.js'
beforeAll(() => server.listen())
afterEach(() => server.resetHandlers())
afterAll(() => server.close())发布于 2021-05-20 21:26:34
只是在我的模拟服务工作者处理程序中的一个服务器url地址中有一个拼写错误。因此,当测试一个部分时,它有时会抛出一个错误,如果我把这个函数放在最后,测试就完成了,然后再对错误的url执行get请求。这就是为什么它会有这种奇怪的行为。
https://stackoverflow.com/questions/67619141
复制相似问题