Skip to content

Commit

Permalink
Added throws assertion (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech authored Dec 20, 2018
1 parent 014729b commit a3cebc9
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Assert.uuid('3e9009a0-4b2f-414e-bf02-ec0df56fc864');
Assert.hasElement('#div', window.document);
Assert.hasAttribute('data-test', window.document.querySelector('#test'));
Assert.hasAttributes(['data-test', 'id'], window.document.querySelector('#test'));
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));
```

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

---

```js
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));
```

Asserts that function throws expected exception.

Example:

```js
Assert.throws(() => { throw new String('expected message'); }, new String('expected message'));
Assert.throws(() => { throw 'expected message'; }, 'expected message');
Assert.throws(() => { throw new Error(); });
Assert.throws(() => { throw new Error('some not relevant error message'); }, new Error());
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));
```

## Custom exception message

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


4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "assert-js",
"version": "0.21.0",
"version": "0.22.0",
"description": "Javascript simple assertion library with no dependencies.",
"main": "bin/es5/assert-js.js",
"scripts": {
Expand Down Expand Up @@ -29,7 +29,7 @@
"expect.js": "^0.3.1",
"jsdom": "^11.6.2",
"madge": "^3.2.0",
"mocha": "^3.3.0",
"mocha": "^5.2.0",
"webpack": "^1.13.0"
}
}
32 changes: 32 additions & 0 deletions src/AssertJS/Assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,38 @@ class Assert
}
})
}

/**
* @param {function} callback
* @param {object} [expectedError]
*/
static throws(callback, expectedError = new Error())
{
this.isFunction(callback);

try {
callback();
} catch (error) {
if (typeof error === 'object' && error instanceof Error && typeof expectedError === 'object' && expectedError instanceof Error) {

if (expectedError.message.length) {
this.equal(error.message, expectedError.message, `Expected exception message "${error.message}" to be equals "${expectedError.message}" but it's not.`);
}

return ;
}

this.equal(error, expectedError, `Expected error of type ${ValueConverter.toString(error)} to be equals ${ValueConverter.toString(expectedError)} but it's not.`);

return ;
}

throw InvalidValueException.expected(
ValueConverter.toString(expectedError),
null,
"Expected from callback to throw an Error \"${expected}\" but it didn't."
);
}
}

module.exports = Assert;
40 changes: 40 additions & 0 deletions tests/AssertJS/AssertTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,44 @@ describe("Assert", () => {

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>"\]"./);
});

it ("throws exception when callback is not throwing expected exception", () => {
expect(() => {
Assert.throws(() => {
// do nothing
}, new Error('Expected error message'));
}).to.throwError(/Expected from callback to throw an Error "object\[{}\]" but it didn't\./);
});

it ("throws exception when callback is not throwing expected exception type", () => {
expect(() => {
Assert.throws(() => {
throw 'test';
}, new Error('test'));
}).to.throwError(/Expected error of type string\[\"test\"\] to be equals object\[{}\] but it\'s not\./);
});

it ("throws exception when error message is different than expected but type matches", () => {
expect(() => {
Assert.throws(() => {
throw new Error('unexpected message');
}, new Error('expected message'));
}).to.throwError(/Expected exception message "unexpected message" to be equals "expected message" but it\'s not./);
});

it ("throws exception when error type is different than expected error type", () => {
expect(() => {
Assert.throws(() => {
throw new String('expected message');
}, new Error('expected message'));
}).to.throwError(/Expected error of type String\["expected message"\] to be equals object\[{}\] but it\'s not./);
});

it ("asserts that thrown errors are the same", () => {
Assert.throws(() => { throw new String('expected message'); }, new String('expected message'));
Assert.throws(() => { throw 'expected message'; }, 'expected message');
Assert.throws(() => { throw new Error(); });
Assert.throws(() => { throw new Error('some not relevant error message'); }, new Error());
Assert.throws(() => { throw new Error('some relevant error message'); }, new Error('some relevant error message'));
});
});

0 comments on commit a3cebc9

Please sign in to comment.