From d36e52478bf95e9d8727451b8c9f89aa69724977 Mon Sep 17 00:00:00 2001 From: Rohan Hart Date: Tue, 26 Feb 2019 11:08:33 +1300 Subject: [PATCH 1/3] typescript may not mark async functions correctly so just await everything --- lib/interfaces/gherkin.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/interfaces/gherkin.js b/lib/interfaces/gherkin.js index 118b8a7a1..f96b73a8d 100644 --- a/lib/interfaces/gherkin.js +++ b/lib/interfaces/gherkin.js @@ -43,11 +43,7 @@ module.exports = (text) => { } event.dispatcher.on(event.step.before, setMetaStep); try { - if (isAsyncFunction(fn)) { - await fn(...fn.params); - } else { - fn(...fn.params); - } + await fn(...fn.params); } finally { event.dispatcher.removeListener(event.step.before, setMetaStep); } From 4209213c8ff64512954c83ae2d717232a5714c16 Mon Sep 17 00:00:00 2001 From: Rohan Hart Date: Fri, 1 Mar 2019 13:34:13 +1300 Subject: [PATCH 2/3] scenario tests are always asynchronous --- lib/interfaces/gherkin.js | 4 ---- lib/scenario.js | 2 ++ lib/ui.js | 2 -- test/unit/scenario_test.js | 2 +- 4 files changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/interfaces/gherkin.js b/lib/interfaces/gherkin.js index f96b73a8d..53b7fcdd1 100644 --- a/lib/interfaces/gherkin.js +++ b/lib/interfaces/gherkin.js @@ -75,8 +75,6 @@ module.exports = (text) => { const title = `${child.name} ${JSON.stringify(current)} ${tags.join(' ')}`.trim(); const test = new Test(title, async () => runSteps(exampleSteps)); test.tags = suite.tags.concat(tags); - test.timeout(0); - test.async = true; suite.addTest(scenario.test(test)); } } @@ -85,9 +83,7 @@ module.exports = (text) => { const tags = child.tags.map(t => t.name); const title = `${child.name} ${tags.join(' ')}`.trim(); const test = new Test(title, async () => runSteps(child.steps)); - test.timeout(0); test.tags = suite.tags.concat(tags); - test.async = true; suite.addTest(scenario.test(test)); } diff --git a/lib/scenario.js b/lib/scenario.js index 20ed0b726..337b3fa5e 100644 --- a/lib/scenario.js +++ b/lib/scenario.js @@ -28,6 +28,8 @@ module.exports.test = (test) => { } test.steps = []; + test.timeout(0); + test.async = true; test.fn = function (done) { recorder.errHandler((err) => { diff --git a/lib/ui.js b/lib/ui.js index 2a2631909..6b8e36319 100644 --- a/lib/ui.js +++ b/lib/ui.js @@ -54,8 +54,6 @@ module.exports = function (suite) { test.tags = (suite.tags || []).concat(title.match(/(\@[a-zA-Z0-9-_]+)/g)); // match tags from title test.file = file; - test.async = true; - test.timeout(0); if (!test.inject) { test.inject = {}; } diff --git a/test/unit/scenario_test.js b/test/unit/scenario_test.js index 58fa4258c..a15758a4e 100644 --- a/test/unit/scenario_test.js +++ b/test/unit/scenario_test.js @@ -15,7 +15,7 @@ let started; describe('Scenario', () => { beforeEach(() => { - test = {}; + test = { timeout: () => { } }; fn = sinon.spy(); test.fn = fn; }); From 7b72e9f46ea2113a16fd9b4478d07183c6a6eb5e Mon Sep 17 00:00:00 2001 From: Rohan Hart Date: Fri, 1 Mar 2019 13:36:52 +1300 Subject: [PATCH 3/3] need to wait for the tested test to complete before asserting --- test/unit/bdd_test.js | 96 ++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 42 deletions(-) diff --git a/test/unit/bdd_test.js b/test/unit/bdd_test.js index 4282829b1..4388d8c67 100644 --- a/test/unit/bdd_test.js +++ b/test/unit/bdd_test.js @@ -69,15 +69,17 @@ describe('BDD', () => { }); - it('should load step definitions', async () => { + it('should load step definitions', (done) => { let sum = 0; Given(/I have product with (\d+) price/, param => sum += parseInt(param, 10)); When('I go to checkout process', () => sum += 10); const suite = run(text); assert.equal('checkout process', suite.title); - suite.tests[0].fn(() => {}); - assert.ok(suite.tests[0].steps); - assert.equal(1610, sum); + suite.tests[0].fn(() => { + assert.ok(suite.tests[0].steps); + assert.equal(1610, sum); + done(); + }); }); @@ -95,24 +97,26 @@ describe('BDD', () => { }); }); - it('should work with async functions', async () => { + it('should work with async functions', (done) => { let sum = 0; Given(/I have product with (\d+) price/, param => sum += parseInt(param, 10)); When('I go to checkout process', async () => { - return new Promise((done) => { + return new Promise((checkoutDone) => { sum += 10; - setTimeout(done, 0); + setTimeout(checkoutDone, 0); }); }); const suite = run(text); assert.equal('checkout process', suite.title); - await suite.tests[0].fn(() => {}); - assert.ok(suite.tests[0].steps); - assert.equal(1610, sum); + suite.tests[0].fn(() => { + assert.ok(suite.tests[0].steps); + assert.equal(1610, sum); + done(); + }); }); - it('should execute scenarios step-by-step ', () => { + it('should execute scenarios step-by-step ', (done) => { printed = []; container.append({ helpers: { @@ -132,27 +136,29 @@ describe('BDD', () => { I.do('add finish checkout'); }); const suite = run(text); - suite.tests[0].fn(() => {}); - return recorder.promise().then(() => { - printed.should.include.members([ - 'add 600', - 'add 1600', - 'add finish checkout', - ]); - const lines = recorder.scheduled().split('\n'); - lines.should.include.members([ - 'do: "add", 600', - 'step passed', - 'return result', - 'do: "add", 1600', - 'step passed', - 'return result', - 'do: "add finish checkout"', - 'step passed', - 'return result', - 'fire test.passed', - 'finish test', - ]); + suite.tests[0].fn(() => { + recorder.promise().then(() => { + printed.should.include.members([ + 'add 600', + 'add 1600', + 'add finish checkout', + ]); + const lines = recorder.scheduled().split('\n'); + lines.should.include.members([ + 'do: "add", 600', + 'step passed', + 'return result', + 'do: "add", 1600', + 'step passed', + 'return result', + 'do: "add finish checkout"', + 'step passed', + 'return result', + 'fire test.passed', + 'finish test', + ]); + done(); + }); }); }); @@ -192,13 +198,13 @@ describe('BDD', () => { Given('I am logged in as customer', () => sum++); Then('I am shopping', () => sum++); const suite = run(text); - const done = () => {}; + const done = () => { }; suite._beforeEach.forEach(hook => hook.run(done)); suite.tests[0].fn(done); assert.equal(2, sum); }); - it('should execute scenario outlines', () => { + it('should execute scenario outlines', (done) => { const text = ` @awesome @cool Feature: checkout process @@ -215,7 +221,6 @@ describe('BDD', () => { | 20 | 18 | `; let cart = 0; - let executed = 0; let sum = 0; Given('I have product with price {int}$ in my cart', (price) => { cart = price; @@ -223,20 +228,27 @@ describe('BDD', () => { Given('discount is {int} %', (discount) => { cart -= cart * discount / 100; }); - Then('I should see price is {string} $', async (total) => { - executed++; + Then('I should see price is {string} $', (total) => { sum = parseInt(total, 10); }); const suite = run(text); - const done = () => {}; - suite.tests.forEach(t => t.fn(done)); + assert.ok(suite.tests[0].tags); assert.equal('@awesome', suite.tests[0].tags[0]); assert.equal('@cool', suite.tests[0].tags[1]); assert.equal('@super', suite.tests[0].tags[2]); - assert.equal(executed, 2); - assert.equal(18, cart); - assert.equal(18, sum); + + assert.equal(2, suite.tests.length); + suite.tests[0].fn(() => { + assert.equal(9, cart); + assert.equal(9, sum); + + suite.tests[1].fn(() => { + assert.equal(18, cart); + assert.equal(18, sum); + done(); + }); + }); }); });