@cucumber-e2e/xhr-mock

0.0.1 • Public • Published

xhr-mock

xhr mock is the lib that allow to intercept and change XMLHttpRequest responses according to testing needs instead of injecting test data.

const { XMLHttpRequestMock, Rule } = require("xhr-mock");

const mock = new XMLHttpRequestMock([
    new Rule({
        condition: function (xhr) {
            return xhr.responseURL.includes("api/endpointtomock")
        },
        response: function () {
            return JSON.stringify({
                data: "yourData"
            })
        }
    })
]);

//browser - is protractor browser global
await mock.mock(browser);

Also it is possible to modify original response

const { XMLHttpRequestMock, Rule } = require("xhr-mock");

const mock = new XMLHttpRequestMock([
    new Rule({
        condition: function (xhr) {
            return xhr.responseURL.includes("api/endpointtomock")
        },
        responseText: function (originalResponseText) {
            return originalResponseText.replace("A", "B")
        }
    })
]);

//browser - is protractor browser global
await mock.mock(browser);

Dependencies (3)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i @cucumber-e2e/xhr-mock

    Weekly Downloads

    1

    Version

    0.0.1

    License

    MIT

    Unpacked Size

    35.4 kB

    Total Files

    13

    Last publish

    Collaborators

    • alexgalichenko