How I mocked my schema for my Cypress tests
Few days I was doing some UT using cypress but I wanted to mock my schema so the requests that were made by the front will receive a mocked value of the query made.
Was there when I decided to use easygraphql-now
to create a mocked server of my schema. So, how it works?
Install the package on my project
$ npm i easygraphql-now --save-dev
also, I installed concurrently
$ npm i concurrently --save-dev
Have my schema on the project
I created a .graphql
file with my schema on my project
Run the frontend and the mocked server at the same time
On my package.json
, I created a new script to run both projects at the time.
"scripts": {
"dev": "NODE_ENV=development nodemon server.js",
"build": "next build",
"start": "next start",
"now": "easygraphql-now schema.gql --graphiql --local -p=7000",
"front-dev": "concurrently 'npm run now' 'npm run dev'"
},
as you can see, easygraphql-now
has some flags:
- The first one: is the route and name of the GraphQL schema to mock.
--graphiql
: if the mocked server should display GraphiQL.--local
: to run it locally and not deploy it withnow
.-p=7000
: the port of the mocked server.
Now all the requests made to http://localhost:7000/ are going to return a mocked value.
If you like this package don’t forget to give a ⭐️ on GitHub.
Repo: https://github.com/EasyGraphQL/easygraphql-now
npm: https://www.npmjs.com/package/easygraphql-now
More easygraphql projects: https://github.com/EasyGraphQL