首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >网络错误:在react-testing库中执行一定数量的测试后,连接ECONNREFUSED 127.0.0.1:5000

网络错误:在react-testing库中执行一定数量的测试后,连接ECONNREFUSED 127.0.0.1:5000
EN

Stack Overflow用户
提问于 2021-05-20 19:05:04
回答 1查看 138关注 0票数 0

我正在使用react- testing -library测试一个应用程序,并且我正在使用一个模拟的服务工作者。我的所有测试都通过了,直到最后一个测试在标题中出现了这个错误。当只测试给出错误隔离(test.only)的部分时,它不会抛出错误。错误指向localhost:5000,这是我的数据服务器(我的应用程序在3000上运行)

这是我的最后一个测试,只有在单独运行时才有效:

代码语言:javascript
复制
    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:

代码语言:javascript
复制
const headerFirstCell = await screen.findByRole('columnheader', {name: /name/i, })   
expect(headerFirstCell).toHaveTextContent('Name')

或者其他部分+所有其他部分:

代码语言:javascript
复制
 const headerSecondCell = await screen.findByRole('columnheader', {name: /courses/i,}) //second
 expect(headerSecondCell).toHaveTextContent('Courses')

然后所有的测试都会通过。看起来我在那之后添加的任何一行代码都会抛出错误。

我知道这可能不容易从这条线索中看出I'm lost...Any info...but??

以防这是我的setupTests.js:

代码语言:javascript
复制
import '@testing-library/jest-dom'

import { server } from './mocks/server.js'

beforeAll(() => server.listen())

afterEach(() => server.resetHandlers())

afterAll(() => server.close())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-20 21:26:34

只是在我的模拟服务工作者处理程序中的一个服务器url地址中有一个拼写错误。因此,当测试一个部分时,它有时会抛出一个错误,如果我把这个函数放在最后,测试就完成了,然后再对错误的url执行get请求。这就是为什么它会有这种奇怪的行为。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67619141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档