Intern Example Object Style Setup Beforeeach Teardown

intern

// Intern - Object style (example with setup, beforeEach, and teardown):

define([
    'intern!object',
    'intern/chai!assert',
    '../Request'
], function (registerSuite, assert, Request) {
    var request,
        url = 'https://github.com/theintern/intern';

    registerSuite({
        name: 'demo',

        // before the suite starts
        setup: function () {
            request = new Request();
        },

        // before each test executes
        beforeEach: function () {
            request.reset();
        },

        // after the suite is done
        teardown: function () {
            request.cleanup();

            if (!request.cleaned) {
                throw new Error('Request should have been cleaned up after suite execution.');
            }
        },

        // asynchronous test for Promises/A-based interfaces
        '#getUrl (async)': function () {
            // `getUrl` returns a promise
            return request.getUrl(url).then(function (result) {
                assert.equal(result.url, url, 'Result URL should be requested URL');
                assert.isTrue(
                  result.data.indexOf('next-generation') > -1, 
                  'Result data should contain term "next-generation"'
                );
            });
        },

        // asynchronous test for callback-based interfaces
        '#getUrlCallback (async)': function () {
            // test will time out after 1 second
            var dfd = this.async(1000);

            // dfd.callback resolves the promise as long as no errors are thrown from within the callback function
            request.getUrlCallback(url, dfd.callback(function () {
                assert.equal(result.url, url, 'Result URL should be requested URL');
                assert.isTrue(
                  result.data.indexOf('next-generation') > -1, 
                  'Result data should contain term "next-generation"'
                );
            });

            // no need to return the promise; calling `async` makes the test async
        },

        // nested suites work too
        'xhr': {
            // synchronous test
            'sanity check': function () {
                assert.ok(request.xhr, 'XHR interface should exist on `xhr` property');
            }
        }
    });
});
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License