Skip to content

Commit a3cebc9

Browse files
authored
Added throws assertion (#12)
1 parent 014729b commit a3cebc9

File tree

4 files changed

+92
-4
lines changed

4 files changed

+92
-4
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Assert.uuid('3e9009a0-4b2f-414e-bf02-ec0df56fc864');
7777
Assert.hasElement('#div', window.document);
7878
Assert.hasAttribute('data-test', window.document.querySelector('#test'));
7979
Assert.hasAttributes(['data-test', 'id'], window.document.querySelector('#test'));
80+
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));
8081
```
8182

8283
---
@@ -415,6 +416,23 @@ let dom = new JSDOM(`<body><div id="div" data-test></div></body>`);
415416
Assert.hasAttributes(['data-test','id'], dom.window.document.querySelector('#div'));
416417
```
417418

419+
---
420+
421+
```js
422+
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));
423+
```
424+
425+
Asserts that function throws expected exception.
426+
427+
Example:
428+
429+
```js
430+
Assert.throws(() => { throw new String('expected message'); }, new String('expected message'));
431+
Assert.throws(() => { throw 'expected message'; }, 'expected message');
432+
Assert.throws(() => { throw new Error(); });
433+
Assert.throws(() => { throw new Error('some not relevant error message'); }, new Error());
434+
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));
435+
```
418436

419437
## Custom exception message
420438

@@ -432,5 +450,3 @@ you can also use variables `expected` and `received` in your messages.
432450
```js
433451
Assert.string(1234, 'Expected ${expected} but got ${received}'); // it throws Error("Expected string but got int[1234]")
434452
```
435-
436-

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "assert-js",
3-
"version": "0.21.0",
3+
"version": "0.22.0",
44
"description": "Javascript simple assertion library with no dependencies.",
55
"main": "bin/es5/assert-js.js",
66
"scripts": {
@@ -29,7 +29,7 @@
2929
"expect.js": "^0.3.1",
3030
"jsdom": "^11.6.2",
3131
"madge": "^3.2.0",
32-
"mocha": "^3.3.0",
32+
"mocha": "^5.2.0",
3333
"webpack": "^1.13.0"
3434
}
3535
}

src/AssertJS/Assert.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,38 @@ class Assert
601601
}
602602
})
603603
}
604+
605+
/**
606+
* @param {function} callback
607+
* @param {object} [expectedError]
608+
*/
609+
static throws(callback, expectedError = new Error())
610+
{
611+
this.isFunction(callback);
612+
613+
try {
614+
callback();
615+
} catch (error) {
616+
if (typeof error === 'object' && error instanceof Error && typeof expectedError === 'object' && expectedError instanceof Error) {
617+
618+
if (expectedError.message.length) {
619+
this.equal(error.message, expectedError.message, `Expected exception message "${error.message}" to be equals "${expectedError.message}" but it's not.`);
620+
}
621+
622+
return ;
623+
}
624+
625+
this.equal(error, expectedError, `Expected error of type ${ValueConverter.toString(error)} to be equals ${ValueConverter.toString(expectedError)} but it's not.`);
626+
627+
return ;
628+
}
629+
630+
throw InvalidValueException.expected(
631+
ValueConverter.toString(expectedError),
632+
null,
633+
"Expected from callback to throw an Error \"${expected}\" but it didn't."
634+
);
635+
}
604636
}
605637

606638
module.exports = Assert;

tests/AssertJS/AssertTest.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,44 @@ describe("Assert", () => {
444444

445445
expect(() => {Assert.hasAttributes(['data-foo', 'bar'], dom.window.document.querySelector('#test'))}).to.throwError(/Expected html element with attributes "data-foo, bar" but got "string\["<div id="test" data-test=""><\/div>"\]"./);
446446
});
447+
448+
it ("throws exception when callback is not throwing expected exception", () => {
449+
expect(() => {
450+
Assert.throws(() => {
451+
// do nothing
452+
}, new Error('Expected error message'));
453+
}).to.throwError(/Expected from callback to throw an Error "object\[{}\]" but it didn't\./);
454+
});
455+
456+
it ("throws exception when callback is not throwing expected exception type", () => {
457+
expect(() => {
458+
Assert.throws(() => {
459+
throw 'test';
460+
}, new Error('test'));
461+
}).to.throwError(/Expected error of type string\[\"test\"\] to be equals object\[{}\] but it\'s not\./);
462+
});
463+
464+
it ("throws exception when error message is different than expected but type matches", () => {
465+
expect(() => {
466+
Assert.throws(() => {
467+
throw new Error('unexpected message');
468+
}, new Error('expected message'));
469+
}).to.throwError(/Expected exception message "unexpected message" to be equals "expected message" but it\'s not./);
470+
});
471+
472+
it ("throws exception when error type is different than expected error type", () => {
473+
expect(() => {
474+
Assert.throws(() => {
475+
throw new String('expected message');
476+
}, new Error('expected message'));
477+
}).to.throwError(/Expected error of type String\["expected message"\] to be equals object\[{}\] but it\'s not./);
478+
});
479+
480+
it ("asserts that thrown errors are the same", () => {
481+
Assert.throws(() => { throw new String('expected message'); }, new String('expected message'));
482+
Assert.throws(() => { throw 'expected message'; }, 'expected message');
483+
Assert.throws(() => { throw new Error(); });
484+
Assert.throws(() => { throw new Error('some not relevant error message'); }, new Error());
485+
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));
486+
});
447487
});

0 commit comments

Comments
 (0)