如何解决使用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 举报,一经查实,本站将立刻删除。