passt test resulsts from postman/newman to mocha

33 Views Asked by At

I try to integrate my http tests from postman with newman into mocha so that they show up as test results.

The main problem i am facing is, that i cant define in mocha test cases asynchronous.
But newman reports test results asynchronous via event emitter.

I tried:

const newman = require("newman");
const collection = require("./postman.json");
const { describe, it } = require("mocha");

console.log("Started....");


const emitter = newman.run({
    collection,
    workingDir: __dirname
});


describe("Should test HTTP API", () => {
    emitter.on("done", (err, { run: { failures, executions } }) => {

        executions.forEach(({ request }) => {
            describe(`[${request.method}] - ${request.url.toString()}`, () => {

                // get test name from postman here
                // passt to mocha test to "fake" testing, but show in output

                /*
                it(`<test name>`, (done) => {
                    done()
                });
                */

            });
        });

    });
});

But as written above, the test suite/cases are not recognized by mocha and it just says:

0 passing (1ms)

My goal is to create a dynamic test suite from the collection as the following:

describe("Should test HTTP API", () => {
    describe("[GET] http://example.com/foo/bar/baz", () => {

        it("content-type = application/json", (done) => {
            // get err result from newman assertion here
            // pass it to mocha
            done(err);
        });

        it("Status code 200 || 202", (done) => {
            // get err result from newman assertion here
            // pass it to mocha
            done(err);
        });

        it("Response has no error field", (done) => {
            // get err result from newman assertion here
            // pass it to mocha
            done(err);
        });

    });
});

The testing is all done in newman/postman and only the result of the test should be passed to mocha.

I think, if im possible to add tests in mocha asynchron, im be able to resolve my issue with newman & mocha.

So, long story short: How can i add test in mocha asynchron and get the following work:

const { describe, it } = require("mocha");


describe("Should test HTTP API", () => {
    describe("[GET] http://example.com/foo/bar/baz", () => {

        setTimeout(() => {
            it("content-type = application/json", (done) => {
                done();
            });
        }, 1000);

        setTimeout(() => {
            it("Status code 200 || 202", (done) => {
                done();
            });
        }, 2000);

        setTimeout(() => {
            it("Response has no error field", (done) => {
                done();
            });
        }, 3000);

    });
});

I found https://gist.github.com/davfive/eae043135ed98b9647ad631bbfc1ab38 but that approach is not asynchron.

Hope its clear, and some one can point me into the right direction.

Thanks in advance.

0

There are 0 best solutions below