Skip to content

Commit

Permalink
🐛 fix multiple describe to always run in series
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed Aug 20, 2018
1 parent ffa81ab commit 1b6fd04
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 43 deletions.
81 changes: 41 additions & 40 deletions src/describe.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class Describe {
after: []
};
this.tasks = [];
this.describes = [];
this.taskCount = 0;

this.beforeEach = this.beforeEach.bind(this);
Expand Down Expand Up @@ -48,54 +49,54 @@ export default class Describe {
this.evaluateBlock = resolve;
}
});
this.finish = new Promise(finish => {
this.busy = new Promise(async free => {
await this.evaluted;
this.finish = new Promise(async finish => {
await this.evaluted;
try {
await Promise.each(this.hooks.before, Promise.try);
} catch (error) {
this.opts.reporter.fail("before hooks", error);
process.emit("exit");
}

while (this.tasks.length) {
const tasks = this.tasks.slice();
this.tasks.length = 0;

if (this.opts.concurrency === 1) {
await Promise.each(tasks, ([title, task]) => task());
} else {
await Promise.map(tasks, ([title, task]) => task(), {
concurrency: this.opts.concurrency || Infinity
});
}
}
await Promise.each(this.describes, describe => describe());

setImmediate(async () => {
await this.waitForChildren();
try {
await Promise.each(this.hooks.before, Promise.try);
await Promise.each(this.hooks.after, Promise.try);
} catch (error) {
this.opts.reporter.fail("before hooks", error);
this.opts.reporter.fail("after hooks", error);
process.emit("exit");
}

while (this.tasks.length) {
const tasks = this.tasks.slice();
this.tasks.length = 0;

if (this.opts.concurrency === 1) {
await Promise.each(tasks, ([title, task]) => task());
} else {
await Promise.map(tasks, ([title, task]) => task(), {
concurrency: this.opts.concurrency || Infinity
});
}
}
free();

setImmediate(async () => {
await this.waitForChildren();
try {
await Promise.each(this.hooks.after, Promise.try);
} catch (error) {
this.opts.reporter.fail("after hooks", error);
process.emit("exit");
}
finish();
});
finish();
});
});
}
async describe(title, fn, options = {}) {
await this.busy;

const describe = this.child(title, options);
const reporter = this.opts.reporter;
describe(title, fn, options = {}) {
this.describes.push(() => {
const describe = this.child(title, options);
const reporter = this.opts.reporter;

reporter.describe(title, fn);
if (fn === undefined) {
return;
}
fn.call(describe, describe.block);

reporter.describe(title, fn);
if (fn === undefined) {
return;
}
return fn.call(describe, describe.block);
return describe.finish;
});
}
child(title, options) {
const opts = Object.assign({}, options);
Expand Down
10 changes: 8 additions & 2 deletions test/describe/multiple/expected.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ export const code = 0;
export const stdout = `
foo
bar
- bar-foo
- bar-foo
✓ bar-bar (ELAPSED ms)
✓ bar-bar (ELAPSED ms)
✓ bar-bar (ELAPSED ms)
✓ bar-baz (ELAPSED ms)
baz
- baz-foo
- baz-foo
Expand All @@ -10,8 +16,8 @@ export const stdout = `
✓ baz-bar (ELAPSED ms)
✓ baz-baz (ELAPSED ms)
4 passing (ELAPSED ms)
2 pending
8 passing (ELAPSED ms)
4 pending
`;

export const stderr = ``;
12 changes: 11 additions & 1 deletion test/describe/multiple/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@ import { describe } from "eastern";
import delay from "delay";

describe("foo");
describe("bar");
describe("bar", it => {
it("bar-foo");
it.skip("bar-foo");
it("bar-baz", async () => {
await delay(15);
});

it("bar-bar", () => {});
it("bar-bar", () => {});
it("bar-bar", () => {});
});
describe("baz", it => {
it("baz-foo");
it.skip("baz-foo");
Expand Down

0 comments on commit 1b6fd04

Please sign in to comment.