Testing promises in NodeJS

I'm trying to learn about testing promises in NodeJS, and the testing methods that I used in other languages ​​do not suit me a bit. The main question: "How to effectively test indirect inputs and outputs in one or more then coded (and made or captured) blocks of promises?"

Here is the source lib/test.js:

var Bluebird = require("bluebird"),
    fs = Bluebird.promisifyAll(require("fs"));

function read(file) {
    return fs.readFileAsync(file)
        .then(JSON.parse)
        .done(function () {
            console.log("Read " + file);
        });
}

function main() {
    read("test.json");
}

if (require.main === module) {
    main();
}

module.exports = read;

And here is the source tests/test.js

var Bluebird = require("bluebird"),
    chai = require("chai"),
    expect = chai.expect,
    sinon = require("sinon"),
    sandbox = sinon.sandbox.create(),
    proxyquire = require("proxyquire");

chai.use(require("chai-as-promised"));
chai.use(require("sinon-chai"));

describe("test", function () {
    var stub, test;

    beforeEach(function () {
        stub = {
            fs: {
                readFile: sandbox.stub()
            }
        }
        test = proxyquire("../lib/test", stub);
    });

    afterEach(function () {
        sandbox.verifyAndRestore();
    });

    it("reads the file", function () {
        test("test.json");
        expect(stub.fs.readFile).to.have.been.calledWith("test.json");
    });
    it("parses the file as JSON", function () {
        stub.fs.readFileAsync = sandbox.stub().returns(Bluebird.resolve("foo"));
        sandbox.stub(JSON, "parse");
        test("test.json");
        expect(JSON.parse).to.have.been.calledWith("foo");
    });
    it("logs which file was read", function () {
        stub.fs.readFileAsync = sandbox.stub();
        sandbox.stub(JSON, "parse");
        test("bar");
        expect(console.log).to.have.been.calledWith("Read bar")
    });
});

I understand that these examples are trivial and inventive, but I try to understand how to test the promise chain, and not how to read the file and parse it as JSON. :)

In addition, I am not attached to any frameworks or anything like that, so if I inadvertently chose the bad one when capturing any of the included NodeJS libraries, the call will also be evaluated.

Thanks!

+4
2

, Mocha, promises. , promises , , , , .

describe("testing promises with mocha", () => {
   it("works by returning promises", () => {
      return Promise.resolve("This test passes");
   });
   it("fails by returning promises", () => {
      return Promise.reject(Error("This test fails"));
   });
   it("Lets you chain promises", () => {
      return fs.readFileAsync("test.json").then(JSON.parse);
   });
});

( NodeJS, node - function(){)

+2

@Benjamin Gruenbaum

lib/test.js:

var Bluebird = require("bluebird"),
    fs = Bluebird.promisifyAll(require("fs"));

function read(files) {
    return Bluebird.map(files, function (file) {
        return fs.readFileAsync(file)
            .then(JSON.parse)
            .then(function (data) {
                console.log("Read " + file);
            });
    });
}

function main() {
    read(["test.json", "test2.json"]);
}

if (require.main === module) {
    main();
}

module.exports = read;

tests/test.js:

var Bluebird = require("bluebird"),
    chai = require("chai"),
    chaiAsPromised = require("chai-as-promised"),
    expect = chai.expect,
    sinon = require("sinon"),
    sandbox = sinon.sandbox.create(),
    proxyquire = require("proxyquire");

chai.use(chaiAsPromised);
chai.use(require("sinon-chai"));

describe("test", function () {
    var stub, test;

    beforeEach(function () {
        stub = {
            fs: {
                readFile: sandbox.stub()
            }
        };
        sandbox.stub(JSON, "parse");
        test = proxyquire("../lib/test", stub);
        stub.fs.readFileAsync = sandbox.stub().returns(Bluebird.resolve());
    });

    afterEach(function () {
        sandbox.verifyAndRestore();
    });

    it("reads the files", function () {
        var files = ["test.json", "test2.json"];
        return test(files).then(function () {
            var expects = files.map(function (file) {
                return expect(stub.fs.readFileAsync).to.have.been.calledWith(file);
            });
            // expects.push(expect(Bluebird.resolve(1)).to.eventually.equal(2));
            return Bluebird.all(expects);
        });
    });
    it("parses the files as JSON", function () {
        var returns = ["foo", "bar"];
        returns.forEach(function (value, index) {
            stub.fs.readFileAsync.onCall(index).returns(Bluebird.resolve(value));
        });
        return test(["baz", "buz"]).then(function () {
            var expects = returns.map(function (value) {
                return expect(JSON.parse).to.have.been.calledWith(value);
            })
            // expects.push(expect(Bluebird.resolve(1)).to.eventually.equal(2));
            return Bluebird.all(expects);
        });
    });
    it("logs which files were read", function () {
        var files = ["bleep", "blorp"];
        sandbox.spy(console, "log");
        return test(files).then(function () {
            var expects = files.map(function (file) {
                return expect(console.log).to.have.been.calledWith("Read " + file);
            });
            // expects.push(expect(Bluebird.resolve(1)).to.eventually.equal(2));
            return Bluebird.all(expects);
        });
    });
});

1 === 2 , , (, .then , ).

+1

Source: https://habr.com/ru/post/1610482/


All Articles