Skip to content

typescript may not mark async functions correctly so just await everything #1526

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

Merged
merged 3 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions lib/interfaces/gherkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -79,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));
}
}
Expand All @@ -89,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));
}

Expand Down
2 changes: 2 additions & 0 deletions lib/scenario.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module.exports.test = (test) => {
}

test.steps = [];
test.timeout(0);
test.async = true;

test.fn = function (done) {
recorder.errHandler((err) => {
Expand Down
2 changes: 0 additions & 2 deletions lib/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}
Expand Down
96 changes: 54 additions & 42 deletions test/unit/bdd_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});


Expand All @@ -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: {
Expand All @@ -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();
});
});
});

Expand Down Expand Up @@ -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
Expand All @@ -215,28 +221,34 @@ 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;
});
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();
});
});
});
});
2 changes: 1 addition & 1 deletion test/unit/scenario_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let started;

describe('Scenario', () => {
beforeEach(() => {
test = {};
test = { timeout: () => { } };
fn = sinon.spy();
test.fn = fn;
});
Expand Down