Skip to content

Commit

Permalink
return context object as result - fixes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Nov 1, 2016
1 parent 8058102 commit 72ee8cc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ class Listr {
return tasks
.then(() => {
this._renderer.end();

return context;
})
.catch(err => {
err.context = context;
this._renderer.end(err);
throw err;
});
Expand Down
5 changes: 4 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ const tasks = new Listr([

tasks.run({
foo: 'bar'
}).then(ctx => {
console.log(ctx);
//=> {foo: 'bar', unicorn: 'rainbow'}
});
```

Expand Down Expand Up @@ -322,7 +325,7 @@ Task object or multiple task objects.

#### run([context])

Start executing the tasks.
Start executing the tasks. Returns a `Promise` for the context object.

##### context

Expand Down
38 changes: 36 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ test('context', async t => {
}
]);

await list.run();
const result = await list.run();

t.deepEqual(result, {
foo: 'bar'
});
});

test('subtask context', async t => {
Expand All @@ -155,5 +159,35 @@ test('subtask context', async t => {
}
]);

await list.run({foo: 'bar'});
const result = await list.run({foo: 'bar'});

t.deepEqual(result, {
foo: 'bar',
fiz: 'biz'
});
});

test('context is attached to error object', async t => {
const list = new Listr([
{
title: 'foo',
task: context => {
context.foo = 'bar';
}
},
{
title: 'unicorn',
task: () => Promise.reject(new Error('foo bar'))
}
]);

try {
await list.run();
t.fail('Should throw error');
} catch (err) {
t.is(err.message, 'foo bar');
t.deepEqual(err.context, {
foo: 'bar'
});
}
});

0 comments on commit 72ee8cc

Please sign in to comment.