Skip to content

Commit 4c3b772

Browse files
committed
Add more tests
1 parent e8a1b5f commit 4c3b772

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
"@ehmicky/eslint-config": "^20.0.32",
6363
"@ehmicky/prettier-config": "^1.0.6",
6464
"@types/sinon": "^17.0.4",
65+
"figures": "^6.1.0",
66+
"modern-errors-beautiful": "^2.0.1",
6567
"sinon": "^20.0.0",
6668
"test-each": "^7.0.1"
6769
},

src/main.test.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import process from 'node:process'
22

33
import test from 'ava'
4+
import figures from 'figures'
45
import ModernError from 'modern-errors'
6+
import modernErrorsBeautiful from 'modern-errors-beautiful'
57
import { stub, type SinonStub } from 'sinon'
68
import { each } from 'test-each'
79

@@ -16,12 +18,16 @@ const processExit = stub(process, 'exit')
1618
// `handle-cli-error` use global variables `process.exitCode`, `process.exit()`
1719
// and `console.error()` so we need to mock them.
1820
// It also relies on timeout, which we need to mock as well.
19-
const errorExit = (errorArg: Error, options?: Options) => {
21+
const errorExit = (
22+
errorArg: Error,
23+
options?: Options,
24+
ErrorClass = BaseError,
25+
) => {
2026
try {
2127
consoleError.resetHistory()
2228
processExit.resetHistory()
2329

24-
BaseError.exit(errorArg, options)
30+
ErrorClass.exit(errorArg, options)
2531

2632
const consoleArg = getStubArg(consoleError)
2733
return { consoleArg, exitCode: process.exitCode }
@@ -42,6 +48,11 @@ const BaseError = ModernError.subclass('BaseError', {
4248
})
4349
const error = new BaseError(message)
4450

51+
const BothError = ModernError.subclass('BothError', {
52+
plugins: [modernErrorsCli, modernErrorsBeautiful],
53+
cli: { timeout: 0 },
54+
})
55+
4556
each(
4657
[
4758
true,
@@ -78,8 +89,7 @@ test.serial('"exitCode" defaults to 1', (t) => {
7889
})
7990

8091
test.serial('Can pass "stack"', (t) => {
81-
const { consoleArg } = errorExit(error, { stack: false })
82-
t.false(consoleArg.includes('at '))
92+
t.false(errorExit(error, { stack: false }).consoleArg.includes('at '))
8393
})
8494

8595
test.serial('"stack" defaults to true', (t) => {
@@ -89,3 +99,17 @@ test.serial('"stack" defaults to true', (t) => {
8999
test.serial('Can pass any options', (t) => {
90100
t.is(errorExit(error, { silent: true }).consoleArg, '')
91101
})
102+
103+
test.serial('Can use together with modern-errors-beautiful', (t) => {
104+
const bothError = new BothError('test')
105+
t.is(
106+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
107+
bothError.beautiful({ stack: false }),
108+
`${figures.cross} BothError: test`,
109+
)
110+
t.is(
111+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
112+
errorExit(bothError, { stack: false }, BothError).consoleArg,
113+
`${figures.cross} BothError: test`,
114+
)
115+
})

0 commit comments

Comments
 (0)