Mocking GraphQL for unit tests with Cypress

Unit testing GraphQL with Cypress

Cypress is great for front end mocking. Its purpose built for this with Promises in mind.Most commonly, cy.intercept is used for this.Eg.:cy.intercept(‘POST’, ‘/your-backend-api’, {}).as(‘backendAPI’);

cy.wait('@backendAPI').then(xhr => {

  expect(xhr.response.statusCode).to.equal(404);
});

Cypress Documentation

Network Requests | Cypress Documentation

What you’ll learn How Cypress enables you to stub out the back end with cy.intercept() What tradeoffs we make when we stub our network requests.

https://docs.cypress.io/guides/guides/network-requests

Examples for GraphQL

Cypress Documentation

Working with GraphQL | Cypress Documentation

What you’ll learn Best practices to alias multiple GraphQL queries or mutations for a group of tests. Overriding an existing intercept to modify…

https://docs.cypress.io/guides/testing-strategies/working-with-graphql

Unit Testing

For an example in .net, we can rely on something like Nunit combined wth Rest Sharp to get our GraphQL service tested.

Rest sharp is really just an option for us to make that REST call

you can use any other

Nunit, its just our assertion library, you can use any other here as well

Java based?

Well using RestTemplates and Junit would do great as basic tools.

Using Native GraphQL

Its best to try and work directly with these components.

https://www.graphql-tools.com/docs/mocking
https://graphql.org/blog/mocking-with-graphql/

Test infrastructure as code

8 types of infrastructure on construction projects - LetsBuild

‘Test infrastructure as code’ should really be part of your Test Strategy or testing game plan for the lack of one.

From experience working with docker and kubernetes, the top benefits for have been:

– easily manage the setup of Ci jobs,

– changes can be tracked and become the single source of truth,

– easily deploy test automation tooling such as selenium and pact infrastructure,

– manage Cloud costs effectively,

– setup and tear down Jenkins nodes with ease,

– easily chain jobs and share volumes if needed,

– deploying across regions, teams and environments are a breeze,

– setting up end to end development and test tooling for manual testing becomes quicker.

#automation #cloud #testing #jenkins #aws #azure #cicd #kubernetes #docker

Visibility – Your tests have value, showcase them.

https://www.automatetheplanet.com/wp-content/uploads/2019/01/test-automation-reporting-allure.png

Having automated tests are great! … but not sharing the results or centralizing them is not so great. 😦

Your automated tests have undeniable business value, don’t be shy about it. Excellence does not happen overnight. So even if your tests are in early development, share the results with your team.

So what can you do?

  • Setup a nightly run using some CI tool. I like Jenkins 😉
  • Promote the results by using a dashboard, email or a tool like slack.
  • Address failed tests daily. Fix them or add them to a backlog. Fix these soonest.
  • Prep a backlog of test automation scenarios that the entire team supports and can contribute towards.

In conclusion: 

Visibility and Reliability are really important. This makes them trust worthy, needed… valuable. 

Pursue it viciously. Take small incremental steps towards implementation.