Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain how nested tests work with .plan() #122

Open
callumlocke opened this issue Dec 3, 2014 · 4 comments
Open

Explain how nested tests work with .plan() #122

callumlocke opened this issue Dec 3, 2014 · 4 comments

Comments

@callumlocke
Copy link

From experimenting with tape, it seems that an outer test's .plan() needs to account for all the inner tests too (even if they have their own plans), and also you need to count both t.test() and nested t.plan() calls as assertions when planning tests. Or maybe I'm doing something wrong?

Also I tried first without using .plan(), but using .end() instead, and I couldn't get nesting working at all. A very basic example of nesting would be useful (I know there's one in the ./example dir but it's a very complex example)

@callumlocke callumlocke changed the title Explain how nested tests work re: plan numbers Explain how nested tests work with plan numbers Dec 3, 2014
@callumlocke callumlocke changed the title Explain how nested tests work with plan numbers Explain how nested tests work with .plan() Dec 3, 2014
@miguelmota
Copy link

Like you said, .plan() needs to take account for all the inner assertions too.

This issue is old and I'm sure you figured it out but here's the example simply explained.

./example/nested.js:

var falafel = require('falafel');
var test = require('../');

test('nested array test', function (t) {
    t.plan(5); // there should be a total of 5 assertions within this test scope

    var src = '(' + function () {
        var xs = [ 1, 2, [ 3, 4 ] ];
        var ys = [ 5, 6 ];
        g([ xs, ys ]);
    } + ')()';

    var output = falafel(src, function (node) {
        if (node.type === 'ArrayExpression') {
            node.update('fn(' + node.source() + ')');
        }
    });

    t.test('inside test', function (q) {
        q.plan(2);  // there should be a total of 2 assertions within this test scope. This is 1st assertion of outer test scope.

        q.ok(true, 'inside ok'); // 2nd assertion in outer test scope. 1st assertion in this test scope.

        setTimeout(function () {
            q.ok(true, 'inside delayed'); // 3rd assertion in outer test scope. 2nd assertion in this test scope.
        }, 3000);
    });

    var arrays = [
        [ 3, 4 ],
        [ 1, 2, [ 3, 4 ] ],
        [ 5, 6 ],
        [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ],
    ];

    Function(['fn','g'], output)(
        function (xs) {
            t.same(arrays.shift(), xs); // 4th assertion in outer test scope.
            return xs;
        },
        function (xs) {
            t.same(xs, [ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]); // 5th assertion in outer test scope.
        }
    );
});

test('another', function (t) {
    t.plan(1);
    setTimeout(function () {
        t.ok(true);
    }, 100);
});

@benfleis
Copy link

benfleis commented Sep 2, 2015

A potential improvement would be to allow plan to increment the number of things being counted. Thus,

  t.plan(2);
  testAsyncThing(..., function() {
    t.ok(true);
    t.notOk(false);
  });

  t.plan(1);
  testAnotherThing(..., function(randInt) {
    t.pass();
    if (randInt % 2 === 0) {
      t.plan(1)
      testOnEven(..., function() {
        t.pass();
      }
    }
  });

Thoughts? (I will test it locally, since by my naive glance, this is trivial to implement.)

EDIT: the other reason I considered this is the case of iterative tests that may want to accumulate tests, as slightly exemplified above. It's far nicer to localize the .plan counts per section.

@strarsis
Copy link

+1 I also need it because I run functions that may call sub-functions that plan tap tests.

With best regards

@tejesh0
Copy link

tejesh0 commented Jun 25, 2018

@benfleis @strarsis A workaround for incremental test plans:

          first_test(t) {
            t.plan(2);
            testAsyncThing(..., function () {
              t.ok(true);
              t.notOk(false);
            });
          }
          
          second_test(t) {
            t.plan(1);
            testAnotherThing(..., function (randInt) {
              t.pass();
              if (randInt % 2 === 0) {
                t.plan(1)
                testOnEven(..., function () {
                  t.pass();
                }
             }
            });
          } 

          tape.test('parent test clause', function(test) {
            test.plan(2)
            test.test('nested test', function(t) {
              first_test(t)
            })
            test.test('nexted test 2', function(t) {
              second_test(t)
            })
          })

do let know if this is not helpful, please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants