Skip to content

Commit a57dda3

Browse files
koddssonkeithamus
andauthored
refactor: convert package to ES Module. (#20)
* Remove component * Convert Makefile to npm scripts * Update TypeScript * Convert package to ES module * Fix import/exports after converting package to ES module Co-authored-by: Keith Cirkel <keithamus@users.noreply.github.com>
1 parent 5eb7a5f commit a57dda3

File tree

5 files changed

+70
-67
lines changed

5 files changed

+70
-67
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
55
## Installation
66

7+
### Node.js
8+
9+
`assertion-error` is available on [npm](http://npmjs.org).
10+
711
```
812
$ npm install --save assertion-error
9-
```
13+
```

index.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ function exclude () {
3939
* Primary Exports
4040
*/
4141

42-
module.exports = AssertionError;
43-
4442
/**
4543
* ### AssertionError
4644
*
@@ -52,7 +50,7 @@ module.exports = AssertionError;
5250
* @param {callee} start stack function (optional)
5351
*/
5452

55-
function AssertionError (message, _props, ssf) {
53+
export default function AssertionError (message, _props, ssf) {
5654
var extend = exclude('name', 'message', 'stack', 'constructor', 'toJSON')
5755
, props = extend(_props || {});
5856

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
"url": "git@github.com:chaijs/assertion-error.git"
1616
},
1717
"engines": {
18-
"node": "*"
18+
"node": ">=12"
1919
},
20-
"main": "./index",
20+
"type": "module",
21+
"module": "./index.js",
22+
"main": "./index.js",
2123
"scripts": {
2224
"test": "npm run test:node && npm run test:typescript",
2325
"test:node": "NODE_ENV=test node ./test/index.js",

test/index.js

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Include lib
33
*/
44

5-
global.AssertionError = require('..');
5+
import AssertionError from '../index.js'
66

77
/*!
88
* Simple test runner.
@@ -20,7 +20,7 @@ function assert (pass, msg) {
2020
if (!pass) throw new Error(msg);
2121
}
2222

23-
global.suite = function (fn) {
23+
function suite(fn) {
2424
fn(test, assert);
2525

2626
console.log('');
@@ -56,4 +56,61 @@ global.suite = function (fn) {
5656
* Load the tests
5757
*/
5858

59-
require('./test');
59+
suite(function (test, assert) {
60+
test('construction', function () {
61+
var err = new AssertionError();
62+
assert(err instanceof Error, 'instanceof Error');
63+
assert(err instanceof AssertionError, 'instanceof AssertionError');
64+
assert(err.name && err.name === 'AssertionError', 'name === "AssertionError"');
65+
});
66+
67+
test('message', function () {
68+
var err = new AssertionError('Oops.')
69+
, empty = new AssertionError();
70+
assert(err.message === 'Oops.', 'w/ err.message');
71+
assert(empty.message === 'Unspecified AssertionError', 'w/o err.message');
72+
});
73+
74+
test('stack', function() {
75+
assert(typeof new AssertionError().stack === 'string');
76+
});
77+
78+
test('custom properties', function () {
79+
var err = new AssertionError('good message', {
80+
name: 'ShouldNotExist'
81+
, hello: 'universe'
82+
, message: 'bad message'
83+
, stack: 'custom stack'
84+
});
85+
86+
assert(err.name === 'AssertionError', 'does not overwrite name');
87+
assert(err.message === 'good message', 'does not overwrite message');
88+
assert(err.hello && err.hello === 'universe', 'has custom property');
89+
90+
// some browsers don't have stack
91+
if (err.stack) {
92+
assert(err.stack && err.stack !== 'custom stack', 'does not overwrite stack');
93+
}
94+
});
95+
96+
test('.toJSON()', function () {
97+
var err = new AssertionError('some message', {
98+
hello: 'universe'
99+
, goodbye: 'known'
100+
});
101+
102+
var json = err.toJSON();
103+
104+
assert(json.name === 'AssertionError', 'json has name');
105+
assert(json.message === 'some message', 'json has message');
106+
assert(json.hello === 'universe' && json.goodbye === 'known', 'json has custom properties');
107+
108+
// some browsers don't have stack
109+
if (err.stack) {
110+
assert('string' === typeof json.stack, 'json has stack');
111+
}
112+
113+
var nostack = err.toJSON(false);
114+
assert(!nostack.stack, 'no stack on false argument');
115+
});
116+
});

test/test.js

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)