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

before and after blocks #440

Closed
imatefx opened this issue Sep 14, 2015 · 6 comments
Assignees
Labels
Milestone

Comments

@imatefx
Copy link

@imatefx imatefx commented Sep 14, 2015

when i run a specific test using the id

./node_modules/lab/bin/lab -v -i 2

is it supposed to run all the befores and after blocks??

@danielb2

This comment has been minimized.

Copy link
Contributor

@danielb2 danielb2 commented Sep 14, 2015

Yes, it should. the one specified for that scope at least. Generally we avoid using before and after even though it's supported.

@imatefx

This comment has been minimized.

Copy link
Author

@imatefx imatefx commented Sep 14, 2015

Is there any way to run what is related to just that specific test

@danielb2

This comment has been minimized.

Copy link
Contributor

@danielb2 danielb2 commented Sep 14, 2015

The before/after blocks should run if they're related. What else are you thinking?

@imatefx

This comment has been minimized.

Copy link
Author

@imatefx imatefx commented Sep 14, 2015

var Lab = require('lab');
var Code = require('code');

var lab = exports.lab = Lab.script();
var describe = lab.experiment;
var expect = Code.expect;
var it = lab.test;


describe('main', function () {
    describe('sub1', function () {

        lab.before(function (done) {
            console.log('\t\t before test1')
            done();
        });
        it('test1', function (done) {
            console.log('\t\t test1')

            done();
        });
        lab.after(function (done) {
            console.log('\t\t after test1')
            done();
        });


        lab.before(function (done) {
            console.log('\t\t before test2')
            done();
        });
        it('test2', function (done) {
            console.log('\t\t test2')

            done();
        });
        lab.after(function (done) {
            console.log('\t\t after test2')
            done();
        });
    });


        describe('sub2', function () {

        lab.beforeEach(function (done) {
            console.log('\t\t before Each Sub2')
            done();
        });
        it('test3', function (done) {
            console.log('\t\t test3')

            done();
        });
        lab.after(function (done) {
            console.log('\t\t after test3')
            done();
        });


        lab.before(function (done) {
            console.log('\t\t before test4')
            done();
        });
        it('test4', function (done) {
            console.log('\t\t test4')

            done();
        });
        lab.after(function (done) {
            console.log('\t\t after test4')
            done();
        });
    });
});

If i run the above test using

./node_modules/lab/bin/lab -v  -d

I get

main
  sub1
    - 1) test1 (0 ms)
    - 2) test2 (0 ms)
  sub2
    - 3) test3 (0 ms)
    - 4) test4 (0 ms)

if i use

./node_modules/lab/bin/lab -v -i 2

I get the following output

         before test1
         before test2
         test2
main
  sub1
    ✔ 2) test2 (0 ms)
         after test1
         after test2
         before test4
         after test3
         after test4


1 tests complete
Test duration: 18 ms

Should the other before and after tests run for that specific test?

@danielb2

This comment has been minimized.

Copy link
Contributor

@danielb2 danielb2 commented Sep 14, 2015

Think of before() and after() as meaning running before and after ALL the tests in a scope. The place you define the function, literally before and after, has no bearing on when they're executed.

var Lab = require('lab');
var Code = require('code');

var lab = exports.lab = Lab.script();
var describe = lab.experiment;
var expect = Code.expect;
var it = lab.test;


lab.before(function (done) {

    console.log('runs once before all tests');
    done();
});

lab.after(function (done) {

    console.log('runs once after all tests');
    done();
});




describe('main', function () {

    describe('sub1', function () {

        it('test1', function (done) {

            console.log('\t\t test1');

            done();
        });

        it('test2', function (done) {

            console.log('\t\t test2');

            done();
        });
    });


    describe('sub2', function () {

        lab.beforeEach(function (done) {

            console.log('\t\t before Each test in Sub2');
            done();
        });

        it('test3', function (done) {

            console.log('\t\t test3');

            done();
        });

        it('test4', function (done) {

            console.log('\t\t test4');

            done();
        });
    });
});

@imatefx

This comment has been minimized.

Copy link
Author

@imatefx imatefx commented Sep 14, 2015

ok thanks,
I think it would be great to restrict it based on the contained describe block

@geek geek added the request label Sep 14, 2015
@geek geek added feature and removed request labels May 18, 2016
@geek geek added this to the 10.6.0 milestone May 18, 2016
@geek geek self-assigned this May 18, 2016
@cjihrig cjihrig closed this in #584 May 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.