-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.ts
52 lines (45 loc) · 1.73 KB
/
test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import assert from 'node:assert'
import test from 'node:test'
import { goTry, goTryRaw, goTryRawSync, goTrySync } from './src/index'
test(`value returned by callback is used when callback doesn't throw`, async () => {
const fn = () => 'value'
const [err1, value] = goTrySync(fn)
const [err2, value2] = goTryRawSync(fn)
assert.equal(value, 'value')
assert.equal(value2, 'value')
assert.equal(err1, undefined)
assert.equal(err2, undefined)
})
test('if callback throws, value should be undefined and err should contain the error message', async () => {
const fn = () => {
return JSON.parse('{/') as string
}
const [err1, value] = goTrySync(fn)
// add the Error type to the tuple so it's not unknown anymore
const [err2, value2] = goTryRawSync<Error>(fn)
assert.equal(value, undefined)
assert.equal(value2, undefined)
assert.equal(typeof err1, 'string')
assert.equal(typeof err2, 'object')
assert.equal(typeof err2?.message, 'string')
})
test('first parameter accepts promises and makes the function async', async () => {
const fn = Promise.resolve('value')
const [err1, value] = await goTry(fn)
const [err2, value2] = await goTryRaw(fn)
assert.equal(value, 'value')
assert.equal(value2, 'value')
assert.equal(err1, undefined)
assert.equal(err2, undefined)
})
test('if async callback throws, value should be undefined and err should contain the error message', async () => {
const fn = Promise.reject(new Error('error'))
const [err, value] = await goTry(fn)
// add the Error type to the tuple so it's not unknown anymore
const [err2, value2] = await goTryRaw<Error>(fn)
assert.equal(value, undefined)
assert.equal(value2, undefined)
assert.equal(err, 'error')
assert.equal(typeof err2, 'object')
assert.equal(err2?.message, 'error')
})