How to test an fql query?

Let say I have following gql query.


I want to mock the result of this query in my node(typescript) application. I am able to mock graphql but I am not sure how do I mock this?

I am using Ava for testing and nock for mocking http calls.


below code is for mocking graphql queries. Now I have fql so I am not sure how do I mock results of fql query. Can I consider it as normal http function and mock using library like nock ?

BTW, I am not using below mentioned code, just looking for something like this to test fql code

import { queryMock } from '../path/to/your/queryMock';
import { wait, render } from 'react-testing-library';
import React from 'react';

describe('Some test Relay Modern test', () => {
  it('should render the logged in users name', async () => {
  // This mocks all queries named 'StartQuery' with the data provided.
  name: 'StartQuery', <=======I have fql code, so this is not working
  variables: {
    userId: '123'
  data: {
    user: {
      id: '123',
      name: 'Some Name'

// This will render a Start-component that displays user 123's first and last name.
const r = render(<Start userId="123" />);
await wait(() => r.getByText('Some Name'));
expect(r.queryByText('Some Name')).toBeTruthy();

I’d like to get more insight in what you are looking for since testing is of great importance and I’m looking to provide some more guidance there in the future.

In general

To start off, the below query is merely a definition of a query which is hard to mock.


If you want to keep it simple initially a function like this that actually performs a query could easily be mocked.

function retrieveDistinct() {

Specifically on nock

What nock is doing is interesting though, after a quick read, my understanding is that it seems to contact the GraphQL endpoint prior to the test to have a kind of cache of how a query result looks like. Other frameworks might ‘simulate’ the GraphQL endpoint which seems harder to achieve (to me) for FQL.

The simulation of a real endpoint though by doing the real call is something that is feasible with FQL but I haven’t seen a Fauna library yet that does so I believe. To do something similar with FQL you would essentially automate the above.

  • Have a function where you specify what to mock, e.g Paginate(Distinct(Match(Index('myIndex'))))
  • Run a setup phase that contacts the database and runs the actual query. The result as well as the query will be stored.
  • Hook into client.query with a mock framework and override the query in case the result instead of actually performing the above query.

Of course, when data changes, you will have to redo this since Fauna is schemaless, new properties could appear.

Hey @databrecht thanks for quick reply.
I forgot to mention, I do have function like retrieveDistinct(), so I am mocking that only.
I have some graphql functions which connects to rest api, for that I am doing exactly like you said. I have some code written which gives mock data and using that we do test. I was wondering if something is available for FQL, I can utilize that. I will write code as you suggested and will put up here. May be I will get some suggestions to better my code or someone will benefit from it.

1 Like

Hi after trial and error, I am able get this code but still I am not sure how do I setup mock fo the function which executes FQL query i.e. retrieveDistinct()

my test file is like

import test from 'ava';
//Import some mock library like nok or sinon

test.before((t) => {
  //setup mock to get mock data

const testfqlDistinctName = async (t) => {
   let response = null;
   //mock setup here or before this to avoid actual data base hit
try {
    response = await fqlDistinctNameQuery();
} catch (error) {
console.error('fqlDistinctNameQuery error');

test('Get testfqlDistinctName gets data of distinct names', async (t) => {
  await testfqlDistinctName(t);

and my actual functions are like:

import { Expr, query as q } from 'faunadb';
import { IPeople } from './Interfaces;
import { fqlClient } from '/library/api';

const fqlDistinctNameQuery = (): Expr => {
   return q.Paginate(q.Distinct(q.Match(q.Index('myIndex'))));

const mapIndexResultToLocal = (data): IPeople[] => {
   //Logic to map results to local IPeople type data

const findDistinctNamesWithFql = async () => {
try {
     const { data }: any = await fqlClient.query(fqlDistinctNameQuery());
     return mapIndexResultToLocal(data);
} catch (e) {
     console.log('fql error: ', e);

export const fqlDistinctNamesQuery = async (): Promise<IPeople[]> => {
    const fqlNames: IPeople[] = await findDistinctNamesWithFql();
   return fqlNames;

Above test executes but it makes real database calls. I tried to mock but no luck there.Here is my unsuccessfull attempt.

   * Set up mock objects
test.before((t) => {
  const api_base="I dont think there is any api base,is there any url for FQL"
  const api_path= "/not sure which url to put"

 const uniqueNamesMock = nock(api_base).persist().get(new RegExp(api_path)).reply(200, 

 t.context = {

Any suggestions or points towards right direction would be very helpful.