Search code examples
javascriptfunctional-testingintern

How to clean up after failed Intern functional tests?


I have some functional tests that run using Intern (3) and the last step in the test is to do some cleanup, including clearing localStorage on the remote browser, and switching back to the original window by storing the window handle right away and switching back to it before the tests end (so any subsequent tests don't fail because they're trying to run on a closed window if the previous test ended on a different one). However if some chaijs assertions fail in a .then() the cleanup code at the end gets skipped. Is there a better way to do cleanup for functional tests that will still get run even when some assertions fail?

this.leadfoot.remote
    .get(require.toUrl(url))
    .execute(function() { return document.querySelector('#welcome').textContent})
    .then(welcome => {
        assert.equal(welcome, 'hello world', 'properly greeted');
    })
    .execute(function() {
        localStorage.clear();
    });

If the assertion fails it'll never clear localStorage at the end and if the next test that runs expects localStorage to be empty it will fail too. Is there a better way to clean up after a functional test?


Solution

  • Use an afterEach method.

    afterEach() {
        return this.remote
            .execute(function () {
                localStorage.clear();
            });
    },
    
    'my test'() {
        return this.remote
            .get(...)
            .execute(...)
            .then(welcome => {
                assert.equal(welcome, ...);
            });
    }