
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
graphql-tester
Advanced tools
Test Framework for executing integration tests against GraphQL Services
Test Framework for executing integration tests against GraphQL Services.
This example works against the live SWAPI Implementation available on http://graphql-swapi.parseapp.com. There is a real test, using Mocha and Chai for this under src/test/swapi/simple-swapi.tests.js
import {tester} from 'graphql-tester';
const test = tester({
url: 'http://graphql-swapi.parseapp.com'
});
// This tests a successful request for the name of person 1
test('{person(personID: 1) { name } }')
.then((response) => {
assert(response.success == true);
assert(response.status == 200);
assert(response.data.person.name == 'Luke Skywalker');
});
// This tests a request for the name of non-existant person 1234
test('{person(personID: 1234) { name } }')
.then((response) => {
assert(response.success == false);
assert(response.status == 200);
});
// This tests a malformed query
test('{person(personId: 1) { name } }')
.then((response) => {
assert(response.success == false);
assert(response.status == 400);
});
At it's core, this library allows you to make a request to a service and perform assertions on the response. The way that the request query is generated, and the response is asserted is entirely up to you. The above example uses a simple string for the query, and the core assert method. However, there is no need to use these mechanisms and you can use whatever makes the most sense for your tests.
The "tester" function is used to create a function that can be used to test a specific API. At creation time it is given all of the details needed to call the API, and then it can be used to make requests and get responses from the API. This function takes a single Object as a parameter, with the following keys:
This function will return a function that can be used to make requests to the API. This returned function takes a single parameter as being the GraphQL Query to execute, and will return a Promise for the response from the server. This Promise will be resolved with an Object containing:
If there was a catastrophic failure in making the request - connection refused, for example - then the Promise will instead be rejected.
Note that because a Promise is returned you get some benefits here. You can use this promise as a return in certain promise-aware testing tools - e.g. Mocha. You can also use the same promise to make assertions fit better into some BDD style tests.
When the "tester" function is used to create the GraphQL Tester to use, it is possible to pass in an Application Server configuration. This configuration will be used to start up an Application Server before each test is run, execute the query against this server and then shut the server down. Helper functions are provided to configure an Application Server for use with Express 4.x and HapiJS. Any other server can be used though, as long as you provide the configuration yourself.
An example of testing against an Express Server can be seen in src/test/servers/express-server.test.js.
In order to configure the tester to work against an Express server, you simply need to use the "create" function exported from the "graphql-tester/servers/express" module. This function is called with the configured but not-yet-running Express server, and returns the full configuration to pass in to the "server" property of the "tester" function.
An example of testing against a HapiJS Server can be seen in src/test/servers/hapi-server.test.js.
In oder to configure the tester to work against a HapiJS Server, you simply need to use the "create" function exported from the "graphql-tester/servers/hapi" module. This function is called with the plugin configuration to configure HapiJS with - this can be anything that you can legally pass to Hapi.Server.Register - and will return the full configuration to pass in to the "server" property of the "tester" function.
##### Arbitrary Application servers It is possible to use any arbitrary application server by providing the configuration yourself. This configuration currently takes the form of a Javascript Object that has a single key of "creator". This key is a function that is called with the port number to listen on - this will be dynamically generated every test - and should return a Promise for the server configuration. This server configuration should be a Javascript object that has keys of:
Note that because of how this works, it is possible to start a server before the tests run and re-use it between all tests - simply return the URL to the singleton server and never return a function to shut it down. This might be useful in some circumstances but it greatly increases the likelihood of cross-contamination of tests.
FAQs
Test Framework for executing integration tests against GraphQL Services
The npm package graphql-tester receives a total of 1,123 weekly downloads. As such, graphql-tester popularity was classified as popular.
We found that graphql-tester demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.