Approval Testing in Cypress

Approval Testing in Cypress

Continuing with using Cypress for API testing, a lot of the focus is on the live API connected to a real database populated with known example data. The majority of the tests centre on "given this request, the data returned should look like this", i.e. approval testing.

Approval testing is the approach of using real data and comparing returned data against a known good version - the "approved" version.

In Cypress there is no way to compare one object against another in sufficent detail to support a multi-level API object. Fortunately, there is an easy way to add it. Again, API testing is really end-to-end testing so we extend using the cypress/e2e.js file, require the compare function(s) and attach them to the Cypress object.


const compare = require('./compare');

// ... other stuff = compare;

That was the easy part. The slightly more awkward part is the comparison function itself. We'd like to be able to write a test like this:

it('objects match', () => {

const golden = {
name: 'bob',
age: 99,
address: {
line1: '55 Imaginary Street',
line2: 'Unknown Town'
postcode: 'UT55 IST'
};, golden);

The comparison function needs to use the golden (expected) object as a reference, iterate through it's properties and find the matching property on the actual object.


export const match = (actual, expected, options = {}) => {

    const maxDepth = options.maxDepth || 999;
    const depth = options.depth || 0;
    const propertyName = || '';
    const exactIds = options.exactIds || false;
    const path = options.path || '';

    if (depth === 0) {
        cy.log('Comparing', JSON.stringify(actual, null, 2), JSON.stringify(expected, null, 2));

    if (depth > maxDepth) return;

    if (Array.isArray(expected)) {
        expect(Array.isArray(actual), `Array '${path}'`);

        for(const index in expected) {
            // Find matching record in the actual data before compare -
            // elements in the array may not be in same order in both objects.
            // Get first property in expected object and use that to match in actual.
            const expectedObject = expected[index];
            const [key, value] = Object.entries(expectedObject)[0];

            const actualObject = actual.find(x => x[key] === value);

            const newOptions = {
                depth: depth + 1,
                property: `index ${index}`,
                path: `${path}[${index}]`

            expect(actualObject, `Matching ${newOptions.path}.${key} = ${value}`);
            match(actualObject, expectedObject, newOptions);
    } else if (typeof expected === 'object') {
        expect(typeof actual, `Object '${path}'`).to.equal('object');

        Object.keys(expected).forEach(key => {

            const newOptions = {
                depth: depth + 1,
                property: key,
                path: `${path}.${key}`

            match(actual[key], expected[key], newOptions);

        // ok for actual data to have more properties but not less
        const keys1 = Object.keys(expected);
        const keys2 = Object.keys(actual);
        expect(keys2.length, `Number of object properties in '${path}'`);
    } else {
        expect(actual, `Object property '${path}' exists`);
        expect(actual, `'${path}'`);


I spent quite a bit of time trying to get the error messages to help as much as possible when there is a failure. The path helps to identify array indexes and nested objects when the api data and the compared object are large and could contain many elements.