Skip to content

Commit 48d4a25

Browse files
committed
Merge pull request #6 from Tiliqua/instanceof-fix
Fixed instanceof assertion when expected argument is not a valid class
2 parents 9b8c4b0 + cea6071 commit 48d4a25

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

bin/JSAssert/Assert.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ var Assert = function () {
2626

2727
/**
2828
* @param {object} objectValue
29-
* @param {function} instance
29+
* @param {function} expectedInstance
3030
* @param {string} [message]
3131
*/
32-
value: function instanceOf(objectValue, instance) {
32+
value: function instanceOf(objectValue, expectedInstance) {
3333
var message = arguments.length <= 2 || arguments[2] === undefined ? "" : arguments[2];
3434

3535
this.string(message, "Custom error message passed to Assert.instanceOf needs to be a valid string.");
@@ -38,8 +38,10 @@ var Assert = function () {
3838
throw _InvalidValueException2.default.expected("object", objectValue, message);
3939
}
4040

41-
if (!(objectValue instanceof instance)) {
42-
throw _InvalidValueException2.default.expected(instance.name, objectValue, message.length ? message : "Expected instance of \"${expected}\" but got \"${received}\".");
41+
this.isFunction(expectedInstance, "Expected argument needs to be a valid class instead of that got \"${received}\". Most likely there is typo in class name or file with class is missing export declaration.");
42+
43+
if (!(objectValue instanceof expectedInstance)) {
44+
throw _InvalidValueException2.default.expected(expectedInstance.name, objectValue, message.length ? message : "Expected instance of \"${expected}\" but got \"${received}\".");
4345
}
4446
}
4547

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
class ObjectWithNoExportStub
4+
{
5+
6+
}

spec/JSAssert/AssertSpec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import Assert from '../../src/JSAssert/Assert';
22

3+
import ObjectWithNoExportStub from './../Doubles/ObjectWithNoExportStub';
4+
35
describe("Assert", () => {
46
it("compares instance of", () => {
57
Assert.instanceOf(new String("string"), String);
68
});
79

10+
it("compares instance of when object has no export", () => {
11+
expect(() => { Assert.instanceOf(new String("test"), ObjectWithNoExportStub); })
12+
.toThrow(`Expected argument needs to be a valid class instead of that got "object[{}]". Most likely there is typo in class name or file with class is missing export declaration.`);
13+
});
14+
815
it ("throws error when asserting instance of non object", () => {
916
expect(() => {Assert.instanceOf(1, String)}).toThrow('Expected object but got "int[1]".');
1017
expect(() => {Assert.instanceOf(new Number(2), String)}).toThrow('Expected instance of "String" but got "Number:int[2]".');
@@ -209,11 +216,11 @@ describe("Assert", () => {
209216
expect(() => {Assert.notEmpty("")}).toThrow('Expected not empty value but got "string[""]".');
210217
expect(() => {Assert.notEmpty("", 'custom message')}).toThrow('custom message');
211218
});
212-
219+
213220
it ("asserts json string", () => {
214221
Assert.jsonString('{"key":"value"}');
215222
});
216-
223+
217224
it ("throws error when expected json string is not valid", () => {
218225
expect(() => {Assert.jsonString('{"key":value"}')}).toThrow('Expected json string but got "string["{"key":value"}"]".');
219226
expect(() => {Assert.jsonString('{"key":value"}', "custom message")}).toThrow('custom message');

src/JSAssert/Assert.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@ export default class Assert
66
{
77
/**
88
* @param {object} objectValue
9-
* @param {function} instance
9+
* @param {function} expectedInstance
1010
* @param {string} [message]
1111
*/
12-
static instanceOf(objectValue, instance, message = "")
12+
static instanceOf(objectValue, expectedInstance, message = "")
1313
{
1414
this.string(message, "Custom error message passed to Assert.instanceOf needs to be a valid string.");
1515

1616
if (typeof objectValue !== 'object') {
1717
throw InvalidValueException.expected("object", objectValue, message);
1818
}
1919

20-
if (!(objectValue instanceof instance)) {
20+
this.isFunction(
21+
expectedInstance,
22+
"Expected argument needs to be a valid class instead of that got \"${received}\". Most likely there is typo in class name or file with class is missing export declaration."
23+
);
24+
25+
if (!(objectValue instanceof expectedInstance)) {
2126
throw InvalidValueException.expected(
22-
instance.name,
27+
expectedInstance.name,
2328
objectValue,
2429
message.length ? message : "Expected instance of \"${expected}\" but got \"${received}\"."
2530
);
@@ -279,7 +284,7 @@ export default class Assert
279284
throw InvalidValueException.expected("even number", integerValue, message);
280285
}
281286
}
282-
287+
283288
/**
284289
* @param {string} stringValue
285290
* @param {string} [message]

0 commit comments

Comments
 (0)