Skip to content

Commit

Permalink
WIP: end to end test utility
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleytodd committed Jan 3, 2024
1 parent 1ed108f commit 1036e11
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
20 changes: 11 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ suite(pkg.name, () => {
})

test('collect basic input', async () => {
const opts = opta({
options: {
foo: true,
bar: true
const values = await utils.test.e2e({
opta,
optaOpts: {
options: {
foo: true,
bar: true
}
},
promptModule: utils.test.promptModule({
promptModuleOpts: {
assertCount: 1,
prompts: {
foo: {
Expand All @@ -46,12 +49,11 @@ suite(pkg.name, () => {
}
}
}
})
},
cliArgs: ['--foo', 'foo'],
values: { baz: 'baz' }
})

opts.cli()(['--foo', 'foo'])
await opts.prompt()()
const values = opts.values({ baz: 'baz' })
assert.strictEqual(values.foo, 'foo')
assert.strictEqual(values.bar, 'bar')
assert.strictEqual(values.baz, 'baz')
Expand Down
40 changes: 40 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
'use strict'
const assert = require('assert')

/**
* Helper for testing E2E workflow
*/
async function e2e (opts = {}) {
const opta = opts.opta || require('./index')
const optaOpts = opts.optaOpts || {}
const promptModuleOpts = opts.promptModuleOpts
const shouldCli = typeof opts.shouldCli !== 'undefined' ? opts.shouldCli : true
const cliOpts = opts.cliOpts
const cliArgs = opts.cliArgs
const shouldPrompt = typeof opts.shouldPrompt !== 'undefined' ? opts.shouldPrompt : true
const promptOpts = opts.promptOpts
const promptGroups = opts.promptGroups
const defaults = opts.defaults
const overrides = opts.overrides
const values = opts.values

// Setup prompt module
if (promptModule) {
optaOpts.promptModule = promptModule(promptModuleOpts)
}

// Do all the stuff in the typical order
const o = opta(optaOpts)
if (shouldCli !== false) {
o.cli(cliOpts)(cliArgs)
}
if (defaults) {
o.defaults(defaults)
}
if (shouldPrompt !== false) {
await o.prompt(promptGroups)(promptOpts)
}
if (overrides) {
o.overrides(overrides)
}
return o.values(values)
}

/**
* Helper for testing prompts
*
Expand Down Expand Up @@ -82,6 +121,7 @@ function promptModule (opts = {}) {
*/
module.exports = {
test: {
e2e,
promptModule
}
}

0 comments on commit 1036e11

Please sign in to comment.