Git Product home page Git Product logo

sync-browser-mocks's Introduction

Sync Browser Mocks

Synchronous browser mocks for common async browser apis: window.postMessage, window.Promise, window.setTimeout, window.setInterval, window.XmlHttpRequest

These mocks make it dramatically easier to write tests for browser-based code, by making the above utilities synchronous. This way, you can write synchronous tests without having to care about timeouts, race conditions, and tests which pass sometimes and fail other times because some async api did not return on time.

Inspired by ngMock's $timeout, $interval and $httpBackend, but works independently of Angular.

Usage

require('sync-browser-mocks').patchAll();

Promise

require('sync-browser-mocks').patchPromise();

No additional changes are needed to use synchronous Promises: as soon as your promise is resolved, rather than waiting for the next tick, your .then() and .catch() handlers will be immediately invoked.

var x = new Promise(function(resolve) {
    resolve('foobar');
});

x.then(function() {
    console.log('This will be logged first');
});

console.log('This will be logged second');

Obviously, this relies on your code not containing any race conditions where you explicitly rely on .then() being called on the next tick. These situations might break if you're using synchronous promises -- but that's a good thing, as they should be factored out.

setTimeout

require('sync-browser-mocks').patchSetTimeout();

Any time you want to flush any pending timeout functions, you will need to call setTimeout.flush(). For example:

setTimeout(function() {
    console.log('This will be logged second');
}, 200);

setTimeout(function() {
    console.log('This will be logged first');
}, 100);

setTimeout.flush();

setInterval

require('sync-browser-mocks').patchSetInterval();

Any time you want to flush any pending interval functions, you will need to call setInterval.cycle(). For example:

setInterval(function() {
    console.log('This will be logged second');
}, 200);

setInterval(function() {
    console.log('This will be logged first');
}, 100);

setInterval.cycle();
setInterval.cycle();

XmlHttpRequest

require('sync-browser-mocks').patchXmlHttpRequest();

This module sets up a mock http backend you can use to handle incoming ajax requests:

var $mockEndpoint = require('sync-browser-mocks').$mockEndpoint;

$mockEndpoint.register({
    method: 'GET',
    uri: '/api/user/.+',
    data: {
        name: 'Zippy the Pinhead'
    }
}).listen();

Dynamic handler:

$mockEndpoint.register({
    method: 'GET',
    uri: '/api/user/.+',
    handler: function() {
        return {
            name: 'Zippy the Pinhead'
        };
    }
}).listen();

Expecting calls in a test:

var $mockEndpoint = require('sync-browser-mocks').$mockEndpoint;

var myListener = $mockEndpoint.register({
    method: 'GET',
    uri: '/api/user/.+',
    data: {
        name: 'Zippy the Pinhead'
    }
});

it('should correctly call /api/user', function() {

    myListener.expectCalls();
    
    // Run some code which would call the api

    myListener.done(); // This will throw if the api was not called
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.