Skip to content
Permalink
Browse files

Implement tests for supporting promises for setup, teardown and test …

…functions
  • Loading branch information
rluba committed Feb 27, 2016
1 parent e720c01 commit 83ab77dc7481d5c04c606693289589debd9c93de
Showing with 270 additions and 19 deletions.
  1. +24 −17 test/index.js
  2. +246 −2 test/runner.js
@@ -793,42 +793,49 @@ describe('Lab', () => {
};
});

it('throws on invalid functions', (done) => {
it('should not throw on tests without a function', (done) => {

const script = Lab.script();
const script = Lab.script({ schedule: false });

expect(() => {

script.test('a');
}).not.to.throw();
done();
});

it('should not throw on tests with a function without arguments', (done) => {

const script = Lab.script({ schedule: false });

expect(() => {

script.test('a', () => {});
}).to.throw('Function for test "a" should take exactly one argument');
}).not.to.throw();
done();
});

['before', 'beforeEach', 'after', 'afterEach'].forEach((fn) => {
['before', 'beforeEach', 'after', 'afterEach'].forEach((fnName) => {

expect(() => {
it(`should throw on "${fnName}" without a function`, (done) => {

script.experiment('exp', () => {

script[fn]();
});
}).to.throw('Function for ' + fn + ' in "exp" should take exactly one argument');
const script = Lab.script({ schedule: false });

expect(() => {

script.experiment('exp', () => {

script[fn](() => {});
});
}).to.throw('Function for ' + fn + ' in "exp" should take exactly one argument');
script[fnName]();
}).to.throw(`${fnName} in "script" requires a function argument`);
done();
});

Lab.execute(script, null, null, (err, notebook) => {
it(`should not throw on "${fnName}" with a function without arguments`, (done) => {

expect(err).to.not.exist();
const script = Lab.script({ schedule: false });

expect(() => {

script[fnName](() => {});
}).not.to.throw();
done();
});
});
@@ -99,6 +99,250 @@ describe('Runner', () => {
});
});

it('should fail test that neither takes a callback nor returns anything', (done) => {

const script = Lab.script({ schedule: false });

script.test('a', () => {});

Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(1);
expect(notebook.failures).to.equal(1);
expect(notebook.tests[0].err.toString()).to.contain('Function for "a" should either take a callback argument or return a promise');
done();
});
});

it('should fail test that neither takes a callback nor returns a valid promise', (done) => {

const script = Lab.script({ schedule: false });

script.test('a', () => {

return { not: 'a promise' };
});

Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(1);
expect(notebook.failures).to.equal(1);
expect(notebook.tests[0].err.toString()).to.contain('Function for "a" should either take a callback argument or return a promise');
done();
});
});

it('should fail test that returns a rejected promise', (done) => {

const script = Lab.script({ schedule: false });

script.test('a', () => {

return Promise.reject(new Error('A reason why this test failed'));
});

Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(1);
expect(notebook.failures).to.equal(1);
expect(notebook.tests[0].err.toString()).to.contain('A reason why this test failed');
done();
});
});

it('should fail test that calls the callback with an error', (done) => {

const script = Lab.script({ schedule: false });

script.test('a', (done) => {

done(new Error('A reason why this test failed'));
});

Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(1);
expect(notebook.failures).to.equal(1);
expect(notebook.tests[0].err.toString()).to.contain('A reason why this test failed');
done();
});
});

it('should not fail test that returns a resolved promise', (done) => {

const script = Lab.script({ schedule: false });

script.test('a', () => {

return Promise.resolve();
});

Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(1);
expect(notebook.failures).to.equal(0);
done();
});
});

it('should not fail test that calls the callback without an error', (done) => {

const script = Lab.script({ schedule: false });

script.test('a', (done) => {

done();
});

Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(1);
expect(notebook.failures).to.equal(0);
done();
});
});

['before', 'beforeEach', 'after', 'afterEach'].forEach((fnName) => {

it(`should fail "${fnName}" that neither takes a callback nor returns anything`, (done) => {

const script = Lab.script({ schedule: false });
script.describe('a test group', () => {

script.test('a', (done) => done());

script[fnName](() => {});
});


Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(1);
expect(notebook.errors[0].message).to.contain('should either take a callback argument or return a promise');
done();
});
});

it(`should fail "${fnName}" that neither takes a callback nor returns a valid promise`, (done) => {

const script = Lab.script({ schedule: false });
script.describe('a test group', () => {

script.test('a', (done) => done());

script[fnName](() => {

return { not: 'a promise' };
});
});


Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.errors[0].message).to.contain('should either take a callback argument or return a promise');
done();
});
});

it(`should fail "${fnName}" that returns a rejected promise`, (done) => {

const script = Lab.script({ schedule: false });
script.describe('a test group', () => {

script.test('a', (done) => done());

script[fnName](() => {

return Promise.reject(new Error('A reason why this test failed'));
});
});


Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.errors[0].message).to.contain('A reason why this test failed');
done();
});
});

it(`should fail "${fnName}" that calls the callback with an error`, (done) => {

const script = Lab.script({ schedule: false });
script.describe('a test group', () => {

script.test('a', (done) => done());

script[fnName]((done) => {

done(new Error('A reason why this test failed'));
});
});


Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.errors[0].message).to.contain('A reason why this test failed');
done();
});
});

it(`should not fail "${fnName}" that returns a resolved promise`, (done) => {

const script = Lab.script({ schedule: false });
script.describe('a test group', () => {

script.test('a', (done) => done());

script[fnName](() => {

return Promise.resolve();
});
});

Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(1);
expect(notebook.failures).to.equal(0);
expect(notebook.errors.length).to.equal(0);
done();
});
});

it(`should not fail "${fnName}" calls the callback without an error`, (done) => {

const script = Lab.script({ schedule: false });
script.describe('a test group', () => {

script.test('a', (done) => done());

script[fnName]((done) => {

done();
});
});

Lab.execute(script, {}, null, (err, notebook) => {

expect(err).not.to.exist();
expect(notebook.tests).to.have.length(1);
expect(notebook.failures).to.equal(0);
expect(notebook.errors.length).to.equal(0);
done();
});
});
});

it('filters on ids', (done) => {

const script = Lab.script();
@@ -577,7 +821,7 @@ describe('Runner', () => {
Lab.execute(script, null, null, (err, notebook) => {

expect(err).to.not.exist();
expect(notebook.tests[0].err).to.equal('\'before\' action failed');
expect(notebook.tests[0].err).to.contain('\'before\' action failed');
expect(steps).to.deep.equal(['before']);
done();
});
@@ -611,7 +855,7 @@ describe('Runner', () => {
Lab.execute(script, null, null, (err, notebook) => {

expect(err).to.not.exist();
expect(notebook.tests[0].err).to.equal('\'before each\' action failed');
expect(notebook.tests[0].err).to.contain('\'before each\' action failed');
expect(steps).to.deep.equal(['before']);
done();
});

0 comments on commit 83ab77d

Please sign in to comment.
You can’t perform that action at this time.