Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from t-bonatti/emit-events
Emit events on finished task / Execute function or promise
- Loading branch information
Showing
2 changed files
with
68 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { assert } = require('chai'); | ||
const sinon = require('sinon'); | ||
const Task = require('../src/task'); | ||
|
||
describe('Task', () => { | ||
beforeEach(() => { | ||
this.clock = sinon.useFakeTimers(new Date(2018, 0, 1, 0, 0, 0, 0)); | ||
}); | ||
|
||
afterEach(() => { | ||
this.clock.restore(); | ||
}); | ||
|
||
it('should emit event on finish a task', async () => { | ||
let finished = false; | ||
let task = new Task(() => 'ok'); | ||
task.on('task-finished', () => finished = true); | ||
await task.execute(); | ||
assert.equal(true, finished); | ||
}); | ||
|
||
it('should emit event on error a task', async () => { | ||
let error; | ||
let task = new Task(() => { | ||
throw Error('execution error'); | ||
}); | ||
task.on('task-failed', (err) => error = err.message); | ||
await task.execute(); | ||
assert.equal('execution error', error); | ||
}); | ||
|
||
it('should emit event on finish a promise task', async () => { | ||
let finished = false; | ||
const promise = () => new Promise((resolve) => resolve('ok')); | ||
let task = new Task(promise); | ||
task.on('task-finished', () => finished = true); | ||
await task.execute(); | ||
assert.equal(true, finished); | ||
}); | ||
|
||
it('should emit event on error a promise task', async () => { | ||
let failed = false; | ||
const promise = () => new Promise((resolve, reject) => reject('errou')); | ||
const task = new Task(promise); | ||
task.on('task-failed', (error) => failed = error); | ||
await task.execute(); | ||
assert.equal('errou', failed); | ||
}); | ||
}); |