Search code examples
salesforcecypressjsforce

cleaning up back-end data in Salesforce between Cypress tests using JSforce


I'm using the Cypress testing framework to test a web application which has a Salesforce back-end.

I'm looking for a way to ensure that all data is cleaned up (deleted) from the Salesforce back-end after (or before) each test is run. My purpose is to make sure that all tests can be run independently and in any order, and to make sure that the back-end data gets cleaned up (deleted) even in the event of a failing test.

Unless you have any suggestions on a better way of implementing this, I'm thinking that the best approach is to use JSforce to perform the data clean up from within the Cypress test file.


Solution

  • You can use cy.task for this:

    cypress/support/index.js:

    beforeEach(() => {
        cy.task('cleanUp');
    });
    

    cypress/plugins/index.js:

    module.exports = ( on ) => {
        on('task', {
            cleanUp () {
                // do your thing with your back-end
            }
        });
    };
    

    For more, see my previous answer.