I am currently using testcafe for my e2e tests.
As testcafe doesn't support async code, I have to mock my graphql queries.
Is there any other way than using RequestMock function ?
It's not really developer friendly as it's heavily REST focused
EDIT:
Regarding the fact that I testcafe doesn't allow the usage of async code, here is some details: The setBody function is lost if I try to use async code (query real data). There is an issue here
const handler = async (req, res) => {
// ok
res.setBody('foobar')
data = await Promise.resolve({ some: 'data' })
// setBody is no longer defined
res.setBody(JSON.stringify(data))
}
fixture('Register')
.page('http://foobar/register')
.requestHook(handler)
EDIT 2:
I have added the two files I am currently using.
Please take a look at apolloHook.js just before the export default
// apolloHook.js
import { RequestMock } from 'testcafe'
import fetch from 'node-fetch'
import gql from 'graphql-tag'
import configureApolloClient from '../../../src/ApolloClient'
const urlToCatch = 'http://127.0.0.1/index.php/graphql/'
const client = configureApolloClient({
uri: 'http://nginx/index_test.php/graphql/',
fetch
})
const handler = async (req, res) => {
const body = JSON.parse(req.body.toString('utf8'))
const query = gql`${body.query}`
const { variables } = body
let data = null
if (body.query.startsWith('query')) {
data = await client.query({ query, variables })
} else {
data = await client.mutate({ mutation: query, variables })
}
// res.setBody is undefined !
res.setBody(JSON.stringify(data))
}
export default RequestMock()
.onRequestTo({ url: urlToCatch, method: 'POST' })
.respond(handler)
// register.js
import { Selector } from 'testcafe'
import { REGISTER_FIXTURES } from '../fixtures'
import { testInput, testPhoneInput, testCountryInput, testCountriesInput, testSelect } from '../utils/utils'
import apolloHook from '../utils/apolloHook'
const registerUrl = 'http://front:3000'
//Uncomment for local testing
//const registerUrl = 'http://127.0.0.1:3000'
fixture('Register')
.page(registerUrl)
.requestHooks(apolloHook)
test('Fill out sign up', async t => {
await testInput(t, Selector('[data-testid=\'email\']'), 'john.doe@professional.com')
await t
.click(Selector('[data-testid=\'send-me-a-code\']'))
.expect(Selector('[data-testid=\'one-time-password-button\']').innerText).eql('Submit')
})