1

I am using tape for unit testing in Node JS. I wanted to ask one thing about better coding while writing unit testing.

When using different inputs to a single method, is it a better idea to use all cases inside one test function or different cases of single method in different test function?

test('getXYZ: get XYZ from db',
    function testGetXYZ(t) {

        var expected = 'string';
        var actual = utils.getXYZ("dassa", "dasds");

        t.equal(actual, expected);

        var expected = 'string';
        var actual = utils.getXYZ("dassa", "dasds");

        t.equal(actual, expected);

        t.end();
});

or,

test('getXYZ1: get XYZ from db',
    function testGetXYZ1(t) {

        var expected = 'string';
        var actual = utils.getXYZ("dsa", "eqwew");

        t.equal(actual, expected);

        t.end();
    });

test('getXYZ2: get XYZ from db',
    function testGetXYZ2(t) {

        var expected = 'string';
        var actual = utils.getXYZ("dsa", "eqwew");

        t.equal(actual, expected);

        t.end();
});

While writing all cases inside one function, the only problem I faced is my JS Lint start throwing an error saying method body is too large.

So, please suggest which is the better way to write Unit tests in Tape?

  • 2
    Purists will argue to separate the tests. I say do whatever seems most practical to you. Obviously there can be "too much" in a single test, but, especially if those two fields are somewhat related, keeping them together is fine. – user949300 Nov 1 '17 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.