Skip to content

Commit

Permalink
refactor: assertion messages
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 10, 2023
1 parent fe20bd3 commit ce6185e
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 39 deletions.
7 changes: 5 additions & 2 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/

import { AssertionError } from 'node:assert'
import { ObjectBuilder } from '@poppinss/utils'

import {
Expand All @@ -30,7 +31,7 @@ import { promptHiglight, promptPrefix, promptStyles } from './prompt_options.js'
*/
export abstract class BasePrompt {
traps: {
prompts: Map<string, { prompt: MockedPrompt; triggerError: Error }>
prompts: Map<string, { prompt: MockedPrompt; triggerError: AssertionError }>
verify: () => void
} = {
prompts: new Map(),
Expand Down Expand Up @@ -311,7 +312,9 @@ export abstract class BasePrompt {
* Trigger error is raised when the prompt is not triggered but
* trapped
*/
const triggerError = new Error(`Expected prompt "${message}" to get triggered`)
const triggerError = new AssertionError({
message: `Expected prompt "${message}" to get triggered`,
})
const mockedPrompt = new MockedPrompt()

this.traps.prompts.set(message, { prompt: mockedPrompt, triggerError })
Expand Down
59 changes: 47 additions & 12 deletions src/mocked_prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* file that was distributed with this source code.
*/

import { inspect } from 'node:util'
import { AssertionError } from 'node:assert'
import { PromptState } from './types.js'

export class MockedPrompt {
Expand All @@ -32,7 +34,7 @@ export class MockedPrompt {
value: string
expectsError: boolean // Does assertion expects error?
expectedErrorMessage?: string | RegExp
error: Error
error: AssertionError
}[] = []

/**
Expand Down Expand Up @@ -167,20 +169,23 @@ export class MockedPrompt {
if (result !== true) {
throw assertion.error
}

} else {
/**
* Expected the value to fail the assertion, but the
* validation method returned true
*/
} else {
if (result === true) {
throw assertion.error
}

/**
* Validation failed but the error message is different
*/
if (
typeof assertion.expectedErrorMessage === 'string' &&
result !== assertion.expectedErrorMessage
) {
assertion.error.actual = result
throw assertion.error
}

Expand Down Expand Up @@ -300,18 +305,46 @@ export class MockedPrompt {
* Expect the given value to fail the prompt validation
*/
assertFails(value: string, message?: string | RegExp): this {
const error = new Error(
message
? typeof message === 'string'
? `Expected assertion to fail with message "${message}"`
: `Expected assertion messages to match "${message}"`
: 'Expected assertion to fail'
)
if (!message) {
this.#assertions.push({
value,
expectsError: true,
expectedErrorMessage: message,
error: new AssertionError({
message: 'Expected prompt validation to fail',
stackStartFn: this.assertFails,
}),
})
return this
}

if (typeof message === 'string') {
const error = new AssertionError({
message: `Expected prompt validation message to equal ${inspect(message)}`,
expected: message,
operator: 'strictEqual',
stackStartFn: this.assertFails,
})
Object.defineProperty(error, 'showDiff', { value: true })

this.#assertions.push({
value,
expectsError: true,
expectedErrorMessage: message,
error,
})

return this
}

this.#assertions.push({
value,
expectsError: true,
expectedErrorMessage: message,
error: error,
error: new AssertionError({
message: `Expected prompt validation message to match ${inspect(message)}`,
expected: message,
}),
})

return this
Expand All @@ -321,7 +354,9 @@ export class MockedPrompt {
* Expect the given value to pass the prompt validation
*/
assertPasses(value: string): this {
const error = new Error('Expected assertion to pass, instead it failed')
const error = new AssertionError({
message: 'Expected assertion to pass, instead it failed',
})
this.#assertions.push({
value,
expectsError: false,
Expand Down
6 changes: 3 additions & 3 deletions tests/traps/autocomplete.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test.group('Prompts | autocomplete', () => {

await assert.rejects(
() => prompt.autocomplete('Select the installation client', ['npm', 'yarn']),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand All @@ -73,7 +73,7 @@ test.group('Prompts | autocomplete', () => {
return true
},
}),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand Down Expand Up @@ -105,7 +105,7 @@ test.group('Prompts | autocomplete', () => {
return 'Enter client'
},
}),
'Expected assertion to fail with message "client is required"'
`Expected prompt validation message to equal 'client is required'`
)
})

Expand Down
9 changes: 6 additions & 3 deletions tests/traps/confirmation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ test.group('Prompts | confirm', () => {
const prompt = new Prompt()
prompt.trap('Delete the file?').assertFails('').replyWith('virk')

await assert.rejects(() => prompt.confirm('Delete the file?'), 'Expected assertion to fail')
await assert.rejects(
() => prompt.confirm('Delete the file?'),
'Expected prompt validation to fail'
)
})

test('fail when expected failing assertion passes', async ({ assert }) => {
Expand All @@ -58,7 +61,7 @@ test.group('Prompts | confirm', () => {
return true
},
}),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand Down Expand Up @@ -90,7 +93,7 @@ test.group('Prompts | confirm', () => {
return 'Do not delete files'
},
}),
'Expected assertion to fail with message "You should not delete files"'
`Expected prompt validation message to equal 'You should not delete files'`
)
})

