11import process from 'node:process'
22
33import test from 'ava'
4+ import figures from 'figures'
45import ModernError from 'modern-errors'
6+ import modernErrorsBeautiful from 'modern-errors-beautiful'
57import { stub , type SinonStub } from 'sinon'
68import { 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} )
4349const error = new BaseError ( message )
4450
51+ const BothError = ModernError . subclass ( 'BothError' , {
52+ plugins : [ modernErrorsCli , modernErrorsBeautiful ] ,
53+ cli : { timeout : 0 } ,
54+ } )
55+
4556each (
4657 [
4758 true ,
@@ -78,8 +89,7 @@ test.serial('"exitCode" defaults to 1', (t) => {
7889} )
7990
8091test . 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
8595test . serial ( '"stack" defaults to true' , ( t ) => {
@@ -89,3 +99,17 @@ test.serial('"stack" defaults to true', (t) => {
8999test . 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