How to check GraphQl API?

I need to write a functional test suite (which will validate the GraphQl API). The test suite will be in a separate repo and API container.

One approach that I thought was to use a BDD environment inside a test suite. The package will run all BDD tests after receiving an HTTP request.

I was considering using Cucumber.js as the basis of BDD. I know there is npm test . I am not sure how I will perform the tests. Thus, it seems a little inconvenient to use the unit testing infrastructure. Does this approach make sense?

What tool exists to do something like this? I am open to learning different languages ​​and tools.

+16
testing graphql functional-testing
source share
2 answers

Karate is a relatively new web service testing automation platform that is well suited for testing GraphQL responses due to two specific features.

  • text manipulation: easy to execute GraphQL inline queries, read them from (reusable) files and replace placeholders
  • JsonPath statements: Although GraphQL answers are JSON, they change dynamically depending on the request (without a fixed schema) and tend to be deeply nested. JsonPath's own karate statements allow you to focus only on the pieces you need, and you can express the expected results in a concise JSON form, which is very readable.

Here is a good example: graphql.feature with a snippet below:

 # you can also read this query from a file Given text query = """ { pokemon(name: "Pikachu") { id number name attacks { special { name type damage } } } } """ And request { query: '#(query)' } When method post Then status 200 # json-path makes it easy to focus only on the parts you are interested in # which is especially useful for graph-ql as responses tend to be heavily nested * match $.data.pokemon.number == '025' # the '..' wildcard is useful for traversing deeply nested parts of the json * def attacks = get[0] response..special * match attacks contains { name: 'Thunderbolt', type: 'Electric', damage: 55 } 

For non-Java commands, Karate provides a binary executable that requires only a JRE, and Visual Studio code is sufficient as an IDE . JavaScript programmers will especially feel at home because of the way karate embeds the JavaScript runtime and supports soft JSON (no double quotation marks, no need to enclose JSON keys in quotation marks).

EDIT: here's a 10 minute video of a lightning conversation with demos that explain it.

Disclaimer: Dev is here.

+5
source share

You can simply use the npm test with any invited tester. I use mocha and tea. Jest might be a little better, as I consider it probably the most advanced test suite. You just created the tests, like any endpoints.

  it('should be null when user is not logged in', async () => { const query = ` query { user(id: "") { username email } } ` const rootValue = {}; const context = {}; const result = await graphql(schema, query, rootValue, context); const { data } = result; expect(data.user).to.equal(null); }); 

pretty easy way to check it out. You also run the statement before pasting the appropriate user into your db. The problem with maintaining a separate test suite is that you need to access db directly. Your tests should not rely on other API calls, as this creates unnecessary dependencies. Therefore, if the test is interrupted, suddenly the root cause is more difficult to understand.

+1
source share

All Articles