Skip to content

Commit 39c963c

Browse files
committed
Re-add tests
1 parent 4b18b0d commit 39c963c

File tree

8 files changed

+295
-7
lines changed

8 files changed

+295
-7
lines changed

src/main.test.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { each } from 'test-each'
44

55
import { handleError } from './helpers/main.test.js'
66

7-
const testError = new TypeError('test')
8-
97
each(
108
[
119
{ error: undefined, expectedMessage: 'Error: undefined' },
@@ -28,8 +26,3 @@ test.serial('validateOpts() throws on invalid options', (t) => {
2826
test.serial('validateOpts() does not throw on valid options', (t) => {
2927
t.notThrows(validateOptions.bind(undefined, { silent: true }))
3028
})
31-
32-
test.serial('Does not log if "silent" is true', (t) => {
33-
const { consoleArg } = handleError(testError, { silent: true })
34-
t.is(consoleArg, undefined)
35-
})

src/print/colors.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import test from 'ava'
2+
import chalkString from 'chalk-string'
3+
import hasAnsi from 'has-ansi'
4+
import { each } from 'test-each'
5+
6+
import { handleError } from '../helpers/main.test.js'
7+
8+
const testError = new TypeError('test')
9+
const addStyles = chalkString({ colors: true })
10+
11+
each([true, false], ({ title }, colors) => {
12+
test.serial(`Add colors unless "colors" is false | ${title}`, (t) => {
13+
const { consoleArg } = handleError(testError, { colors })
14+
t.is(hasAnsi(consoleArg), colors !== false)
15+
})
16+
})
17+
18+
test.serial('"colors" defaults to TTY color support', (t) => {
19+
const { consoleArg } = handleError(testError)
20+
t.false(hasAnsi(consoleArg))
21+
})
22+
23+
each(
24+
[
25+
{ quote: '"', styles: 'bold' },
26+
{ quote: "'", styles: 'bold' },
27+
{ quote: '`', styles: 'italic' },
28+
],
29+
[true, false],
30+
({ title }, { quote, styles }, hasNewline) => {
31+
test.serial(`"colors" colorize quoted strings | ${title}`, (t) => {
32+
const newline = hasNewline ? '\n' : ''
33+
const error = new Error(
34+
`a ${quote}b${newline}${quote} c ${quote}d${quote}`,
35+
)
36+
const { consoleArg } = handleError(error, { colors: true })
37+
t.not(
38+
consoleArg.includes(
39+
`a ${quote}${addStyles(
40+
styles,
41+
'b',
42+
)}${newline}${quote} c ${quote}${addStyles(styles, 'd')}${quote}`,
43+
),
44+
hasNewline,
45+
)
46+
})
47+
},
48+
)

src/print/header.test.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import test from 'ava'
2+
import chalkString from 'chalk-string'
3+
import figures from 'figures'
4+
import { each } from 'test-each'
5+
6+
import { handleError } from '../helpers/main.test.js'
7+
8+
const addStyles = chalkString({ colors: true })
9+
const testOpts = { icon: '', colors: true }
10+
11+
each(
12+
[
13+
{ header: undefined, styles: 'red bold' },
14+
{ header: 'bold', styles: 'bold' },
15+
{ header: 'red bold', styles: 'red bold' },
16+
],
17+
({ title }, { header, styles }) => {
18+
test.serial(`"header" is applied | ${title}`, (t) => {
19+
const error = new Error('test')
20+
const { consoleArg } = handleError(error, { ...testOpts, header })
21+
t.true(consoleArg.includes(addStyles(styles, `${error.name}:`)))
22+
t.pass()
23+
})
24+
},
25+
)
26+
27+
each([{ colors: false }, { header: '' }], ({ title }, opts) => {
28+
test.serial(
29+
`"header" is not applied if empty or no colors | ${title}`,
30+
(t) => {
31+
const error = new Error('test')
32+
const { consoleArg } = handleError(error, { ...testOpts, ...opts })
33+
t.true(consoleArg.includes(`${error.name}: `))
34+
t.pass()
35+
},
36+
)
37+
})
38+
39+
test.serial('"header" is not added to preview lines', (t) => {
40+
const error = new Error('test')
41+
const preview = 'preview'
42+
error.stack = `${preview}\n${error.stack}`
43+
const { consoleArg } = handleError(error, testOpts)
44+
t.true(consoleArg.startsWith(preview))
45+
})
46+
47+
test.serial('"header" works with empty messages', (t) => {
48+
const error = new Error('')
49+
const header = 'green'
50+
const { consoleArg } = handleError(error, { ...testOpts, header })
51+
t.true(consoleArg.includes(addStyles(header, error.name)))
52+
})
53+
54+
test.serial('"header" colorizes the icon', (t) => {
55+
const error = new Error('test')
56+
const header = 'green'
57+
const { consoleArg } = handleError(error, {
58+
...testOpts,
59+
icon: 'warning',
60+
header,
61+
})
62+
t.true(
63+
consoleArg.includes(addStyles(header, `${figures.warning} ${error.name}:`)),
64+
)
65+
})

src/print/icon.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import test from 'ava'
2+
import figures from 'figures'
3+
import { each } from 'test-each'
4+
5+
import { handleError } from '../helpers/main.test.js'
6+
7+
each(
8+
[
9+
{ icon: '', output: '' },
10+
{ icon: undefined, output: `${figures.cross} ` },
11+
{ icon: 'warning', output: `${figures.warning} ` },
12+
],
13+
({ title }, { icon, output }) => {
14+
test.serial(`"icon" prepends an icon | ${title}`, (t) => {
15+
const error = new Error('test')
16+
const { consoleArg } = handleError(error, { icon })
17+
t.true(consoleArg.includes(`${output}${error.name}`))
18+
})
19+
},
20+
)
21+
22+
test.serial('"icon" is not added to preview lines', (t) => {
23+
const error = new Error('test')
24+
error.stack = `preview\n${error.stack}`
25+
const { consoleArg } = handleError(error, { icon: 'warning' })
26+
t.true(consoleArg.includes(`${figures.warning} ${error.name}`))
27+
})

src/print/main.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from 'ava'
2+
3+
import { handleError } from '../helpers/main.test.js'
4+
5+
const testError = new TypeError('test')
6+
7+
test.serial('Does not log if "silent" is true', (t) => {
8+
const { consoleArg } = handleError(testError, { silent: true })
9+
t.is(consoleArg, undefined)
10+
})

src/print/pretty.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import test from 'ava'
2+
import chalkString from 'chalk-string'
3+
import { each } from 'test-each'
4+
5+
import { handleError } from '../helpers/main.test.js'
6+
7+
const addStyles = chalkString({ colors: true })
8+
9+
test.serial('"colors" does not colorize quoted strings in stack line', (t) => {
10+
const error = new Error('test')
11+
const stackLines = ' at "a"'
12+
error.stack = `Error: test\n${stackLines}`
13+
const { consoleArg } = handleError(error, { colors: true })
14+
t.true(consoleArg.endsWith(stackLines))
15+
})
16+
17+
each(['Error: ', 'Error [TypeError]: '], ({ title }, name) => {
18+
test.serial(
19+
`"colors" does not colorize quoted strings in preview lines | ${title}`,
20+
(t) => {
21+
const error = new Error('test "b"')
22+
const previewLines = '"a"'
23+
error.stack = `${previewLines}\n${name}${error.stack}`
24+
const { consoleArg } = handleError(error, { colors: true })
25+
t.true(consoleArg.startsWith(previewLines))
26+
t.true(consoleArg.includes(`"${addStyles('bold', 'b')}"`))
27+
},
28+
)
29+
})
30+
31+
test.serial(
32+
'"colors" does not colorize quoted strings without preview nor lines',
33+
(t) => {
34+
const error = new Error('test')
35+
error.stack = '"a"'
36+
const { consoleArg } = handleError(error, { colors: true })
37+
t.true(consoleArg.includes(`"${addStyles('bold', 'a')}"`))
38+
},
39+
)

src/print/props.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import test from 'ava'
2+
import { each } from 'test-each'
3+
4+
import { handleError } from '../helpers/main.test.js'
5+
6+
const propsError = new TypeError('test')
7+
// eslint-disable-next-line fp/no-mutation
8+
propsError.prop = 'propValue'
9+
10+
each(
11+
[true, false, undefined],
12+
[true, false, undefined],
13+
({ title }, stack, props) => {
14+
test.serial(`Prints properties unless "props" is false | ${title}`, (t) => {
15+
const { consoleArg } = handleError(propsError, { stack, props })
16+
t.is(consoleArg.includes(propsError.prop), props !== false)
17+
})
18+
},
19+
)

src/print/stack.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import test from 'ava'
2+
import { serialize } from 'error-serializer'
3+
import { each } from 'test-each'
4+
5+
import { handleError } from '../helpers/main.test.js'
6+
7+
const createDeepErrors = () => Array.from({ length: 5 }, createDeepError)
8+
9+
const createDeepError = (_, depth) => {
10+
const error = new TypeError('test')
11+
setDeepError(error, depth)
12+
return error
13+
}
14+
15+
const setDeepError = (error, depth) => {
16+
if (depth === 0) {
17+
return
18+
}
19+
20+
// eslint-disable-next-line fp/no-mutating-methods
21+
Object.defineProperty(error, 'cause', {
22+
value: new TypeError('test'),
23+
enumerable: false,
24+
writable: true,
25+
configurable: true,
26+
})
27+
// eslint-disable-next-line fp/no-delete
28+
delete error.stack
29+
setDeepError(error.cause, depth - 1)
30+
}
31+
32+
const recursiveError = new TypeError('test')
33+
// eslint-disable-next-line fp/no-mutation
34+
recursiveError.self = recursiveError
35+
const deepErrors = createDeepErrors()
36+
const ownNameError = new Error('test')
37+
// eslint-disable-next-line fp/no-mutation
38+
ownNameError.name = 'TypeError'
39+
const noStackError = new Error('test')
40+
// eslint-disable-next-line fp/no-mutating-methods
41+
Object.defineProperty(noStackError, 'stack', { value: noStackError.toString() })
42+
43+
each(
44+
[recursiveError, noStackError, ...deepErrors],
45+
[true, false, undefined],
46+
[true, false, undefined],
47+
// eslint-disable-next-line max-params
48+
({ title }, error, stack, props) => {
49+
test.serial(`Prints stack unless "stack" is false | ${title}`, (t) => {
50+
const { consoleArg } = handleError(error, { stack, props })
51+
t.is(
52+
consoleArg.includes('at '),
53+
stack !== false && error.stack.includes('at '),
54+
)
55+
})
56+
57+
test.serial(`Does not put the error in brackets | ${title}`, (t) => {
58+
const { consoleArg } = handleError(error, { stack, props, icon: '' })
59+
t.false(consoleArg.startsWith('['))
60+
})
61+
62+
test.serial(`Does not modify the error | ${title}`, (t) => {
63+
const errorCopy = serialize(error)
64+
handleError(error, { stack, props })
65+
t.deepEqual(serialize(error), errorCopy)
66+
})
67+
68+
test.serial(`Prints error name and message | ${title}`, (t) => {
69+
const { consoleArg } = handleError(error, { stack, props })
70+
t.true(consoleArg.includes(`${error.name}: ${error.message}`))
71+
})
72+
},
73+
)
74+
75+
each([true, false], ({ title }, stack, props) => {
76+
test.serial(`Prints error name consistently | ${title}`, (t) => {
77+
const { consoleArg } = handleError(ownNameError, { stack: false, props })
78+
t.true(consoleArg.includes(`Error [${ownNameError.name}`))
79+
})
80+
})
81+
82+
test.serial('Does not remove stacks from non-errors', (t) => {
83+
const error = new Error('test')
84+
error.prop = { stack: 'propStack' }
85+
const { consoleArg } = handleError(error, { stack: false, props: true })
86+
t.true(consoleArg.includes(error.prop.stack))
87+
})

0 commit comments

Comments
 (0)