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

feat: support async hooks #390

Merged
merged 1 commit into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/config/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const HOOK_STAGES = [

function promisifyHooks (hooks) {
Object.keys(hooks).forEach((key) => {
hooks[key] = promisify(hooks[key])
if (hooks[key].length) {
// hook takes args, is expecting a callback so promisify it
hooks[key] = promisify(hooks[key])
}
})

return hooks
Expand Down
57 changes: 57 additions & 0 deletions test/config/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,60 @@ describe('config - user', () => {
})
})
})

describe('config - user with async hooks', () => {
let config

before(() => {
mock('../../src/utils', {
getPkg () {
return Promise.resolve({
name: 'example'
})
},
getUserConfig () {
return {
webpack: {
devtool: 'eval'
},
entry: 'src/main.js',
hooks: {
async pre () {
await Promise.resolve()

return 'pre done async'
},
async post () {
await Promise.resolve()

return 'post done async'
}
}
}
},
getLibraryName () {
return 'Example'
},
getPathToDist () {
return 'dist'
},
getPathToNodeModules () {
return 'aegir/node_modules'
},
fromRoot () {
return './src/index.js'
}
})

config = mock.reRequire('../../src/config/user')()
})

after(() => {
mock.stop('../../src/utils.js')
})

it('supports async hooks', async () => {
expect(await config.hooks.browser.pre()).to.eql('pre done async')
expect(await config.hooks.browser.post()).to.eql('post done async')
})
})