Expand Down
11 changes: 7 additions & 4 deletions tests/traps/input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ test.group('Prompts | input', () => {
const prompt = new Prompt()
prompt.trap("What's your username?").assertFails('').replyWith('virk')

await assert.rejects(() => prompt.ask("What's your username?"), 'Expected assertion to fail')
await assert.rejects(
() => prompt.ask("What's your username?"),
'Expected prompt validation to fail'
)
})

test('fail when expected failing assertion passes', async ({ assert }) => {
Expand All @@ -49,7 +52,7 @@ test.group('Prompts | input', () => {
return true
},
}),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand Down Expand Up @@ -80,7 +83,7 @@ test.group('Prompts | input', () => {
return 'Enter username'
},
}),
'Expected assertion to fail with message "Username is required"'
`Expected prompt validation message to equal 'Username is required'`
)
})

Expand Down Expand Up @@ -127,7 +130,7 @@ test.group('Prompts | input', () => {
return 'Enter username'
},
}),
'Expected assertion messages to match "/\\w+ is required/"'
'Expected prompt validation message to match /\\w+ is required/'
)
})

Expand Down
6 changes: 3 additions & 3 deletions tests/traps/list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ test.group('Prompts | list', () => {
const prompt = new Prompt()
prompt.trap('Enter tags').assertFails('').replyWith('node,js')

await assert.rejects(() => prompt.list('Enter tags'), 'Expected assertion to fail')
await assert.rejects(() => prompt.list('Enter tags'), 'Expected prompt validation to fail')
})

test('fail when expected failing assertion passes', async ({ assert }) => {
Expand All @@ -58,7 +58,7 @@ test.group('Prompts | list', () => {
return true
},
}),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand Down Expand Up @@ -89,7 +89,7 @@ test.group('Prompts | list', () => {
return 'Enter tags'
},
}),
'Expected assertion to fail with message "Tags are required"'
`Expected prompt validation message to equal 'Tags are required'`
)
})

Expand Down
6 changes: 3 additions & 3 deletions tests/traps/multi_select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ test.group('Prompts | multiselect', () => {

await assert.rejects(
() => prompt.multiple('Select the installation client', ['npm', 'yarn']),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand All @@ -88,7 +88,7 @@ test.group('Prompts | multiselect', () => {
return true
},
}),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand Down Expand Up @@ -120,7 +120,7 @@ test.group('Prompts | multiselect', () => {
return 'Enter client'
},
}),
'Expected assertion to fail with message "client is required"'
`Expected prompt validation message to equal 'client is required'`
)
})

Expand Down
9 changes: 6 additions & 3 deletions tests/traps/secure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ test.group('Prompts | secure', () => {
const prompt = new Prompt()
prompt.trap('Choose password').assertFails('').replyWith('secret')

await assert.rejects(() => prompt.secure('Choose password'), 'Expected assertion to fail')
await assert.rejects(
() => prompt.secure('Choose password'),
'Expected prompt validation to fail'
)
})

test('fail when expected failing assertion passes', async ({ assert }) => {
Expand All @@ -49,7 +52,7 @@ test.group('Prompts | secure', () => {
return true
},
}),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand Down Expand Up @@ -80,7 +83,7 @@ test.group('Prompts | secure', () => {
return 'Enter password'
},
}),
'Expected assertion to fail with message "Password is required"'
`Expected prompt validation message to equal 'Password is required'`
)
})

Expand Down
6 changes: 3 additions & 3 deletions tests/traps/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ test.group('Prompts | select', () => {

await assert.rejects(
() => prompt.choice('Select the installation client', ['npm', 'yarn']),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand All @@ -88,7 +88,7 @@ test.group('Prompts | select', () => {
return true
},
}),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand Down Expand Up @@ -120,7 +120,7 @@ test.group('Prompts | select', () => {
return 'Enter client'
},
}),
'Expected assertion to fail with message "client is required"'
`Expected prompt validation message to equal 'client is required'`
)
})

Expand Down
6 changes: 3 additions & 3 deletions tests/traps/toggle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ test.group('Prompts | toggle', () => {

await assert.rejects(
() => prompt.toggle('Delete the file?', ['Yep', 'Nope']),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand All @@ -70,7 +70,7 @@ test.group('Prompts | toggle', () => {
return true
},
}),
'Expected assertion to fail'
'Expected prompt validation to fail'
)
})

Expand Down Expand Up @@ -102,7 +102,7 @@ test.group('Prompts | toggle', () => {
return 'Do not delete files'
},
}),
'Expected assertion to fail with message "You should not delete files"'
`Expected prompt validation message to equal 'You should not delete files'`
)
})

Expand Down

0 comments on commit ce6185e

Please sign in to comment.