Last active
September 21, 2017 08:44
-
-
Save KlavierCat/2910cdf8d6a6a2c61f17352343a7d0b0 to your computer and use it in GitHub Desktop.
Integration Server for GraphQL testing using NodeJS (with Express) and MongoDB (with Mongoose)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import express from 'express'; | |
import graphqlHTTP from 'express-graphql'; | |
import request from 'request-promise'; | |
import graphqlSchema from '../src/data/schema'; | |
function start(appPort) { | |
const app = express(); | |
const PORT = appPort || 9000; | |
const schema = graphqlSchema(); | |
app.use('/graphql', graphqlHTTP(req => ({ | |
schema, | |
graphiql: false | |
}))); | |
return app.listen(PORT, () => { | |
console.log('Integration server started at port [%s]', PORT); | |
}); | |
}; | |
function stop(app) { | |
console.log('Integration server stopped'); | |
app.close(); | |
}; | |
function graphqlQuery(app, query, variables) { | |
let requestBody; | |
if (variables !== undefined){ | |
requestBody = { | |
"query": query, | |
"variables": variables | |
} | |
} else { | |
requestBody = { | |
"query": query | |
} | |
} | |
return request({ | |
method: 'POST', | |
url : `http://localhost:${app.address().port}/graphql`, | |
headers: { | |
'content-type': 'application/json' | |
}, | |
body: requestBody, | |
resolveWithFullResponse: true, | |
json: true, | |
}, function(error, response, body) { | |
if(error){ | |
throw error; | |
} | |
}) | |
}; | |
module.exports = { | |
start, | |
stop, | |
graphqlQuery | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import mongoose from 'mongoose'; | |
/** data model that needs to be cleaned up from the database after each test */ | |
import Bookmark from '../src/models/bookmark'; | |
require('dotenv').load(); | |
async function cleanup() { | |
await Bookmark.remove({}); | |
} | |
async function start() { | |
mongoose.Promise = global.Promise; | |
await mongoose.connect(process.env.TEST_DB_HOST); | |
console.info('mongoose connection started'); | |
} | |
async function stop() { | |
await mongoose.disconnect(); | |
console.info('Done, mongoose connection disconnected.'); | |
} | |
module.exports = { | |
start, | |
stop, | |
cleanup, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import integrationServer from '../../testHelper/integrationServer'; | |
import mongoUtils from '../../testHelper/mongoUtils'; | |
describe('mutation schema integration test', () => { | |
let app; | |
const fakedBookmark = { | |
url: "test url", | |
title: "test title", | |
}; | |
const query = ` | |
query{ | |
me{ | |
bookmarks{ | |
url | |
title | |
} | |
} | |
} | |
`; | |
beforeAll(async () => { | |
await mongoUtils.start(); | |
app = integrationServer.start(); | |
}); | |
afterAll(async () => { | |
integrationServer.stop(app); | |
await mongoUtils.stop(); | |
}); | |
beforeEach(async () => { | |
await mongoUtils.cleanup(); | |
}); | |
afterEach(async () => { | |
await mongoUtils.cleanup(); | |
}); | |
it('Should be able to add a bookmark', async () => { | |
const responseBeforeAdd = await integrationServer.graphqlQuery(app, query); | |
expect(responseBeforeAdd.body.data.me.bookmarks).toEqual([]); | |
const variables = { | |
input: fakedBookmark | |
}; | |
const mutation = ` | |
mutation($input: NewBookmark!){ | |
addBookmark(input: $input){ | |
url | |
title | |
} | |
} | |
`; | |
const mutationResponse = await integrationServer.graphqlQuery(app, mutation, variables); | |
expect(mutationResponse.body.data.addBookmark).toEqual(fakedBookmark); | |
const responseAfterAdd = await integrationServer.graphqlQuery(app, query); | |
expect(responseAfterAdd.body.data.me.bookmarks).toEqual([fakedBookmark]); | |
}); | |
it('Should be able to delete bookmark by ID', async () => { | |
const responseBeforeAdd = await integrationServer.graphqlQuery(app, query); | |
expect(responseBeforeAdd.body.data.me.bookmarks).toEqual([]); | |
const addBookmarkVariables = { | |
input: fakedBookmark | |
}; | |
const addBookmarkMutation = ` | |
mutation($input: NewBookmark!){ | |
addBookmark(input: $input){ | |
_id | |
} | |
} | |
`; | |
const addBookmarkResponse = await integrationServer.graphqlQuery(app, addBookmarkMutation, addBookmarkVariables); | |
const bookmarkId = addBookmarkResponse.body.data.addBookmark._id; | |
expect(bookmarkId).not.toBeNull(); | |
const responseAfterAdd = await integrationServer.graphqlQuery(app, query); | |
expect(responseAfterAdd.body.data.me.bookmarks).toEqual([fakedBookmark]); | |
const deleteBookmarkVariables = { | |
input: bookmarkId | |
}; | |
const deleteBookmarkMutation = ` | |
mutation($input: String!){ | |
deleteBookmarkById(input: $input){ | |
url | |
title | |
} | |
} | |
`; | |
const deleteBookmarkResponse = await integrationServer.graphqlQuery(app, deleteBookmarkMutation, deleteBookmarkVariables); | |
const deletedBookmark = deleteBookmarkResponse.body.data.deleteBookmarkById; | |
expect(deletedBookmark).toEqual(fakedBookmark); | |
const responseAfterDelete = await integrationServer.graphqlQuery(app, query); | |
expect(responseAfterDelete.body.data.me.bookmarks).toEqual([]); | |
}); | |
it('Should be able to delete bookmark by URL', async () => { | |
const responseBeforeAdd = await integrationServer.graphqlQuery(app, query); | |
expect(responseBeforeAdd.body.data.me.bookmarks).toEqual([]); | |
const addBookmarkVariables = { | |
input: fakedBookmark | |
}; | |
const addBookmarkMutation = ` | |
mutation($input: NewBookmark!){ | |
addBookmark(input: $input){ | |
url | |
} | |
} | |
`; | |
const addBookmarkResponse = await integrationServer.graphqlQuery(app, addBookmarkMutation, addBookmarkVariables); | |
const bookmarkUrl = addBookmarkResponse.body.data.addBookmark.url; | |
expect(bookmarkUrl).toBe(fakedBookmark.url); | |
const responseAfterAdd = await integrationServer.graphqlQuery(app, query); | |
expect(responseAfterAdd.body.data.me.bookmarks).toEqual([fakedBookmark]); | |
const deleteBookmarkVariables = { | |
input: bookmarkUrl | |
}; | |
const deleteBookmarkMutation = ` | |
mutation($input: String!){ | |
deleteBookmarkByUrl(input: $input){ | |
url | |
title | |
} | |
} | |
`; | |
const deleteBookmarkResponse = await integrationServer.graphqlQuery(app, deleteBookmarkMutation, deleteBookmarkVariables); | |
const deletedBookmark = deleteBookmarkResponse.body.data.deleteBookmarkByUrl; | |
expect(deletedBookmark).toEqual(fakedBookmark); | |
const responseAfterDelete = await integrationServer.graphqlQuery(app, query); | |
expect(responseAfterDelete.body.data.me.bookmarks).toEqual([]); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment