mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-03-06 14:28:45 -06:00
Creator & PromptModuleAPI tests
This commit is contained in:
@@ -47,6 +47,15 @@ exports.clearConsole = title => {
|
||||
}
|
||||
}
|
||||
|
||||
// make all logs except error noop during tests
|
||||
if (process.env.VUE_CLI_TEST) {
|
||||
Object.keys(exports).forEach(key => {
|
||||
if (key !== 'error') {
|
||||
exports[key] = () => {}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
exports.hasYarn = (() => {
|
||||
if (process.env.VUE_CLI_TEST) {
|
||||
return true
|
||||
|
||||
47
packages/@vue/cli/lib/__mocks__/inquirer.js
Normal file
47
packages/@vue/cli/lib/__mocks__/inquirer.js
Normal file
@@ -0,0 +1,47 @@
|
||||
let pendingAssertions
|
||||
|
||||
exports.prompt = prompts => {
|
||||
if (!pendingAssertions) {
|
||||
throw new Error(`inquirer was mocked and used without pending assertions: ${prompts}`)
|
||||
}
|
||||
|
||||
const answers = {}
|
||||
let skipped = 0
|
||||
prompts.forEach((prompt, i) => {
|
||||
if (prompt.when && !prompt.when(answers)) {
|
||||
skipped++
|
||||
return
|
||||
}
|
||||
const a = pendingAssertions[i - skipped]
|
||||
if (a.message) {
|
||||
expect(prompt.message).toContain(a.message)
|
||||
}
|
||||
if (a.choices) {
|
||||
expect(prompt.choices.length).toBe(a.choices.length)
|
||||
a.choices.forEach((c, i) => {
|
||||
expect(prompt.choices[i].name).toContain(a.choices[i])
|
||||
})
|
||||
}
|
||||
if (a.choose != null) {
|
||||
expect(prompt.type).toBe('list')
|
||||
answers[prompt.name] = prompt.choices[a.choose].value
|
||||
}
|
||||
if (a.check != null) {
|
||||
expect(prompt.type).toBe('checkbox')
|
||||
answers[prompt.name] = a.check.map(i => prompt.choices[i].value)
|
||||
}
|
||||
if (a.confirm != null) {
|
||||
expect(prompt.type).toBe('confirm')
|
||||
answers[prompt.name] = a.confirm
|
||||
}
|
||||
})
|
||||
|
||||
expect(prompts.length).toBe(pendingAssertions.length + skipped)
|
||||
pendingAssertions = null
|
||||
|
||||
return Promise.resolve(answers)
|
||||
}
|
||||
|
||||
exports.expectPrompts = assertions => {
|
||||
pendingAssertions = assertions
|
||||
}
|
||||
112
packages/@vue/cli/lib/__tests__/CreatorPrompts.test.js
Normal file
112
packages/@vue/cli/lib/__tests__/CreatorPrompts.test.js
Normal file
@@ -0,0 +1,112 @@
|
||||
jest.mock('fs')
|
||||
jest.mock('inquirer')
|
||||
|
||||
const Creator = require('../Creator')
|
||||
const { defaults } = require('../options')
|
||||
const { expectPrompts } = require('inquirer') // from mock
|
||||
|
||||
it('default', async () => {
|
||||
const creator = new Creator('test', '/', [])
|
||||
|
||||
expectPrompts([
|
||||
{
|
||||
message: 'project creation mode',
|
||||
choices: [
|
||||
'Zero-config',
|
||||
'Manually select'
|
||||
],
|
||||
choose: 0
|
||||
}
|
||||
])
|
||||
|
||||
const options = await creator.promptAndResolveOptions()
|
||||
expect(options).toEqual(defaults)
|
||||
})
|
||||
|
||||
it('manual + PromptModuleAPI', async () => {
|
||||
const creator = new Creator('test', '/', [
|
||||
api => {
|
||||
api.injectFeature({
|
||||
name: 'Foo',
|
||||
value: 'foo'
|
||||
})
|
||||
api.injectFeature({
|
||||
name: 'Bar',
|
||||
value: 'bar'
|
||||
})
|
||||
api.injectPrompt({
|
||||
name: 'customFoo',
|
||||
message: 'customFoo',
|
||||
when: answers => answers.features.includes('foo'),
|
||||
type: 'confirm'
|
||||
})
|
||||
api.injectPrompt({
|
||||
name: 'customBar',
|
||||
message: 'customBar',
|
||||
when: answers => answers.features.includes('bar'),
|
||||
type: 'list',
|
||||
choices: []
|
||||
})
|
||||
api.injectOptionForPrompt('customBar', {
|
||||
name: 'barChoice',
|
||||
value: 'barChoice'
|
||||
})
|
||||
api.onPromptComplete((answers, options) => {
|
||||
if (answers.features.includes('bar')) {
|
||||
options.plugins.bar = {}
|
||||
}
|
||||
if (answers.customBar === 'barChoice') {
|
||||
options.plugins.barChoice = {}
|
||||
}
|
||||
})
|
||||
}
|
||||
])
|
||||
|
||||
expectPrompts([
|
||||
{ choose: 1 },
|
||||
{
|
||||
message: 'Check the features',
|
||||
choices: ['Foo', 'Bar'],
|
||||
check: [1]
|
||||
},
|
||||
{
|
||||
message: 'customBar',
|
||||
choices: ['barChoice'],
|
||||
choose: 0
|
||||
},
|
||||
{
|
||||
message: 'package manager',
|
||||
choices: ['Yarn', 'NPM'],
|
||||
choose: 0
|
||||
},
|
||||
{
|
||||
message: 'Save the preferences',
|
||||
confirm: true
|
||||
}
|
||||
])
|
||||
|
||||
const options = await creator.promptAndResolveOptions()
|
||||
const expectedOptions = {
|
||||
packageManager: 'yarn',
|
||||
plugins: {
|
||||
bar: {},
|
||||
barChoice: {}
|
||||
}
|
||||
}
|
||||
expect(options).toEqual(expectedOptions)
|
||||
|
||||
// should be saved now
|
||||
expectPrompts([
|
||||
{
|
||||
choices: [
|
||||
'Use previously saved',
|
||||
'Zero-config',
|
||||
'Manually'
|
||||
],
|
||||
choose: 0
|
||||
}
|
||||
])
|
||||
const newCreator = new Creator('test', '/', [])
|
||||
const newOptions = await newCreator.promptAndResolveOptions()
|
||||
expect(newOptions).toEqual(expectedOptions)
|
||||
})
|
||||
Reference in New Issue
Block a user