class Foo { constructor(num) { this._num = num} }; // dummy implementation
class Bar { constructor(str) { this._srr = str} }; // dummy implementation
const arrayOfFoos = [new Foo(1), new Foo(2), new Foo(3)];
const arrayOfBars = [new Bar("one"), new Bar("two"), new Bar("three")];
const arrayOfMixed = [new Foo(1), new Bar("two"), new Foo(3)];
describe("All Foos", function() {
beforeEach(function() {
this.array = arrayOfFoos;
});
it("every() + toBeTrue()", function() {
expect(this.array.every(x => x instanceof Foo))
.toBeTrue("Some items don't match");
});
it("loop + toBeInstanceOf()", function() {
for (const [index, x] of this.array.entries()) {
expect(x)
.withContext(`index [${index}]`)
.toBeInstanceOf(Foo);
}
});
it("loop + fail()", function() {
for (const [index, x] of this.array.entries()) {
if (!(x instanceof Foo))
fail(`index [${index}] is not Foo`, x);
}
});
});
describe("All Bars", function() {
beforeEach(function() {
this.array = arrayOfBars;
});
it("every() + toBeTrue()", function() {
expect(this.array.every(x => x instanceof Foo))
.toBeTrue("Some items don't match");
});
it("loop + toBeInstanceOf()", function() {
for (const [index, x] of this.array.entries()) {
expect(x)
.withContext(`index [${index}]`)
.toBeInstanceOf(Foo);
}
});
it("loop + fail()", function() {
for (const [index, x] of this.array.entries()) {
if (!(x instanceof Foo))
fail(`index [${index}] is not Foo`, x);
}
});
});
describe("Mixed", function() {
beforeEach(function() {
this.array = arrayOfMixed;
});
it("every() + toBeTrue()", function() {
expect(this.array.every(x => x instanceof Foo))
.toBeTrue("Some items don't match");
});
it("loop + toBeInstanceOf()", function() {
for (const [index, x] of this.array.entries()) {
expect(x)
.withContext(`index [${index}]`)
.toBeInstanceOf(Foo);
}
});
it("loop + fail()", function() {
for (const [index, x] of this.array.entries()) {
if (!(x instanceof Foo))
fail(`index [${index}] is not Foo`, x);
}
});
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.6.0/jasmine.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.6.0/jasmine.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.6.0/jasmine-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/3.6.0/boot.min.js"></script>