Skip to content

Commit a4baea9

Browse files
committed
Fix tests
1 parent f0c8592 commit a4baea9

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

test/main.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import test from 'ava'
22
import setErrorStack from 'set-error-stack'
33
import { each } from 'test-each'
44

5+
const setNewStack = function (error, prefix = '') {
6+
setErrorStack(error, error.stack.replace('Error: one', `${prefix}Error: two`))
7+
}
8+
59
each([null, true, undefined], ({ title }, stack) => {
610
test(`Validate stack | ${title}`, (t) => {
711
t.throws(setErrorStack.bind(undefined, new Error('one'), stack))
@@ -28,7 +32,7 @@ test('Sets error stack', (t) => {
2832

2933
test('Sets error message', (t) => {
3034
const error = new Error('one')
31-
setErrorStack(error, error.stack.replace('one', 'two'))
35+
setNewStack(error)
3236
t.is(error.message, 'two')
3337
t.false(Object.getOwnPropertyDescriptor(error, 'message').enumerable)
3438
})
@@ -43,31 +47,32 @@ test('Does not set error message if the stack has not changed', (t) => {
4347
test('Does not set error message if the old stack does not contain it', (t) => {
4448
const error = new Error('one')
4549
error.message = 'other'
46-
setErrorStack(error, error.stack.replace('one', 'two'))
50+
setNewStack(error)
4751
t.is(error.message, 'other')
4852
})
4953

5054
test('Does not set error message if the old stack is invalid', (t) => {
5155
const error = new Error('one')
5256
error.stack = error.message
53-
setErrorStack(error, error.stack.replace('one', 'two'))
57+
setNewStack(error)
5458
t.is(error.message, 'one')
5559
})
5660

5761
test('Sets error message even if stack includes message but does not start with it', (t) => {
5862
const error = new Error('one')
59-
setErrorStack(error, `prefix\n${error.stack.replace('one', 'two')}`)
63+
setNewStack(error, 'prefix\n')
6064
t.is(error.message, 'two')
6165
})
6266

6367
test('Does not set error message if stack includes message but does not start with it without a newline', (t) => {
6468
const error = new Error('one')
65-
setErrorStack(error, `prefix: ${error.stack.replace('one', 'two')}`)
69+
setNewStack(error, 'prefix: ')
6670
t.is(error.message, 'one')
6771
})
6872

6973
test('Does not set error message if the stack is missing a V8 first line', (t) => {
7074
const error = new Error('one')
75+
error.stack = 'Error:'
7176
setErrorStack(error, error.stack.replace('Error: one', 'two'))
7277
t.is(error.message, 'one')
7378
})

0 commit comments

Comments
 (0)