sazzer / graphql-tester Goto Github PK
View Code? Open in Web Editor NEWTest Framework for executing integration tests against GraphQL Services
Test Framework for executing integration tests against GraphQL Services
Often a GraphQL Service will require authentication to perform all of the required actions. It would be useful to be able to do this in the test tool.
Ideally this should be flexible enough to support arbitrary authentication systems, and certainly it needs to be able to support authentications systems that are more complex than just Basic Auth - e.g. OAuth. In the most complicated case, the authentication details might be dependant on the payload that is being sent to the server.
Currently we only support Node 5+. However, not everybody can use this version. There's no technical reason we can't support earlier versions though, so make sure it works.
Would be great to be able to handle cookies somehow. For my purposes this works:
diff --git a/src/main/index.js b/src/main/index.js
index 28c1254..c974ef6 100644
--- a/src/main/index.js
+++ b/src/main/index.js
@@ -8,7 +8,7 @@ export function tester({
contentType = 'application/graphql',
authorization = null
}) {
- return (query) => {
+ return (query, jar) => {
return new Promise((resolve, reject) => {
if (server) {
freeport((err, port) => {
@@ -40,7 +40,8 @@ export function tester({
method,
uri: url,
headers: headers,
- body: query
+ body: query,
+ jar: jar
}, (error, message, body) => {
if (server && typeof(server.shutdown) === 'function') {
server.shutdown();
But maybe there would be a more general way to include request options. How about using Object.assign
to change/add options to the request and have (query, requestOptions) =>
as the main function?
Just as a reminder...I'll start poking around here too, to see if I can lend a hand.
The currently latest available Release via npm and GitHub is
v0.0.5.
Since then ~3 commits have been made to master: v0.0.5...master
One of them being the merge of PR #10, adding a parameter to specify request options when running a query.
That Feature would allow the user of this Tool to specify headers other than content-type and authorization which are already supported.
@sazzer To ease the release cycle of npm packages there exists a tool called semantic-release which automatically creates GitHub and NPM releases when the master build goes green in CI. If you are interested I could come up with a PR.
@sazzer If you are no longer interested in maintaining this repository it would be awesome if you'd add a little note to the README on GitHub and NPM so that people know what they are up to.
Also I'd be happy to help maintain this project in the future.
fixes #13
Referencing this SO issue: http://stackoverflow.com/questions/41712608/graphql-tester-unit-tests-with-graphql-apollo-server/41842176#41842176.
Potentially something to add to the README?
The example URL mentioned in README is not working - http://graphql-swapi.parseapp.com/
Hi,
NPM package is missing this change: 10716ff
Any idea when the changed will be pushed up?
Thanks!
I have a GraphQL endpoint that requires authentication. I protect with some very simple (passport-based) middleware:
const redirectIfNotAuthenticated = (request, response, next) =>
request.isAuthenticated() && request.session.user
? next()
: response.redirect('/login');
// ...
app.use('/graphql', redirectIfNotAuthenticated, graphqlHTTP(request => ({ //...
This works great ... but not so great for testing. When GraphQL Tester gets that redirect response ('Found. Redirecting to /login'
) it tries to parse it like JSON:
// main/index.js
if (error) {
reject(error);
} else {
var result = JSON.parse(body); // body isn't JSON, its a redirect
resolve({ //...
It'd be really great if the library could detect redirects and resolve with its details, but if that doesn't fit the library then at the least it should reject the promise with the proper details (instead of throwing an un-catachable exception).
In other words, instead of the current behavior of throwing an error:
SyntaxError: Unexpected token F in JSON at position 0
at JSON.parse (<anonymous>)
at Request._callback (server/node_modules/graphql-tester/lib/main/index.js:72:43)
at Request.self.callback (server/node_modules/request/request.js:186:22)
at Request.<anonymous> (server/node_modules/request/request.js:1163:10)
at IncomingMessage.<anonymous> (server/node_modules/request/request.js:1085:12)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)
it would be better if it did something like:
} else {
try {
preValidateThatBodyIsJSON(body);
var result = JSON.parse(body);
resolve({ //...
} catch (parseError) {
reject(parseError);
}
(where preValidateThatBodyIsJSON
throws a catch-able error if the body is not JSON)
In the same way that Supertest does, it could be useful to configure GraphQL-Tester with a configured application server - Express, Hapi, Koa, etc - and a URL path into that application server. The test tool should then start up the application server on an available port, and make calls to that instead. This will make it possible to do in-process integration testing - which includes the ability to mock out external services, and to better control what the service is doing.
Any plans to support mutations?
My application is working in production and works perfectly with graphiql, but i'm getting this response when i use this package:
{ raw: '{"errors":[{"message":"Must provide query string."}]}',
data: undefined,
errors: [ { message: 'Must provide query string.' } ],
headers:
{ 'x-powered-by': 'Express',
'access-control-allow-origin': '*',
'content-type': 'application/json; charset=utf-8',
'content-length': '53',
etag: 'W/"35-1vperDe+r7EpHry/i+J3wg"',
date: 'Sun, 17 Sep 2017 17:39:45 GMT',
connection: 'close' },
status: 400,
success: false }
What can i do to make it work?
our graphql requires a couple of custom headers for authentication. Would be nice to be able to pass in custom headers through _ref.
I'm still getting used to testing libraries so please correct me if my approach is wrong.
When the provided GraphQL endpoint cannot be connected to,
the test still continues against that unreachable URL.
When you instantiate a tester
, shouldn't an introspection query be thrown via the url supplied or check that it returns 200 or something, and if it doesn't, stop the test there?
I'd be down to write something.
Hey, thanks for this tester!
I just tried switching my fairly simple integration test to an internal wrapped express server but the newly modified test times out and prints:
(node:16407) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError: expected false to be truthy
1) responds with session id
(node:16407) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 4): AssertionError: expected false to be truthy
What am I doing wrong?
As an aside, is there a good way to send cookies along with requests?
We need to test our API end point which uses Authentication in header, whereas the tester function will take url, server, method and contenttype.
Can we have Authentication in tester function?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.