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

emit more events #1395

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add error event test
  • Loading branch information
tinovyatkin committed Oct 17, 2019
commit 2d181ddbb62e7b0f13cc51716ca67da8d7982a00
27 changes: 27 additions & 0 deletions test/application/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,31 @@ describe('app emits events', () => {
assert.deepStrictEqual(emitted, ['request', 'fistMiddleWare', 'customEvent', 'lastMiddleware', 'respond', 'responded']);
assert.strictEqual(onceEvents, 3);
});

it('should emit error event on middleware throw', async() => {
const app = new Koa();
const emitted = [];
app.on('error', err => emitted.push(err));

app.use((ctx, next) => {
throw new TypeError('Hello Koa!');
});

const server = app.listen();

let onceEvents = 0;
app.once('error', (err, ctx) => {
assert.ok(err instanceof TypeError);
assert.strictEqual(ctx.app, app);
onceEvents++;
});

await request(server)
.get('/')
.expect(500);

assert.deepStrictEqual(emitted.length, 1);
assert.ok(emitted[0] instanceof TypeError);
assert.strictEqual(onceEvents, 1);
});
});