Skip to content

Commit

Permalink
Merge pull request #157 from AthennaIO/develop
Browse files Browse the repository at this point in the history
feat(task): add addPromise method to Task
  • Loading branch information
jlenon7 authored Oct 7, 2023
2 parents 0b6ae0b + ed4e2cb commit 5ab4002
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/artisan",
"version": "4.14.0",
"version": "4.15.0",
"description": "The Athenna CLI application. Built on top of commander and inspired in @adonisjs/ace.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
33 changes: 33 additions & 0 deletions src/helpers/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,39 @@ export class Task {
return this
}

/**
* Same as add but automatically handle the callback
* depending on the promise result.
*
* @example
* ```ts
* await this.logger
* .task()
* .addPromise('hello', async () => {
* await Exec.sleep(1000)
* })
* .run()
* ```
* Output:
* ```bash
* → hello 1005ms
* ```
*/
public addPromise(title: string, cb: () => Promise<any>): Task {
this.tasks.push({
title,
cb: async task =>
await cb()
.then(() => task.complete())
.catch(error => {
task.fail()
throw error
})
})

return this
}

/**
* Run all the tasks added.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/helpers/TaskTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { Task } from '#src/helpers/Task'
import { Test, BeforeEach, type Context } from '@athenna/test'
import { RunningTaskException } from '#src/exceptions/RunningTaskException'
import { Exec } from '@athenna/common'

export default class TaskTest {
@BeforeEach()
Expand Down Expand Up @@ -43,6 +44,45 @@ export default class TaskTest {
assert.equal(value, 'hello')
}

@Test()
public async shouldBeAbleToCreateTasksRunnerUsingPromises({ assert }: Context) {
let value = 'hello'
Config.set('logging.channels.console.driver', 'null')

await new Task()
.addPromise('Testing 1', async () => {
await Exec.sleep(100)
})
.addPromise('Testing 2', async () => {
await Exec.sleep(100)
})
.addPromise('Testing 3', async () => {
value = null
await Exec.sleep(100)
})
.run()

assert.equal(value, null)
}

@Test()
public async shouldThrowIfPromiseDoesNotResolveWhenUsingAddPromise({ assert }: Context) {
Config.set('logging.channels.console.driver', 'null')

const task = new Task()
.addPromise('Testing 1', async () => {
await Exec.sleep(100)
})
.addPromise('Testing 2', async () => {
await Exec.sleep(100)
})
.addPromise('Testing 3', async () => {
throw new Error('Error')
})

await assert.rejects(() => task.run(), Error)
}

@Test()
public async shouldThrowRunningTaskExceptionIfExecutingCallbackAfterEndingTheTaskWithFail({ assert }: Context) {
Config.set('logging.channels.console.driver', 'null')
Expand Down

0 comments on commit 5ab4002

Please sign in to comment.