Skip to content

Commit 143181c

Browse files
Support React Native by adding a shim for AssertionError
Co-authored-by: Mark Wubben <[email protected]>
1 parent f5da817 commit 143181c

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

AssertionError.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('assert').AssertionError

AssertionError.native.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Enable use in React Native by shimming the Node.js AssertionError.
2+
3+
class AssertionError extends Error {
4+
constructor ({ message }) {
5+
super(message)
6+
this.name = 'AssertionError'
7+
}
8+
}
9+
10+
module.exports = AssertionError

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict'
2-
const { AssertionError } = require('assert')
2+
const AssertionError = require('./AssertionError')
33

44
function never (message = 'Unexpected call to never()') {
55
throw new AssertionError({ message, stackStartFn: never })

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@
1010
"types": "index.d.ts",
1111
"files": [
1212
"index.d.ts",
13-
"index.js"
13+
"index.js",
14+
"AssertionError.js",
15+
"AssertionError.native.js"
1416
],
1517
"scripts": {
1618
"test": "standard && tsd && nyc ava"
1719
},
1820
"keywords": [
1921
"never",
2022
"typescript",
21-
"assert"
23+
"assert",
24+
"react-native"
2225
],
2326
"author": "Mark Wubben (https://novemberborn.net/)",
2427
"license": "ISC",

test.js

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const { AssertionError } = require('assert')
33
const test = require('ava')
44
const never = require('.')
5+
const ShimmedAssertionError = require('./AssertionError.native')
56

67
test('throws when called', t => {
78
t.throws(never, { instanceOf: AssertionError })
@@ -22,3 +23,13 @@ test('never() is not in the error stack', t => {
2223
const error = t.throws(wrapper)
2324
t.regex(error.stack.split('\n')[1], /wrapper/)
2425
})
26+
27+
test('the shimmed AssertionError can be constructed', t => {
28+
const error = t.throws(() => {
29+
throw new ShimmedAssertionError({ message: 'Error message' })
30+
}, {
31+
instanceOf: ShimmedAssertionError,
32+
message: 'Error message'
33+
})
34+
t.is(error.name, 'AssertionError')
35+
})

0 commit comments

Comments
 (0)