使用jest和apolloBoost测试graphql请求:网络错误:对http:// localhost:4000 /的请求失败

问题描述

当我尝试通过ApolloClient提交请求时出现此错误

$ yarn test
yarn run v1.19.0
$ env-cmd -f ./config/test.env jest --watchAll --runInBand
 FAIL  tests/user.test.js
  × Should create a new user  (1097 ms)
 
  ● Should create a new user 
 
    Network error: request to http://localhost:4000/ Failed,reason: connect ECONNREFUSED 127.0.0.1:4000

bellow是我的测试文件user.test.js

import "cross-fetch/polyfill"
import "@babel/polyfill/noConflict"
import ApolloBoost,{ gql } from "apollo-boost"
 
const client = new ApolloBoost({
  uri: "http://localhost:4000"
})
 
test("Should create a new user ",async () => {
  const REGISTER = gql`
    mutation {
      createuser(data: { userName: "test",email: "test@test.com",password: "password" }) {
        userName
      }
    }
  `
 
  const response = await client.mutate({
    mutation: REGISTER
  })
})

服务器也在我的globalSetup.js

上被调用

require("@babel/register")
require("@babel/polyfill/noConflict")
const server = require("../../src/graphql/index")
 
 
module.exports = async () => {
 
  global.httpServer = await server.start({ port: 4000 })
}
 

我尝试添加内部Ip地址而不是localhost,但是遇到了同样的问题。 知道为什么我会收到这个错误吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)