Merge remote-tracking branch 'upstream/main' into array_buffer

# Conflicts:
#	spec/core/matchers/matchersUtilSpec.js
This commit is contained in:
Surgie Finesse
2021-03-22 18:31:35 +10:00
246 changed files with 21415 additions and 8225 deletions

View File

@@ -1,47 +1,213 @@
describe("DiffBuilder", function() {
it("records the actual and expected objects", function() {
describe('DiffBuilder', function() {
it('records the actual and expected objects', function() {
var diffBuilder = jasmineUnderTest.DiffBuilder();
diffBuilder.record({x: 'actual'}, {x: 'expected'});
diffBuilder.setRoots({ x: 'actual' }, { x: 'expected' });
diffBuilder.recordMismatch();
expect(diffBuilder.getMessage()).toEqual("Expected Object({ x: 'actual' }) to equal Object({ x: 'expected' }).");
expect(diffBuilder.getMessage()).toEqual(
"Expected Object({ x: 'actual' }) to equal Object({ x: 'expected' })."
);
});
it("prints the path at which the difference was found", function() {
it('prints the path at which the difference was found', function() {
var diffBuilder = jasmineUnderTest.DiffBuilder();
diffBuilder.setRoots({ foo: { x: 'actual' } }, { foo: { x: 'expected' } });
diffBuilder.withPath('foo', function() {
diffBuilder.record({x: 'actual'}, {x: 'expected'});
diffBuilder.recordMismatch();
});
expect(diffBuilder.getMessage()).toEqual("Expected $.foo = Object({ x: 'actual' }) to equal Object({ x: 'expected' }).");
expect(diffBuilder.getMessage()).toEqual(
"Expected $.foo = Object({ x: 'actual' }) to equal Object({ x: 'expected' })."
);
});
it("prints multiple messages, separated by newlines", function() {
it('prints multiple messages, separated by newlines', function() {
var diffBuilder = jasmineUnderTest.DiffBuilder();
diffBuilder.setRoots({ foo: 1, bar: 3 }, { foo: 2, bar: 4 });
diffBuilder.withPath('foo', function() {
diffBuilder.record(1, 2);
diffBuilder.recordMismatch();
});
diffBuilder.withPath('bar', function() {
diffBuilder.recordMismatch();
});
var message =
"Expected $.foo = 1 to equal 2.\n" +
"Expected 3 to equal 4.";
'Expected $.foo = 1 to equal 2.\n' + 'Expected $.bar = 3 to equal 4.';
diffBuilder.record(3, 4);
expect(diffBuilder.getMessage()).toEqual(message);
});
it("allows customization of the message", function() {
it('allows customization of the message', function() {
var diffBuilder = jasmineUnderTest.DiffBuilder();
diffBuilder.setRoots({ x: 'bar' }, { x: 'foo' });
function darthVaderFormatter(actual, expected, path) {
return "I find your lack of " + expected + " disturbing. (was " + actual + ", at " + path + ")"
return (
'I find your lack of ' +
expected +
' disturbing. (was ' +
actual +
', at ' +
path +
')'
);
}
diffBuilder.withPath('x', function() {
diffBuilder.record('bar', 'foo', darthVaderFormatter);
diffBuilder.recordMismatch(darthVaderFormatter);
});
expect(diffBuilder.getMessage()).toEqual("I find your lack of foo disturbing. (was bar, at $.x)");
expect(diffBuilder.getMessage()).toEqual(
'I find your lack of foo disturbing. (was bar, at $.x)'
);
});
it('uses the injected pretty-printer', function() {
var prettyPrinter = function(val) {
return '|' + val + '|';
},
diffBuilder = jasmineUnderTest.DiffBuilder({
prettyPrinter: prettyPrinter
});
prettyPrinter.customFormat_ = function() {};
diffBuilder.setRoots({ foo: 'actual' }, { foo: 'expected' });
diffBuilder.withPath('foo', function() {
diffBuilder.recordMismatch();
});
expect(diffBuilder.getMessage()).toEqual(
'Expected $.foo = |actual| to equal |expected|.'
);
});
it('passes the injected pretty-printer to the diff formatter', function() {
var diffFormatter = jasmine.createSpy('diffFormatter'),
prettyPrinter = function() {},
diffBuilder = jasmineUnderTest.DiffBuilder({
prettyPrinter: prettyPrinter
});
prettyPrinter.customFormat_ = function() {};
diffBuilder.setRoots({ x: 'bar' }, { x: 'foo' });
diffBuilder.withPath('x', function() {
diffBuilder.recordMismatch(diffFormatter);
});
diffBuilder.getMessage();
expect(diffFormatter).toHaveBeenCalledWith(
'bar',
'foo',
jasmine.anything(),
prettyPrinter
);
});
it('uses custom object formatters on leaf nodes', function() {
var formatter = function(x) {
if (typeof x === 'number') {
return '[number:' + x + ']';
}
};
prettyPrinter = jasmineUnderTest.makePrettyPrinter([formatter]);
var diffBuilder = new jasmineUnderTest.DiffBuilder({
prettyPrinter: prettyPrinter
});
diffBuilder.setRoots(5, 4);
diffBuilder.recordMismatch();
expect(diffBuilder.getMessage()).toEqual(
'Expected [number:5] to equal [number:4].'
);
});
it('uses custom object formatters on non leaf nodes', function() {
var formatter = function(x) {
if (x.hasOwnProperty('a')) {
return '[thing with a=' + x.a + ', b=' + JSON.stringify(x.b) + ']';
}
};
prettyPrinter = jasmineUnderTest.makePrettyPrinter([formatter]);
var diffBuilder = new jasmineUnderTest.DiffBuilder({
prettyPrinter: prettyPrinter
});
var expectedMsg =
'Expected $[0].foo = [thing with a=1, b={"x":42}] to equal [thing with a=1, b={"x":43}].\n' +
"Expected $[0].bar = 'yes' to equal 'no'.";
diffBuilder.setRoots(
[{ foo: { a: 1, b: { x: 42 } }, bar: 'yes' }],
[{ foo: { a: 1, b: { x: 43 } }, bar: 'no' }]
);
diffBuilder.withPath(0, function() {
diffBuilder.withPath('foo', function() {
diffBuilder.withPath('b', function() {
diffBuilder.withPath('x', function() {
diffBuilder.recordMismatch();
});
});
});
diffBuilder.withPath('bar', function() {
diffBuilder.recordMismatch();
});
});
expect(diffBuilder.getMessage()).toEqual(expectedMsg);
});
it('builds diffs involving asymmetric equality testers that implement valuesForDiff_ at the root', function() {
var prettyPrinter = jasmineUnderTest.makePrettyPrinter([]),
diffBuilder = new jasmineUnderTest.DiffBuilder({
prettyPrinter: prettyPrinter
}),
expectedMsg =
'Expected $.foo = 1 to equal 2.\n' +
'Expected $.baz = undefined to equal 3.';
diffBuilder.setRoots(
{ foo: 1, bar: 2 },
jasmine.objectContaining({ foo: 2, baz: 3 })
);
diffBuilder.withPath('foo', function() {
diffBuilder.recordMismatch();
});
diffBuilder.withPath('baz', function() {
diffBuilder.recordMismatch();
});
expect(diffBuilder.getMessage()).toEqual(expectedMsg);
});
it('builds diffs involving asymmetric equality testers that implement valuesForDiff_ below the root', function() {
var prettyPrinter = jasmineUnderTest.makePrettyPrinter([]),
diffBuilder = new jasmineUnderTest.DiffBuilder({
prettyPrinter: prettyPrinter
}),
expectedMsg =
'Expected $.x.foo = 1 to equal 2.\n' +
'Expected $.x.baz = undefined to equal 3.';
diffBuilder.setRoots(
{ x: { foo: 1, bar: 2 } },
{ x: jasmine.objectContaining({ foo: 2, baz: 3 }) }
);
diffBuilder.withPath('x', function() {
diffBuilder.withPath('foo', function() {
diffBuilder.recordMismatch();
});
diffBuilder.withPath('baz', function() {
diffBuilder.recordMismatch();
});
});
expect(diffBuilder.getMessage()).toEqual(expectedMsg);
});
});

View File

@@ -0,0 +1,142 @@
describe('MismatchTree', function() {
describe('#add', function() {
describe('When the path is empty', function() {
it('flags the root node as mismatched', function() {
var tree = new jasmineUnderTest.MismatchTree();
tree.add(new jasmineUnderTest.ObjectPath([]));
expect(tree.isMismatch).toBe(true);
});
});
describe('When the path is not empty', function() {
it('flags the node as mismatched', function() {
var tree = new jasmineUnderTest.MismatchTree();
tree.add(new jasmineUnderTest.ObjectPath(['a', 'b']));
expect(tree.child('a').child('b').isMismatch).toBe(true);
});
it('does not flag ancestors as mismatched', function() {
var tree = new jasmineUnderTest.MismatchTree();
tree.add(new jasmineUnderTest.ObjectPath(['a', 'b']));
expect(tree.isMismatch).toBe(false);
expect(tree.child('a').isMismatch).toBe(false);
});
});
it('stores the formatter on only the target node', function() {
var tree = new jasmineUnderTest.MismatchTree();
tree.add(new jasmineUnderTest.ObjectPath(['a', 'b']), formatter);
expect(tree.formatter).toBeFalsy();
expect(tree.child('a').formatter).toBeFalsy();
expect(tree.child('a').child('b').formatter).toBe(formatter);
});
it('stores the path to the node', function() {
var tree = new jasmineUnderTest.MismatchTree();
tree.add(new jasmineUnderTest.ObjectPath(['a', 'b']), formatter);
expect(tree.child('a').child('b').path.components).toEqual(['a', 'b']);
});
});
describe('#traverse', function() {
it('calls the callback for all nodes that are or contain mismatches', function() {
var tree = new jasmineUnderTest.MismatchTree();
tree.add(new jasmineUnderTest.ObjectPath(['a', 'b']), formatter);
tree.add(new jasmineUnderTest.ObjectPath(['c']));
var visit = jasmine.createSpy('visit').and.returnValue(true);
tree.traverse(visit);
expect(visit).toHaveBeenCalledWith(
new jasmineUnderTest.ObjectPath([]),
false,
undefined
);
expect(visit).toHaveBeenCalledWith(
new jasmineUnderTest.ObjectPath(['a']),
false,
undefined
);
expect(visit).toHaveBeenCalledWith(
new jasmineUnderTest.ObjectPath(['a', 'b']),
true,
formatter
);
expect(visit).toHaveBeenCalledWith(
new jasmineUnderTest.ObjectPath(['c']),
true,
undefined
);
});
it('does not call the callback if there are no mismatches', function() {
var tree = new jasmineUnderTest.MismatchTree();
var visit = jasmine.createSpy('visit');
tree.traverse(visit);
expect(visit).not.toHaveBeenCalled();
});
it('visits parents before children', function() {
var tree = new jasmineUnderTest.MismatchTree();
tree.add(new jasmineUnderTest.ObjectPath(['a', 'b']));
var visited = [];
tree.traverse(function(path) {
visited.push(path);
return true;
});
expect(visited).toEqual([
new jasmineUnderTest.ObjectPath([]),
new jasmineUnderTest.ObjectPath(['a']),
new jasmineUnderTest.ObjectPath(['a', 'b'])
]);
});
it('visits children in the order they were recorded', function() {
var tree = new jasmineUnderTest.MismatchTree();
tree.add(new jasmineUnderTest.ObjectPath(['length']));
tree.add(new jasmineUnderTest.ObjectPath([1]));
var visited = [];
tree.traverse(function(path) {
visited.push(path);
return true;
});
expect(visited).toEqual([
new jasmineUnderTest.ObjectPath([]),
new jasmineUnderTest.ObjectPath(['length']),
new jasmineUnderTest.ObjectPath([1])
]);
});
it('does not visit children if the callback returns falsy', function() {
var tree = new jasmineUnderTest.MismatchTree();
tree.add(new jasmineUnderTest.ObjectPath(['a', 'b']));
var visited = [];
tree.traverse(function(path) {
visited.push(path);
return path.depth() === 0;
});
expect(visited).toEqual([
new jasmineUnderTest.ObjectPath([]),
new jasmineUnderTest.ObjectPath(['a'])
]);
});
});
function formatter() {}
});

View File

@@ -4,10 +4,4 @@ describe('NullDiffBuilder', function() {
jasmineUnderTest.NullDiffBuilder().withPath('does not matter', spy);
expect(spy).toHaveBeenCalled();
});
it('responds to record()', function() {
expect(function() {
jasmineUnderTest.NullDiffBuilder().record('does not matter');
}).not.toThrow();
})
});

View File

@@ -39,5 +39,5 @@ describe('ObjectPath', function() {
expect(path.toString()).toEqual('$.foo');
expect(root.toString()).toEqual('');
})
});
});

View File

@@ -0,0 +1,50 @@
/* eslint-disable compat/compat */
describe('toBePending', function() {
it('passes if the actual promise is pending', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBePending(matchersUtil),
actual = new Promise(function() {});
return matcher.compare(actual).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('fails if the actual promise is resolved', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBePending(matchersUtil),
actual = Promise.resolve();
return matcher.compare(actual).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
});
});
it('fails if the actual promise is rejected', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBePending(matchersUtil),
actual = Promise.reject(new Error('promise was rejected'));
return matcher.compare(actual).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
});
});
it('fails if actual is not a promise', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBePending(matchersUtil),
actual = 'not a promise';
function f() {
return matcher.compare(actual);
}
expect(f).toThrowError('Expected toBePending to be called on a promise.');
});
});

View File

@@ -1,23 +1,38 @@
/* eslint-disable compat/compat */
describe('toBeRejected', function() {
it('passes if the actual is rejected', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeRejected(jasmineUnderTest.matchersUtil),
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBeRejected(matchersUtil),
actual = Promise.reject('AsyncExpectationSpec rejection');
return matcher.compare(actual).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({pass: true}));
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('fails if the actual is resolved', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeRejected(jasmineUnderTest.matchersUtil),
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBeRejected(matchersUtil),
actual = Promise.resolve();
return matcher.compare(actual).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({pass: false}));
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
});
});
it('fails if actual is not a promise', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBeRejected(matchersUtil),
actual = 'not a promise';
function f() {
return matcher.compare(actual);
}
expect(f).toThrowError('Expected toBeRejected to be called on a promise.');
});
});

View File

@@ -0,0 +1,261 @@
/* eslint-disable compat/compat */
describe('#toBeRejectedWithError', function() {
it('passes when Error type matches', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject(new TypeError('foo'));
return matcher.compare(actual, TypeError).then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message:
'Expected a promise not to be rejected with TypeError, but it was.'
})
);
});
});
it('passes when Error type and message matches', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject(new TypeError('foo'));
return matcher.compare(actual, TypeError, 'foo').then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message:
"Expected a promise not to be rejected with TypeError: 'foo', but it was."
})
);
});
});
it('passes when Error matches and is exactly Error', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject(new Error());
return matcher.compare(actual, Error).then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message:
'Expected a promise not to be rejected with Error, but it was.'
})
);
});
});
it('passes when Error message matches a string', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject(new Error('foo'));
return matcher.compare(actual, 'foo').then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message:
"Expected a promise not to be rejected with Error: 'foo', but it was."
})
);
});
});
it('passes when Error message matches a RegExp', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject(new Error('foo'));
return matcher.compare(actual, /foo/).then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message:
'Expected a promise not to be rejected with Error: /foo/, but it was.'
})
);
});
});
it('passes when Error message is empty', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject(new Error());
return matcher.compare(actual, '').then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message:
"Expected a promise not to be rejected with Error: '', but it was."
})
);
});
});
it('passes when no arguments', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject(new Error());
return matcher.compare(actual, void 0).then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message:
'Expected a promise not to be rejected with Error, but it was.'
})
);
});
});
it('fails when resolved', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.resolve(new Error('foo'));
return matcher.compare(actual, 'foo').then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: false,
message: 'Expected a promise to be rejected but it was resolved.'
})
);
});
});
it('fails when rejected with non Error type', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject('foo');
return matcher.compare(actual, 'foo').then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: false,
message:
"Expected a promise to be rejected with Error: 'foo' but it was rejected with 'foo'."
})
);
});
});
it('fails when Error type mismatches', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject(new Error('foo'));
return matcher.compare(actual, TypeError, 'foo').then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: false,
message:
"Expected a promise to be rejected with TypeError: 'foo' but it was rejected with type Error."
})
);
});
});
it('fails when Error message mismatches', function() {
jasmine.getEnv().requirePromises();
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = Promise.reject(new Error('foo'));
return matcher.compare(actual, 'bar').then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: false,
message:
"Expected a promise to be rejected with Error: 'bar' but it was rejected with Error: foo."
})
);
});
});
it('fails if actual is not a promise', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWithError(
matchersUtil
),
actual = 'not a promise';
function f() {
return matcher.compare(actual);
}
expect(f).toThrowError(
'Expected toBeRejectedWithError to be called on a promise.'
);
});
});

View File

@@ -1,63 +1,100 @@
describe('#toBeRejectedWith', function () {
it('should return true if the promise is rejected with the expected value', function () {
/* eslint-disable compat/compat */
describe('#toBeRejectedWith', function() {
it('should return true if the promise is rejected with the expected value', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil),
actual = Promise.reject({error: 'PEBCAK'});
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(matchersUtil),
actual = Promise.reject({ error: 'PEBCAK' });
return matcher.compare(actual, {error: 'PEBCAK'}).then(function (result) {
return matcher.compare(actual, { error: 'PEBCAK' }).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('should fail if the promise resolves', function () {
it('should fail if the promise resolves', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil),
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(matchersUtil),
actual = Promise.resolve();
return matcher.compare(actual, '').then(function (result) {
return matcher.compare(actual, '').then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
});
});
it('should fail if the promise is rejected with a different value', function () {
it('should fail if the promise is rejected with a different value', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil),
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(matchersUtil),
actual = Promise.reject('A Bad Apple');
return matcher.compare(actual, 'Some Cool Thing').then(function (result) {
expect(result).toEqual(jasmine.objectContaining({
pass: false,
message: "Expected a promise to be rejected with 'Some Cool Thing' but it was rejected with 'A Bad Apple'.",
}));
return matcher.compare(actual, 'Some Cool Thing').then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: false,
message:
"Expected a promise to be rejected with 'Some Cool Thing' but it was rejected with 'A Bad Apple'."
})
);
});
});
it('should build its error correctly when negated', function () {
it('should build its error correctly when negated', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil),
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(matchersUtil),
actual = Promise.reject(true);
return matcher.compare(actual, true).then(function (result) {
expect(result).toEqual(jasmine.objectContaining({
pass: true,
message: 'Expected a promise not to be rejected with true.'
}));
return matcher.compare(actual, true).then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message: 'Expected a promise not to be rejected with true.'
})
);
});
});
it('should support custom equality testers', function () {
it('should support custom equality testers', function() {
jasmine.getEnv().requirePromises();
var customEqualityTesters = [function() { return true; }],
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(jasmineUnderTest.matchersUtil, customEqualityTesters),
var customEqualityTesters = [
function() {
return true;
}
],
matchersUtil = new jasmineUnderTest.MatchersUtil({
customTesters: customEqualityTesters
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(matchersUtil),
actual = Promise.reject('actual');
return matcher.compare(actual, 'expected').then(function(result) {
expect(result).toEqual(jasmine.objectContaining({pass: true}));
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('fails if actual is not a promise', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeRejectedWith(matchersUtil),
actual = 'not a promise';
function f() {
return matcher.compare(actual);
}
expect(f).toThrowError(
'Expected toBeRejectedWith to be called on a promise.'
);
});
});

View File

@@ -1,23 +1,38 @@
/* eslint-disable compat/compat */
describe('toBeResolved', function() {
it('passes if the actual is resolved', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeResolved(jasmineUnderTest.matchersUtil),
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBeResolved(matchersUtil),
actual = Promise.resolve();
return matcher.compare(actual).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({pass: true}));
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('fails if the actual is rejected', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeResolved(jasmineUnderTest.matchersUtil),
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBeResolved(matchersUtil),
actual = Promise.reject('AsyncExpectationSpec rejection');
return matcher.compare(actual).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({pass: false}));
expect(result).toEqual(jasmine.objectContaining({ pass: false }));
});
});
it('fails if actual is not a promise', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBeResolved(matchersUtil),
actual = 'not a promise';
function f() {
return matcher.compare(actual);
}
expect(f).toThrowError('Expected toBeResolved to be called on a promise.');
});
});

View File

@@ -1,66 +1,109 @@
/* eslint-disable compat/compat */
describe('#toBeResolvedTo', function() {
it('passes if the promise is resolved to the expected value', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil),
actual = Promise.resolve({foo: 42});
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(matchersUtil),
actual = Promise.resolve({ foo: 42 });
return matcher.compare(actual, {foo: 42}).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({pass: true}));
return matcher.compare(actual, { foo: 42 }).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('fails if the promise is rejected', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil),
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(matchersUtil),
actual = Promise.reject('AsyncExpectationSpec error');
return matcher.compare(actual, '').then(function(result) {
expect(result).toEqual(jasmine.objectContaining({
pass: false,
message: "Expected a promise to be resolved to '' but it was rejected.",
}));
expect(result).toEqual(
jasmine.objectContaining({
pass: false,
message:
"Expected a promise to be resolved to '' but it was rejected."
})
);
});
});
it('fails if the promise is resolved to a different value', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil),
actual = Promise.resolve({foo: 17});
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(matchersUtil),
actual = Promise.resolve({ foo: 17 });
return matcher.compare(actual, {foo: 42}).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({
pass: false,
message: 'Expected a promise to be resolved to Object({ foo: 42 }) but it was resolved to Object({ foo: 17 }).',
}));
return matcher.compare(actual, { foo: 42 }).then(function(result) {
expect(result).toEqual(
jasmine.objectContaining({
pass: false,
message:
'Expected a promise to be resolved to Object({ foo: 42 }) but it was resolved to Object({ foo: 17 }).'
})
);
});
});
it('builds its message correctly when negated', function() {
jasmine.getEnv().requirePromises();
var matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil),
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(matchersUtil),
actual = Promise.resolve(true);
return matcher.compare(actual, true).then(function(result) {
expect(result).toEqual(jasmine.objectContaining({
pass: true,
message: 'Expected a promise not to be resolved to true.'
}));
expect(result).toEqual(
jasmine.objectContaining({
pass: true,
message: 'Expected a promise not to be resolved to true.'
})
);
});
});
it('supports custom equality testers', function() {
jasmine.getEnv().requirePromises();
var customEqualityTesters = [function() { return true; }],
matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(jasmineUnderTest.matchersUtil, customEqualityTesters),
var customEqualityTesters = [
function() {
return true;
}
],
matchersUtil = new jasmineUnderTest.MatchersUtil({
customTesters: customEqualityTesters,
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(matchersUtil),
actual = Promise.resolve('actual');
return matcher.compare(actual, 'expected').then(function(result) {
expect(result).toEqual(jasmine.objectContaining({pass: true}));
expect(result).toEqual(jasmine.objectContaining({ pass: true }));
});
});
it('fails if actual is not a promise', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.asyncMatchers.toBeResolvedTo(matchersUtil),
actual = 'not a promise';
function f() {
return matcher.compare(actual);
}
expect(f).toThrowError(
'Expected toBeResolvedTo to be called on a promise.'
);
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
describe("toBeCloseTo", function() {
it("passes when within two decimal places by default", function() {
describe('toBeCloseTo', function() {
it('passes when within two decimal places by default', function() {
var matcher = jasmineUnderTest.matchers.toBeCloseTo(),
result;
@@ -13,7 +13,7 @@ describe("toBeCloseTo", function() {
expect(result.pass).toBe(true);
});
it("fails when not within two decimal places by default", function() {
it('fails when not within two decimal places by default', function() {
var matcher = jasmineUnderTest.matchers.toBeCloseTo(),
result;
@@ -24,7 +24,7 @@ describe("toBeCloseTo", function() {
expect(result.pass).toBe(false);
});
it("accepts an optional precision argument", function() {
it('accepts an optional precision argument', function() {
var matcher = jasmineUnderTest.matchers.toBeCloseTo(),
result;
@@ -47,23 +47,29 @@ describe("toBeCloseTo", function() {
expect(result.pass).toBe(true);
});
it("fails when one of the arguments is null", function() {
it('fails when one of the arguments is null', function() {
var matcher = jasmineUnderTest.matchers.toBeCloseTo();
expect(function() {
matcher.compare(null, null);
}).toThrowError('Cannot use toBeCloseTo with null. Arguments evaluated to: expect(null).toBeCloseTo(null).');
}).toThrowError(
'Cannot use toBeCloseTo with null. Arguments evaluated to: expect(null).toBeCloseTo(null).'
);
expect(function() {
matcher.compare(0, null);
}).toThrowError('Cannot use toBeCloseTo with null. Arguments evaluated to: expect(0).toBeCloseTo(null).');
}).toThrowError(
'Cannot use toBeCloseTo with null. Arguments evaluated to: expect(0).toBeCloseTo(null).'
);
expect(function() {
matcher.compare(null, 0);
}).toThrowError('Cannot use toBeCloseTo with null. Arguments evaluated to: expect(null).toBeCloseTo(0).');
}).toThrowError(
'Cannot use toBeCloseTo with null. Arguments evaluated to: expect(null).toBeCloseTo(0).'
);
});
it("rounds expected values", function() {
it('rounds expected values', function() {
var matcher = jasmineUnderTest.matchers.toBeCloseTo(),
result;
@@ -90,4 +96,18 @@ describe("toBeCloseTo", function() {
result = matcher.compare(1.23, 1.234);
expect(result.pass).toBe(true);
});
it('handles edge cases with rounding', function() {
var matcher = jasmineUnderTest.matchers.toBeCloseTo(),
result;
// these cases resulted in false negatives in version of V8
// included in Node.js 12 and Chrome 74 (and Edge Chromium)
result = matcher.compare(4.030904708957288, 4.0309, 5);
expect(result.pass).toBe(true);
result = matcher.compare(4.82665525779431, 4.82666, 5);
expect(result.pass).toBe(true);
result = matcher.compare(-2.82665525779431, -2.82666, 5);
expect(result.pass).toBe(true);
});
});

View File

@@ -1,18 +1,17 @@
describe("toBeDefined", function() {
it("matches for defined values", function() {
describe('toBeDefined', function() {
it('matches for defined values', function() {
var matcher = jasmineUnderTest.matchers.toBeDefined(),
result;
result = matcher.compare('foo');
expect(result.pass).toBe(true);
});
it("fails when matching undefined values", function() {
it('fails when matching undefined values', function() {
var matcher = jasmineUnderTest.matchers.toBeDefined(),
result;
result = matcher.compare(void 0);
expect(result.pass).toBe(false);
})
});
});

View File

@@ -1,5 +1,5 @@
describe("toBeFalse", function() {
it("passes for false", function() {
describe('toBeFalse', function() {
it('passes for false', function() {
var matcher = jasmineUnderTest.matchers.toBeFalse(),
result;
@@ -7,7 +7,7 @@ describe("toBeFalse", function() {
expect(result.pass).toBe(true);
});
it("fails for non-false", function() {
it('fails for non-false', function() {
var matcher = jasmineUnderTest.matchers.toBeFalse(),
result;
@@ -15,7 +15,7 @@ describe("toBeFalse", function() {
expect(result.pass).toBe(false);
});
it("fails for falsy", function() {
it('fails for falsy', function() {
var matcher = jasmineUnderTest.matchers.toBeFalse(),
result;

View File

@@ -1,4 +1,4 @@
describe("toBeFalsy", function() {
describe('toBeFalsy', function() {
it("passes for 'falsy' values", function() {
var matcher = jasmineUnderTest.matchers.toBeFalsy(),
result;
@@ -15,6 +15,9 @@ describe("toBeFalsy", function() {
result = matcher.compare(null);
expect(result.pass).toBe(true);
result = matcher.compare(undefined);
expect(result.pass).toBe(true);
result = matcher.compare(void 0);
expect(result.pass).toBe(true);
});
@@ -29,10 +32,16 @@ describe("toBeFalsy", function() {
result = matcher.compare(1);
expect(result.pass).toBe(false);
result = matcher.compare("foo");
result = matcher.compare('foo');
expect(result.pass).toBe(false);
result = matcher.compare({});
expect(result.pass).toBe(false);
result = matcher.compare([]);
expect(result.pass).toBe(false);
result = matcher.compare(function() {});
expect(result.pass).toBe(false);
});
});

View File

@@ -1,5 +1,5 @@
describe("toBeGreaterThanOrEqual", function() {
it("passes when actual >= expected", function() {
describe('toBeGreaterThanOrEqual', function() {
it('passes when actual >= expected', function() {
var matcher = jasmineUnderTest.matchers.toBeGreaterThanOrEqual(),
result;
@@ -16,7 +16,7 @@ describe("toBeGreaterThanOrEqual", function() {
expect(result.pass).toBe(true);
});
it("fails when actual < expected", function() {
it('fails when actual < expected', function() {
var matcher = jasmineUnderTest.matchers.toBeGreaterThanOrEqual(),
result;
@@ -25,5 +25,5 @@ describe("toBeGreaterThanOrEqual", function() {
result = matcher.compare(1, 1.0000001);
expect(result.pass).toBe(false);
})
});
});

View File

@@ -1,5 +1,5 @@
describe("toBeGreaterThan", function() {
it("passes when actual > expected", function() {
describe('toBeGreaterThan', function() {
it('passes when actual > expected', function() {
var matcher = jasmineUnderTest.matchers.toBeGreaterThan(),
result;
@@ -7,7 +7,7 @@ describe("toBeGreaterThan", function() {
expect(result.pass).toBe(true);
});
it("fails when actual <= expected", function() {
it('fails when actual <= expected', function() {
var matcher = jasmineUnderTest.matchers.toBeGreaterThan(),
result;

View File

@@ -0,0 +1,241 @@
describe('toBeInstanceOf', function() {
describe('when expecting Number', function() {
it('passes for literal number', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(3, Number);
expect(result).toEqual({
pass: true,
message: 'Expected instance of Number not to be an instance of Number'
});
});
it('passes for NaN', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf({
pp: jasmineUnderTest.makePrettyPrinter()
});
var result = matcher.compare(NaN, Number);
expect(result).toEqual({
pass: true,
message: 'Expected instance of NaN not to be an instance of Number'
});
});
it('passes for Infinity', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(Infinity, Number);
expect(result).toEqual({
pass: true,
message: 'Expected instance of Number not to be an instance of Number'
});
});
it('fails for a non-number', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare('foo', Number);
expect(result).toEqual({
pass: false,
message: 'Expected instance of String to be an instance of Number'
});
});
});
describe('when expecting String', function() {
it('passes for a string', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare('foo', String);
expect(result).toEqual({
pass: true,
message: 'Expected instance of String not to be an instance of String'
});
});
it('fails for a non-string', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare({}, String);
expect(result).toEqual({
pass: false,
message: 'Expected instance of Object to be an instance of String'
});
});
});
describe('when expecting Boolean', function() {
it('passes for a boolean', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(true, Boolean);
expect(result).toEqual({
pass: true,
message: 'Expected instance of Boolean not to be an instance of Boolean'
});
});
it('fails for a non-boolean', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare('false', Boolean);
expect(result).toEqual({
pass: false,
message: 'Expected instance of String to be an instance of Boolean'
});
});
});
describe('when expecting RegExp', function() {
it('passes for a literal regular expression', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(/foo/, RegExp);
expect(result).toEqual({
pass: true,
message: 'Expected instance of RegExp not to be an instance of RegExp'
});
});
});
describe('when expecting Function', function() {
it('passes for a function', function() {
var fn = function() {};
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(fn, Function);
expect(result).toEqual({
pass: true,
message:
'Expected instance of Function not to be an instance of Function'
});
});
it('passes for an async function', function() {
jasmine.getEnv().requireAsyncAwait();
var fn = eval("(async function fn() { return 'foo'; })");
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(fn, Function);
expect(result).toEqual({
pass: true,
message:
'Expected instance of AsyncFunction not to be an instance of Function'
});
});
});
describe('when expecting Object', function() {
function Animal() {}
it('passes for any object', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare({ foo: 'bar' }, Object);
expect(result).toEqual({
pass: true,
message: 'Expected instance of Object not to be an instance of Object'
});
});
it('passes for an Error object', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(new Error('example'), Object);
expect(result).toEqual({
pass: true,
message: 'Expected instance of Error not to be an instance of Object'
});
});
it('passes for a user-defined class', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(new Animal(), Object);
expect(result).toEqual({
pass: true,
message: 'Expected instance of Animal not to be an instance of Object'
});
});
it('fails for a non-object', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare('foo', Object);
expect(result).toEqual({
pass: false,
message: 'Expected instance of String to be an instance of Object'
});
});
it('passes for objects with no constructor', function() {
var object = Object.create(null);
var matcher = jasmineUnderTest.matchers.toBeInstanceOf({
pp: jasmineUnderTest.makePrettyPrinter()
});
var result = matcher.compare(object, Object);
expect(result).toEqual({
pass: true,
message:
'Expected instance of null({ }) not to be an instance of Object'
});
});
});
describe('when expecting a user-defined class', function() {
// Base class
function Animal() {}
// Subclasses, defined using syntax that is as old as possible
function Dog() {
Animal.call(this);
}
Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;
function Cat() {
Animal.call(this);
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
it('passes for instances of that class', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(new Animal(), Animal);
expect(result).toEqual({
pass: true,
message: 'Expected instance of Animal not to be an instance of Animal'
});
});
it('passes for instances of a subclass', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(new Cat(), Animal);
expect(result).toEqual({
pass: true,
message: 'Expected instance of Cat not to be an instance of Animal'
});
});
it('does not pass for sibling classes', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
var result = matcher.compare(new Dog(), Cat);
expect(result).toEqual({
pass: false,
message: 'Expected instance of Dog to be an instance of Cat'
});
});
});
it('raises an error if passed an invalid expected value', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf();
expect(function() {
matcher.compare({}, 'Error');
}).toThrowError(
'<toBeInstanceOf> : Expected value is not a constructor function\n' +
'Usage: expect(value).toBeInstanceOf(<ConstructorFunction>)'
);
});
it('raises an error if missing an expected value', function() {
var matcher = jasmineUnderTest.matchers.toBeInstanceOf({
pp: jasmineUnderTest.makePrettyPrinter()
});
expect(function() {
matcher.compare({}, undefined);
}).toThrowError(
'<toBeInstanceOf> : Expected value is not a constructor function\n' +
'Usage: expect(value).toBeInstanceOf(<ConstructorFunction>)'
);
});
});

View File

@@ -1,5 +1,5 @@
describe("toBeLessThanOrEqual", function() {
it("passes when actual <= expected", function() {
describe('toBeLessThanOrEqual', function() {
it('passes when actual <= expected', function() {
var matcher = jasmineUnderTest.matchers.toBeLessThanOrEqual(),
result;
@@ -11,12 +11,12 @@ describe("toBeLessThanOrEqual", function() {
result = matcher.compare(1, 1.0000001);
expect(result.pass).toBe(true);
result = matcher.compare(1.0, 1.0);
expect(result.pass).toBe(true);
});
it("fails when actual < expected", function() {
it('fails when actual < expected', function() {
var matcher = jasmineUnderTest.matchers.toBeLessThanOrEqual(),
result;

View File

@@ -1,5 +1,5 @@
describe("toBeLessThan", function() {
it("passes when actual < expected", function() {
describe('toBeLessThan', function() {
it('passes when actual < expected', function() {
var matcher = jasmineUnderTest.matchers.toBeLessThan(),
result;
@@ -7,7 +7,7 @@ describe("toBeLessThan", function() {
expect(result.pass).toBe(true);
});
it("fails when actual <= expected", function() {
it('fails when actual <= expected', function() {
var matcher = jasmineUnderTest.matchers.toBeLessThan(),
result;

View File

@@ -1,14 +1,14 @@
describe("toBeNaN", function() {
it("passes for NaN with a custom .not fail", function() {
describe('toBeNaN', function() {
it('passes for NaN with a custom .not fail', function() {
var matcher = jasmineUnderTest.matchers.toBeNaN(),
result;
result = matcher.compare(Number.NaN);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected actual not to be NaN.");
expect(result.message).toEqual('Expected actual not to be NaN.');
});
it("fails for anything not a NaN", function() {
it('fails for anything not a NaN', function() {
var matcher = jasmineUnderTest.matchers.toBeNaN(),
result;
@@ -28,10 +28,12 @@ describe("toBeNaN", function() {
expect(result.pass).toBe(false);
});
it("has a custom message on failure", function() {
var matcher = jasmineUnderTest.matchers.toBeNaN(),
it('has a custom message on failure', function() {
var matcher = jasmineUnderTest.matchers.toBeNaN({
pp: jasmineUnderTest.makePrettyPrinter()
}),
result = matcher.compare(0);
expect(result.message()).toEqual("Expected 0 to be NaN.");
expect(result.message()).toEqual('Expected 0 to be NaN.');
});
});

View File

@@ -1,4 +1,4 @@
describe("toBeNegativeInfinity", function() {
describe('toBeNegativeInfinity', function() {
it("fails for anything that isn't -Infinity", function() {
var matcher = jasmineUnderTest.matchers.toBeNegativeInfinity(),
result;
@@ -13,19 +13,20 @@ describe("toBeNegativeInfinity", function() {
expect(result.pass).toBe(false);
});
it("has a custom message on failure", function() {
var matcher = jasmineUnderTest.matchers.toBeNegativeInfinity(),
it('has a custom message on failure', function() {
var matcher = jasmineUnderTest.matchers.toBeNegativeInfinity({
pp: jasmineUnderTest.makePrettyPrinter()
}),
result = matcher.compare(0);
expect(result.message()).toEqual("Expected 0 to be -Infinity.")
expect(result.message()).toEqual('Expected 0 to be -Infinity.');
});
it("succeeds for -Infinity", function() {
it('succeeds for -Infinity', function() {
var matcher = jasmineUnderTest.matchers.toBeNegativeInfinity(),
result = matcher.compare(Number.NEGATIVE_INFINITY);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected actual not to be -Infinity.")
expect(result.message).toEqual('Expected actual not to be -Infinity.');
});
});

View File

@@ -1,5 +1,5 @@
describe("toBeNull", function() {
it("passes for null", function() {
describe('toBeNull', function() {
it('passes for null', function() {
var matcher = jasmineUnderTest.matchers.toBeNull(),
result;
@@ -7,7 +7,7 @@ describe("toBeNull", function() {
expect(result.pass).toBe(true);
});
it("fails for non-null", function() {
it('fails for non-null', function() {
var matcher = jasmineUnderTest.matchers.toBeNull(),
result;

View File

@@ -1,4 +1,4 @@
describe("toBePositiveInfinity", function() {
describe('toBePositiveInfinity', function() {
it("fails for anything that isn't Infinity", function() {
var matcher = jasmineUnderTest.matchers.toBePositiveInfinity(),
result;
@@ -13,19 +13,20 @@ describe("toBePositiveInfinity", function() {
expect(result.pass).toBe(false);
});
it("has a custom message on failure", function() {
var matcher = jasmineUnderTest.matchers.toBePositiveInfinity(),
it('has a custom message on failure', function() {
var matcher = jasmineUnderTest.matchers.toBePositiveInfinity({
pp: jasmineUnderTest.makePrettyPrinter()
}),
result = matcher.compare(0);
expect(result.message()).toEqual("Expected 0 to be Infinity.")
expect(result.message()).toEqual('Expected 0 to be Infinity.');
});
it("succeeds for Infinity", function() {
it('succeeds for Infinity', function() {
var matcher = jasmineUnderTest.matchers.toBePositiveInfinity(),
result = matcher.compare(Number.POSITIVE_INFINITY);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected actual not to be Infinity.")
expect(result.message).toEqual('Expected actual not to be Infinity.');
});
});

View File

@@ -1,34 +1,46 @@
describe("toBe", function() {
it("passes with no message when actual === expected", function() {
var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil),
describe('toBe', function() {
it('passes with no message when actual === expected', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.matchers.toBe(matchersUtil),
result;
result = matcher.compare(1, 1);
expect(result.pass).toBe(true);
});
it("passes with a custom message when expected is an array", function() {
var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil),
it('passes with a custom message when expected is an array', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.matchers.toBe(matchersUtil),
result,
array = [1];
result = matcher.compare(array, array);
expect(result.pass).toBe(true);
expect(result.message).toBe("Expected [ 1 ] not to be [ 1 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().")
expect(result.message).toBe(
'Expected [ 1 ] not to be [ 1 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().'
);
});
it("passes with a custom message when expected is an object", function() {
var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil),
it('passes with a custom message when expected is an object', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.matchers.toBe(matchersUtil),
result,
obj = {foo: "bar"};
obj = { foo: 'bar' };
result = matcher.compare(obj, obj);
expect(result.pass).toBe(true);
expect(result.message).toBe("Expected Object({ foo: 'bar' }) not to be Object({ foo: 'bar' }). Tip: To check for deep equality, use .toEqual() instead of .toBe().")
expect(result.message).toBe(
"Expected Object({ foo: 'bar' }) not to be Object({ foo: 'bar' }). Tip: To check for deep equality, use .toEqual() instead of .toBe()."
);
});
it("fails with no message when actual !== expected", function() {
var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil),
it('fails with no message when actual !== expected', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
matcher = jasmineUnderTest.matchers.toBe(matchersUtil),
result;
result = matcher.compare(1, 2);
@@ -36,21 +48,47 @@ describe("toBe", function() {
expect(result.message).toBeUndefined();
});
it("fails with a custom message when expected is an array", function() {
var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil),
it('fails with a custom message when expected is an array', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.matchers.toBe(matchersUtil),
result;
result = matcher.compare([1], [1]);
expect(result.pass).toBe(false);
expect(result.message).toBe("Expected [ 1 ] to be [ 1 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().")
expect(result.message).toBe(
'Expected [ 1 ] to be [ 1 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().'
);
});
it("fails with a custom message when expected is an object", function() {
var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil),
it('fails with a custom message when expected is an object', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.matchers.toBe(matchersUtil),
result;
result = matcher.compare({foo: "bar"}, {foo: "bar"});
result = matcher.compare({ foo: 'bar' }, { foo: 'bar' });
expect(result.pass).toBe(false);
expect(result.message).toBe("Expected Object({ foo: 'bar' }) to be Object({ foo: 'bar' }). Tip: To check for deep equality, use .toEqual() instead of .toBe().")
expect(result.message).toBe(
"Expected Object({ foo: 'bar' }) to be Object({ foo: 'bar' }). Tip: To check for deep equality, use .toEqual() instead of .toBe()."
);
});
it('works with custom object formatters when expected is an object', function() {
var formatter = function(x) {
return '<' + x.foo + '>';
},
prettyPrinter = jasmineUnderTest.makePrettyPrinter([formatter]),
matchersUtil = new jasmineUnderTest.MatchersUtil({ pp: prettyPrinter }),
matcher = jasmineUnderTest.matchers.toBe(matchersUtil),
result;
result = matcher.compare({ foo: 'bar' }, { foo: 'bar' });
expect(result.pass).toBe(false);
expect(result.message).toBe(
'Expected <bar> to be <bar>. Tip: To check for deep equality, use .toEqual() instead of .toBe().'
);
});
});

View File

@@ -1,5 +1,5 @@
describe("toBeTrue", function() {
it("passes for true", function() {
describe('toBeTrue', function() {
it('passes for true', function() {
var matcher = jasmineUnderTest.matchers.toBeTrue(),
result;
@@ -7,7 +7,7 @@ describe("toBeTrue", function() {
expect(result.pass).toBe(true);
});
it("fails for non-true", function() {
it('fails for non-true', function() {
var matcher = jasmineUnderTest.matchers.toBeTrue(),
result;

View File

@@ -1,4 +1,4 @@
describe("toBeTruthy", function() {
describe('toBeTruthy', function() {
it("passes for 'truthy' values", function() {
var matcher = jasmineUnderTest.matchers.toBeTruthy(),
result;
@@ -9,11 +9,17 @@ describe("toBeTruthy", function() {
result = matcher.compare(1);
expect(result.pass).toBe(true);
result = matcher.compare("foo");
result = matcher.compare('foo');
expect(result.pass).toBe(true);
result = matcher.compare({});
expect(result.pass).toBe(true);
result = matcher.compare([]);
expect(result.pass).toBe(true);
result = matcher.compare(function() {});
expect(result.pass).toBe(true);
});
it("fails for 'falsy' values", function() {
@@ -32,6 +38,9 @@ describe("toBeTruthy", function() {
result = matcher.compare(null);
expect(result.pass).toBe(false);
result = matcher.compare(undefined);
expect(result.pass).toBe(false);
result = matcher.compare(void 0);
expect(result.pass).toBe(false);
});

View File

@@ -1,18 +1,17 @@
describe("toBeUndefined", function() {
it("passes for undefined values", function() {
describe('toBeUndefined', function() {
it('passes for undefined values', function() {
var matcher = jasmineUnderTest.matchers.toBeUndefined(),
result;
result = matcher.compare(void 0);
expect(result.pass).toBe(true);
});
it("fails when matching defined values", function() {
it('fails when matching defined values', function() {
var matcher = jasmineUnderTest.matchers.toBeUndefined(),
result;
result = matcher.compare('foo');
expect(result.pass).toBe(false);
})
});
});

View File

@@ -1,26 +1,27 @@
describe("toContain", function() {
it("delegates to jasmineUnderTest.matchersUtil.contains", function() {
var util = {
describe('toContain', function() {
it('delegates to jasmineUnderTest.matchersUtil.contains', function() {
var matchersUtil = {
contains: jasmine.createSpy('delegated-contains').and.returnValue(true)
},
matcher = jasmineUnderTest.matchers.toContain(util),
matcher = jasmineUnderTest.matchers.toContain(matchersUtil),
result;
result = matcher.compare("ABC", "B");
expect(util.contains).toHaveBeenCalledWith("ABC", "B", []);
result = matcher.compare('ABC', 'B');
expect(matchersUtil.contains).toHaveBeenCalledWith('ABC', 'B');
expect(result.pass).toBe(true);
});
it("delegates to jasmineUnderTest.matchersUtil.contains, passing in equality testers if present", function() {
var util = {
contains: jasmine.createSpy('delegated-contains').and.returnValue(true)
it('works with custom equality testers', function() {
var tester = function(a, b) {
return a.toString() === b.toString();
},
customEqualityTesters = ['a', 'b'],
matcher = jasmineUnderTest.matchers.toContain(util, customEqualityTesters),
matchersUtil = new jasmineUnderTest.MatchersUtil({
customTesters: [tester]
}),
matcher = jasmineUnderTest.matchers.toContain(matchersUtil),
result;
result = matcher.compare("ABC", "B");
expect(util.contains).toHaveBeenCalledWith("ABC", "B", ['a', 'b']);
result = matcher.compare(['1', '2'], 2);
expect(result.pass).toBe(true);
});
});

File diff suppressed because it is too large Load Diff

View File

@@ -1,63 +1,77 @@
describe("toHaveBeenCalledBefore", function() {
it("throws an exception when the actual is not a spy", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
fn = function() {},
secondSpy = new jasmineUnderTest.Env().createSpy('second spy');
describe('toHaveBeenCalledBefore', function() {
it('throws an exception when the actual is not a spy', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {},
spy = new jasmineUnderTest.Env().createSpy('a spy');
expect(function() { matcher.compare(fn, secondSpy) }).toThrowError(Error, /Expected a spy, but got Function./);
expect(function() {
matcher.compare(fn, spy);
}).toThrowError(Error, /Expected a spy, but got Function./);
});
it("throws an exception when the expected is not a spy", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
fn = function() {};
it('throws an exception when the expected is not a spy', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore({
pp: jasmineUnderTest.makePrettyPrinter()
}),
spy = new jasmineUnderTest.Env().createSpy('a spy'),
fn = function() {};
expect(function() { matcher.compare(firstSpy, fn) }).toThrowError(Error, /Expected a spy, but got Function./);
expect(function() {
matcher.compare(spy, fn);
}).toThrowError(Error, /Expected a spy, but got Function./);
});
it("fails when the actual was not called", function() {
it('fails when the actual was not called', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy');
firstSpy = new jasmineUnderTest.Spy('first spy'),
secondSpy = new jasmineUnderTest.Spy('second spy');
secondSpy();
result = matcher.compare(firstSpy, secondSpy);
expect(result.pass).toBe(false);
expect(result.message).toMatch(/Expected spy first spy to have been called./);
expect(result.message).toMatch(
/Expected spy first spy to have been called./
);
});
it("fails when the expected was not called", function() {
it('fails when the expected was not called', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy');
firstSpy = new jasmineUnderTest.Spy('first spy'),
secondSpy = new jasmineUnderTest.Spy('second spy');
firstSpy();
result = matcher.compare(firstSpy, secondSpy);
expect(result.pass).toBe(false);
expect(result.message).toMatch(/Expected spy second spy to have been called./);
expect(result.message).toMatch(
/Expected spy second spy to have been called./
);
});
it("fails when the actual is called after the expected", function() {
it('fails when the actual is called after the expected', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy'),
result;
firstSpy = new jasmineUnderTest.Spy('first spy'),
secondSpy = new jasmineUnderTest.Spy('second spy'),
result;
secondSpy();
firstSpy();
result = matcher.compare(firstSpy, secondSpy);
expect(result.pass).toBe(false);
expect(result.message).toEqual('Expected spy first spy to have been called before spy second spy');
expect(result.message).toEqual(
'Expected spy first spy to have been called before spy second spy'
);
});
it("fails when the actual is called before and after the expected", function() {
it('fails when the actual is called before and after the expected', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy'),
result;
firstSpy = new jasmineUnderTest.Spy('first spy'),
secondSpy = new jasmineUnderTest.Spy('second spy'),
result;
firstSpy();
secondSpy();
@@ -65,14 +79,16 @@ describe("toHaveBeenCalledBefore", function() {
result = matcher.compare(firstSpy, secondSpy);
expect(result.pass).toBe(false);
expect(result.message).toEqual('Expected latest call to spy first spy to have been called before first call to spy second spy (no interleaved calls)');
expect(result.message).toEqual(
'Expected latest call to spy first spy to have been called before first call to spy second spy (no interleaved calls)'
);
});
it("fails when the expected is called before and after the actual", function() {
it('fails when the expected is called before and after the actual', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy'),
result;
firstSpy = new jasmineUnderTest.Spy('first spy'),
secondSpy = new jasmineUnderTest.Spy('second spy'),
result;
secondSpy();
firstSpy();
@@ -80,20 +96,24 @@ describe("toHaveBeenCalledBefore", function() {
result = matcher.compare(firstSpy, secondSpy);
expect(result.pass).toBe(false);
expect(result.message).toEqual('Expected first call to spy second spy to have been called after latest call to spy first spy (no interleaved calls)');
expect(result.message).toEqual(
'Expected first call to spy second spy to have been called after latest call to spy first spy (no interleaved calls)'
);
});
it("passes when the actual is called before the expected", function() {
it('passes when the actual is called before the expected', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(),
firstSpy = new jasmineUnderTest.Env().createSpy('first spy'),
secondSpy = new jasmineUnderTest.Env().createSpy('second spy'),
result;
firstSpy = new jasmineUnderTest.Spy('first spy'),
secondSpy = new jasmineUnderTest.Spy('second spy'),
result;
firstSpy();
secondSpy();
result = matcher.compare(firstSpy, secondSpy);
expect(result.pass).toBe(true);
expect(result.message).toEqual('Expected spy first spy to not have been called before spy second spy, but it was');
expect(result.message).toEqual(
'Expected spy first spy to not have been called before spy second spy, but it was'
);
});
});

View File

@@ -0,0 +1,114 @@
describe('toHaveBeenCalledOnceWith', function() {
it('passes when the actual was called only once and with matching parameters', function() {
var pp = jasmineUnderTest.makePrettyPrinter(),
util = new jasmineUnderTest.MatchersUtil({ pp: pp }),
matcher = jasmineUnderTest.matchers.toHaveBeenCalledOnceWith(util),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
calledSpy('a', 'b');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(true);
expect(result.message).toEqual(
"Expected spy called-spy to have been called 0 times, multiple times, or once, but with arguments different from:\n [ 'a', 'b' ]\nBut the actual call was:\n [ 'a', 'b' ].\n\n"
);
});
it('supports custom equality testers', function() {
var customEqualityTesters = [
function() {
return true;
}
],
matchersUtil = new jasmineUnderTest.MatchersUtil({
customTesters: customEqualityTesters
}),
matcher = jasmineUnderTest.matchers.toHaveBeenCalledOnceWith(
matchersUtil
),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
calledSpy('a', 'b');
result = matcher.compare(calledSpy, 'a', 'a');
expect(result.pass).toBe(true);
});
it('fails when the actual was never called', function() {
var pp = jasmineUnderTest.makePrettyPrinter(),
util = new jasmineUnderTest.MatchersUtil({ pp: pp }),
matcher = jasmineUnderTest.matchers.toHaveBeenCalledOnceWith(util),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(false);
expect(result.message).toEqual(
"Expected spy called-spy to have been called only once, and with given args:\n [ 'a', 'b' ]\nBut it was never called.\n\n"
);
});
it('fails when the actual was called once with different parameters', function() {
var pp = jasmineUnderTest.makePrettyPrinter(),
util = new jasmineUnderTest.MatchersUtil({ pp: pp }),
matcher = jasmineUnderTest.matchers.toHaveBeenCalledOnceWith(util),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
calledSpy('a', 'c');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(false);
expect(result.message).toEqual(
"Expected spy called-spy to have been called only once, and with given args:\n [ 'a', 'b' ]\nBut the actual call was:\n [ 'a', 'c' ].\nExpected $[1] = 'c' to equal 'b'.\n\n"
);
});
it('fails when the actual was called multiple times with expected parameters', function() {
var pp = jasmineUnderTest.makePrettyPrinter(),
util = new jasmineUnderTest.MatchersUtil({ pp: pp }),
matcher = jasmineUnderTest.matchers.toHaveBeenCalledOnceWith(util),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
calledSpy('a', 'b');
calledSpy('a', 'b');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(false);
expect(result.message).toEqual(
"Expected spy called-spy to have been called only once, and with given args:\n [ 'a', 'b' ]\nBut the actual calls were:\n [ 'a', 'b' ],\n [ 'a', 'b' ].\n\n"
);
});
it('fails when the actual was called multiple times (one of them - with expected parameters)', function() {
var pp = jasmineUnderTest.makePrettyPrinter(),
util = new jasmineUnderTest.MatchersUtil({ pp: pp }),
matcher = jasmineUnderTest.matchers.toHaveBeenCalledOnceWith(util),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
calledSpy('a', 'b');
calledSpy('a', 'c');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(false);
expect(result.message).toEqual(
"Expected spy called-spy to have been called only once, and with given args:\n [ 'a', 'b' ]\nBut the actual calls were:\n [ 'a', 'b' ],\n [ 'a', 'c' ].\n\n"
);
});
it('throws an exception when the actual is not a spy', function() {
var pp = jasmineUnderTest.makePrettyPrinter(),
util = new jasmineUnderTest.MatchersUtil({ pp: pp }),
matcher = jasmineUnderTest.matchers.toHaveBeenCalledOnceWith(util),
fn = function() {};
expect(function() {
matcher.compare(fn);
}).toThrowError(/Expected a spy, but got Function./);
});
});

View File

@@ -1,47 +1,56 @@
describe("toHaveBeenCalled", function() {
it("passes when the actual was called, with a custom .not fail message", function() {
describe('toHaveBeenCalled', function() {
it('passes when the actual was called, with a custom .not fail message', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled(),
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy'),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
calledSpy();
result = matcher.compare(calledSpy);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected spy called-spy not to have been called.");
expect(result.message).toEqual(
'Expected spy called-spy not to have been called.'
);
});
it("fails when the actual was not called", function() {
it('fails when the actual was not called', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled(),
uncalledSpy = new jasmineUnderTest.Env().createSpy('uncalled spy'),
uncalledSpy = new jasmineUnderTest.Spy('uncalled spy'),
result;
result = matcher.compare(uncalledSpy);
expect(result.pass).toBe(false);
});
it("throws an exception when the actual is not a spy", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled(),
it('throws an exception when the actual is not a spy', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {};
expect(function() { matcher.compare(fn) }).toThrowError(Error, /Expected a spy, but got Function./);
expect(function() {
matcher.compare(fn);
}).toThrowError(Error, /Expected a spy, but got Function./);
});
it("throws an exception when invoked with any arguments", function() {
it('throws an exception when invoked with any arguments', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled(),
spy = new jasmineUnderTest.Env().createSpy('sample spy');
spy = new jasmineUnderTest.Spy('sample spy');
expect(function() { matcher.compare(spy, 'foo') }).toThrowError(Error, /Does not take arguments, use toHaveBeenCalledWith/);
expect(function() {
matcher.compare(spy, 'foo');
}).toThrowError(Error, /Does not take arguments, use toHaveBeenCalledWith/);
});
it("has a custom message on failure", function() {
it('has a custom message on failure', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalled(),
spy = new jasmineUnderTest.Env().createSpy('sample-spy'),
spy = new jasmineUnderTest.Spy('sample-spy'),
result;
result = matcher.compare(spy);
expect(result.message).toEqual("Expected spy sample-spy to have been called.");
expect(result.message).toEqual(
'Expected spy sample-spy to have been called.'
);
});
});

View File

@@ -1,14 +1,14 @@
describe("toHaveBeenCalledTimes", function() {
it("passes when the actual 0 matches the expected 0 ", function () {
describe('toHaveBeenCalledTimes', function() {
it('passes when the actual 0 matches the expected 0 ', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy'),
result;
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
result = matcher.compare(calledSpy, 0);
expect(result.pass).toBeTruthy();
});
it("passes when the actual matches the expected", function() {
it('passes when the actual matches the expected', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy'),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
calledSpy();
@@ -16,29 +16,31 @@ describe("toHaveBeenCalledTimes", function() {
expect(result.pass).toBe(true);
});
it("fails when expected numbers is not supplied", function(){
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
spy = new jasmineUnderTest.Env().createSpy('spy'),
it('fails when expected numbers is not supplied', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
spy = new jasmineUnderTest.Spy('spy'),
result;
spy();
expect(function() {
matcher.compare(spy);
}).toThrowError(/The expected times failed is a required argument and must be a number./);
expect(function() {
matcher.compare(spy);
}).toThrowError(
/The expected times failed is a required argument and must be a number./
);
});
it("fails when the actual was called less than the expected", function() {
it('fails when the actual was called less than the expected', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
uncalledSpy = new jasmineUnderTest.Env().createSpy('uncalled spy'),
uncalledSpy = new jasmineUnderTest.Spy('uncalled spy'),
result;
result = matcher.compare(uncalledSpy, 2);
expect(result.pass).toBe(false);
});
it("fails when the actual was called more than expected", function() {
it('fails when the actual was called more than expected', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
uncalledSpy = new jasmineUnderTest.Env().createSpy('uncalled spy'),
uncalledSpy = new jasmineUnderTest.Spy('uncalled spy'),
result;
uncalledSpy();
@@ -48,8 +50,10 @@ describe("toHaveBeenCalledTimes", function() {
expect(result.pass).toBe(false);
});
it("throws an exception when the actual is not a spy", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
it('throws an exception when the actual is not a spy', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {};
expect(function() {
@@ -57,9 +61,9 @@ describe("toHaveBeenCalledTimes", function() {
}).toThrowError(/Expected a spy, but got Function./);
});
it("has a custom message on failure that tells it was called only once", function() {
it('has a custom message on failure that tells it was called only once', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
spy = new jasmineUnderTest.Env().createSpy('sample-spy'),
spy = new jasmineUnderTest.Spy('sample-spy'),
result;
spy();
spy();
@@ -67,12 +71,16 @@ describe("toHaveBeenCalledTimes", function() {
spy();
result = matcher.compare(spy, 1);
expect(result.message).toEqual('Expected spy sample-spy to have been called once. It was called ' + 4 + ' times.');
expect(result.message).toEqual(
'Expected spy sample-spy to have been called once. It was called ' +
4 +
' times.'
);
});
it("has a custom message on failure that tells how many times it was called", function() {
it('has a custom message on failure that tells how many times it was called', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledTimes(),
spy = new jasmineUnderTest.Env().createSpy('sample-spy'),
spy = new jasmineUnderTest.Spy('sample-spy'),
result;
spy();
spy();
@@ -80,7 +88,10 @@ describe("toHaveBeenCalledTimes", function() {
spy();
result = matcher.compare(spy, 2);
expect(result.message).toEqual('Expected spy sample-spy to have been called 2 times. It was called ' + 4 + ' times.');
expect(result.message).toEqual(
'Expected spy sample-spy to have been called 2 times. It was called ' +
4 +
' times.'
);
});
});

View File

@@ -1,67 +1,99 @@
describe("toHaveBeenCalledWith", function() {
it("passes when the actual was called with matching parameters", function() {
var util = {
contains: jasmine.createSpy('delegated-contains').and.returnValue(true)
},
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(util),
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy'),
result;
describe('toHaveBeenCalledWith', function() {
it('passes when the actual was called with matching parameters', function() {
var matchersUtil = {
contains: jasmine.createSpy('delegated-contains').and.returnValue(true),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(matchersUtil),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
calledSpy('a', 'b');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected spy called-spy not to have been called with [ 'a', 'b' ] but it was.");
expect(result.message()).toEqual(
"Expected spy called-spy not to have been called with:\n [ 'a', 'b' ]\nbut it was."
);
});
it("passes through the custom equality testers", function() {
var util = {
contains: jasmine.createSpy('delegated-contains').and.returnValue(true)
},
customEqualityTesters = [function() { return true; }],
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(util, customEqualityTesters),
calledSpy = new jasmineUnderTest.Env().createSpy('called-spy');
it('supports custom equality testers', function() {
var customEqualityTesters = [
function() {
return true;
}
],
matchersUtil = new jasmineUnderTest.MatchersUtil({
customTesters: customEqualityTesters
}),
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(matchersUtil),
calledSpy = new jasmineUnderTest.Spy('called-spy'),
result;
calledSpy('a', 'b');
matcher.compare(calledSpy, 'a', 'b');
expect(util.contains).toHaveBeenCalledWith([['a', 'b']], ['a', 'b'], customEqualityTesters);
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(true);
});
it("fails when the actual was not called", function() {
var util = {
contains: jasmine.createSpy('delegated-contains').and.returnValue(false)
},
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(util),
uncalledSpy = new jasmineUnderTest.Env().createSpy('uncalled spy'),
result;
it('fails when the actual was not called', function() {
var matchersUtil = {
contains: jasmine
.createSpy('delegated-contains')
.and.returnValue(false),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(matchersUtil),
uncalledSpy = new jasmineUnderTest.Spy('uncalled spy'),
result;
result = matcher.compare(uncalledSpy);
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected spy uncalled spy to have been called with [ ] but it was never called.");
expect(result.message()).toEqual(
'Expected spy uncalled spy to have been called with:\n [ ]\nbut it was never called.'
);
});
it("fails when the actual was called with different parameters", function() {
var util = {
contains: jasmine.createSpy('delegated-contains').and.returnValue(false)
},
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(util),
calledSpy = new jasmineUnderTest.Env().createSpy('called spy'),
result;
it('fails when the actual was called with different parameters', function() {
var matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(matchersUtil),
calledSpy = new jasmineUnderTest.Spy('called spy'),
result;
calledSpy('a');
calledSpy('c', 'd');
calledSpy('a', 'b', 'c');
result = matcher.compare(calledSpy, 'a', 'b');
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected spy called spy to have been called with [ 'a', 'b' ] but actual calls were [ 'a' ], [ 'c', 'd' ].");
expect(result.message()).toEqual(
'Expected spy called spy to have been called with:\n' +
" [ 'a', 'b' ]\n" +
'but actual calls were:\n' +
" [ 'a' ],\n" +
" [ 'c', 'd' ],\n" +
" [ 'a', 'b', 'c' ].\n\n" +
'Call 0:\n' +
' Expected $.length = 1 to equal 2.\n' +
" Expected $[1] = undefined to equal 'b'.\n" +
'Call 1:\n' +
" Expected $[0] = 'c' to equal 'a'.\n" +
" Expected $[1] = 'd' to equal 'b'.\n" +
'Call 2:\n' +
' Expected $.length = 3 to equal 2.\n' +
" Unexpected $[2] = 'c' in array."
);
});
it("throws an exception when the actual is not a spy", function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(),
fn = function() {};
it('throws an exception when the actual is not a spy', function() {
var matcher = jasmineUnderTest.matchers.toHaveBeenCalledWith({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {};
expect(function() { matcher.compare(fn) }).toThrowError(/Expected a spy, but got Function./);
expect(function() {
matcher.compare(fn);
}).toThrowError(/Expected a spy, but got Function./);
});
});

View File

@@ -1,30 +1,21 @@
describe('toHaveClass', function() {
beforeEach(function() {
this.createElementWithClassName = function(className) {
var el = this.doc.createElement('div');
el.className = className;
return el;
};
if (typeof document !== 'undefined') {
this.doc = document;
} else {
var JSDOM = require('jsdom').JSDOM;
var dom = new JSDOM();
this.doc = dom.window.document;
}
this.domHelpers = jasmine.getEnv().domHelpers();
});
it('fails for a DOM element that lacks the expected class', function() {
var matcher = jasmineUnderTest.matchers.toHaveClass(),
result = matcher.compare(this.createElementWithClassName(''), 'foo');
result = matcher.compare(
this.domHelpers.createElementWithClassName(''),
'foo'
);
expect(result.pass).toBe(false);
});
it('passes for a DOM element that has the expected class', function() {
var matcher = jasmineUnderTest.matchers.toHaveClass(),
el = this.createElementWithClassName('foo bar baz');
el = this.domHelpers.createElementWithClassName('foo bar baz');
expect(matcher.compare(el, 'foo').pass).toBe(true);
expect(matcher.compare(el, 'bar').pass).toBe(true);
@@ -33,13 +24,15 @@ describe('toHaveClass', function() {
it('fails for a DOM element that only has other classes', function() {
var matcher = jasmineUnderTest.matchers.toHaveClass(),
el = this.createElementWithClassName('foo bar');
el = this.domHelpers.createElementWithClassName('foo bar');
expect(matcher.compare(el, 'fo').pass).toBe(false);
});
it('throws an exception when actual is not a DOM element', function() {
var matcher = jasmineUnderTest.matchers.toHaveClass();
var matcher = jasmineUnderTest.matchers.toHaveClass({
pp: jasmineUnderTest.makePrettyPrinter()
});
expect(function() {
matcher.compare('x', 'foo');
@@ -49,13 +42,13 @@ describe('toHaveClass', function() {
matcher.compare(undefined, 'foo');
}).toThrowError('undefined is not a DOM element');
var textNode = this.doc.createTextNode('');
var textNode = this.domHelpers.document.createTextNode('');
expect(function() {
matcher.compare(textNode, 'foo')
matcher.compare(textNode, 'foo');
}).toThrowError('HTMLNode is not a DOM element');
expect(function() {
matcher.compare({classList: ''}, 'foo');
matcher.compare({ classList: '' }, 'foo');
}).toThrowError("Object({ classList: '' }) is not a DOM element");
});
});

View File

@@ -0,0 +1,138 @@
/* eslint-disable compat/compat */
describe('toHaveSize', function() {
'use strict';
it('passes for an array whose length matches', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare([1, 2], 2);
expect(result.pass).toBe(true);
});
it('fails for an array whose length does not match', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare([1, 2, 3], 2);
expect(result.pass).toBe(false);
});
it('passes for an object with the proper number of keys', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare({ a: 1, b: 2 }, 2);
expect(result.pass).toBe(true);
});
it('fails for an object with a different number of keys', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare({ a: 1, b: 2 }, 1);
expect(result.pass).toBe(false);
});
it('passes for an object with an explicit `length` property that matches', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare({ a: 1, b: 2, length: 5 }, 5);
expect(result.pass).toBe(true);
});
it('fails for an object with an explicit `length` property that does not match', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare({ a: 1, b: 2, length: 5 }, 1);
expect(result.pass).toBe(false);
});
it('passes for a string whose length matches', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare('ab', 2);
expect(result.pass).toBe(true);
});
it('fails for a string whose length does not match', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare('abc', 2);
expect(result.pass).toBe(false);
});
it('passes for a Map whose length matches', function() {
jasmine.getEnv().requireFunctioningMaps();
var map = new Map();
map.set('a', 1);
map.set('b', 2);
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare(map, 2);
expect(result.pass).toBe(true);
});
it('fails for a Map whose length does not match', function() {
jasmine.getEnv().requireFunctioningMaps();
var map = new Map();
map.set('a', 1);
map.set('b', 2);
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare(map, 1);
expect(result.pass).toBe(false);
});
it('passes for a Set whose length matches', function() {
jasmine.getEnv().requireFunctioningSets();
var set = new Set();
set.add('a');
set.add('b');
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare(set, 2);
expect(result.pass).toBe(true);
});
it('fails for a Set whose length does not match', function() {
jasmine.getEnv().requireFunctioningSets();
var set = new Set();
set.add('a');
set.add('b');
var matcher = jasmineUnderTest.matchers.toHaveSize(),
result = matcher.compare(set, 1);
expect(result.pass).toBe(false);
});
it('throws an error for WeakSet', function() {
jasmine.getEnv().requireWeakSets();
var matcher = jasmineUnderTest.matchers.toHaveSize();
expect(function() {
matcher.compare(new WeakSet(), 2);
}).toThrowError('Cannot get size of [object WeakSet].');
});
it('throws an error for WeakMap', function() {
jasmine.getEnv().requireWeakMaps();
var matcher = jasmineUnderTest.matchers.toHaveSize();
expect(function() {
matcher.compare(new WeakMap(), 2);
}).toThrowError(/Cannot get size of \[object (WeakMap|Object)\]\./);
});
it('throws an error for DataView', function() {
var matcher = jasmineUnderTest.matchers.toHaveSize();
expect(function() {
matcher.compare(new DataView(new ArrayBuffer(128)), 2);
}).toThrowError(/Cannot get size of \[object (DataView|Object)\]\./);
});
});

View File

@@ -1,6 +1,5 @@
describe("toMatch", function() {
it("passes when RegExps are equivalent", function() {
describe('toMatch', function() {
it('passes when RegExps are equivalent', function() {
var matcher = jasmineUnderTest.matchers.toMatch(),
result;
@@ -8,7 +7,7 @@ describe("toMatch", function() {
expect(result.pass).toBe(true);
});
it("fails when RegExps are not equivalent", function() {
it('fails when RegExps are not equivalent', function() {
var matcher = jasmineUnderTest.matchers.toMatch(),
result;
@@ -16,7 +15,7 @@ describe("toMatch", function() {
expect(result.pass).toBe(false);
});
it("passes when the actual matches the expected string as a pattern", function() {
it('passes when the actual matches the expected string as a pattern', function() {
var matcher = jasmineUnderTest.matchers.toMatch(),
result;
@@ -24,7 +23,7 @@ describe("toMatch", function() {
expect(result.pass).toBe(true);
});
it("fails when the actual matches the expected string as a pattern", function() {
it('fails when the actual matches the expected string as a pattern', function() {
var matcher = jasmineUnderTest.matchers.toMatch(),
result;
@@ -32,7 +31,7 @@ describe("toMatch", function() {
expect(result.pass).toBe(false);
});
it("throws an Error when the expected is not a String or RegExp", function() {
it('throws an Error when the expected is not a String or RegExp', function() {
var matcher = jasmineUnderTest.matchers.toMatch();
expect(function() {
@@ -40,4 +39,3 @@ describe("toMatch", function() {
}).toThrowError(/Expected is not a String or a RegExp/);
});
});

View File

@@ -1,5 +1,5 @@
describe("toThrowError", function() {
it("throws an error when the actual is not a function", function() {
describe('toThrowError', function() {
it('throws an error when the actual is not a function', function() {
var matcher = jasmineUnderTest.matchers.toThrowError();
expect(function() {
@@ -7,10 +7,10 @@ describe("toThrowError", function() {
}).toThrowError(/Actual is not a Function/);
});
it("throws an error when the expected is not an Error, string, or RegExp", function() {
it('throws an error when the expected is not an Error, string, or RegExp', function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
fn = function() {
throw new Error("foo");
throw new Error('foo');
};
expect(function() {
@@ -18,21 +18,21 @@ describe("toThrowError", function() {
}).toThrowError(/Expected is not an Error, string, or RegExp./);
});
it("throws an error when the expected error type is not an Error", function() {
it('throws an error when the expected error type is not an Error', function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
fn = function() {
throw new Error("foo");
throw new Error('foo');
};
expect(function() {
matcher.compare(fn, void 0, "foo");
matcher.compare(fn, void 0, 'foo');
}).toThrowError(/Expected error type is not an Error./);
});
it("throws an error when the expected error message is not a string or RegExp", function() {
it('throws an error when the expected error message is not a string or RegExp', function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
fn = function() {
throw new Error("foo");
throw new Error('foo');
};
expect(function() {
@@ -40,7 +40,7 @@ describe("toThrowError", function() {
}).toThrowError(/Expected error message is not a string or RegExp./);
});
it("fails if actual does not throw at all", function() {
it('fails if actual does not throw at all', function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
fn = function() {
return true;
@@ -50,11 +50,13 @@ describe("toThrowError", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an Error.");
expect(result.message).toEqual('Expected function to throw an Error.');
});
it("fails if thrown is not an instanceof Error", function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
it('fails if thrown is not an instanceof Error', function() {
var matcher = jasmineUnderTest.matchers.toThrowError({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {
throw 4;
},
@@ -62,12 +64,14 @@ describe("toThrowError", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw an Error, but it threw 4.");
expect(result.message()).toEqual(
'Expected function to throw an Error, but it threw 4.'
);
});
describe("when error is from another frame", function() {
describe('when error is from another frame', function() {
function isNotRunningInBrowser() {
return typeof document === 'undefined'
return typeof document === 'undefined';
}
var iframe = null;
@@ -78,32 +82,39 @@ describe("toThrowError", function() {
}
});
it("passes if thrown is an instanceof Error regardless of global that contains its constructor", function() {
it('passes if thrown is an instanceof Error regardless of global that contains its constructor', function() {
if (isNotRunningInBrowser()) {
return;
}
var matcher = jasmineUnderTest.matchers.toThrowError();
iframe = document.body.appendChild(document.createElement("iframe"));
iframe.src = "about:blank";
iframe = document.body.appendChild(document.createElement('iframe'));
iframe.src = 'about:blank';
var iframeDocument = iframe.contentWindow.document;
if (iframeDocument.body) {
iframeDocument.body.appendChild(iframeDocument.createElement("script"))
.textContent = "function method() { throw new Error('foo'); }";
iframeDocument.body.appendChild(
iframeDocument.createElement('script')
).textContent = "function method() { throw new Error('foo'); }";
} else {
// IE 10 and older
iframeDocument.write("<html><head><script>function method() { throw new Error('foo'); }</script></head></html>");
iframeDocument.write(
"<html><head><script>function method() { throw new Error('foo'); }</script></head></html>"
);
}
var result = matcher.compare(iframe.contentWindow.method);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an Error, but it threw Error.");
expect(result.message).toEqual(
'Expected function not to throw an Error, but it threw Error.'
);
});
});
it("fails with the correct message if thrown is a falsy value", function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
it('fails with the correct message if thrown is a falsy value', function() {
var matcher = jasmineUnderTest.matchers.toThrowError({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {
throw undefined;
},
@@ -111,10 +122,12 @@ describe("toThrowError", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw an Error, but it threw undefined.");
expect(result.message()).toEqual(
'Expected function to throw an Error, but it threw undefined.'
);
});
it("passes if thrown is a type of Error, but there is no expected error", function() {
it('passes if thrown is a type of Error, but there is no expected error', function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
fn = function() {
throw new TypeError();
@@ -124,66 +137,81 @@ describe("toThrowError", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an Error, but it threw TypeError.");
expect(result.message).toEqual(
'Expected function not to throw an Error, but it threw TypeError.'
);
});
it("passes if thrown is an Error and the expected is the same message", function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
it('passes if thrown is an Error and the expected is the same message', function() {
var matcher = jasmineUnderTest.matchers.toThrowError({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {
throw new Error("foo");
throw new Error('foo');
},
result;
result = matcher.compare(fn, "foo");
result = matcher.compare(fn, 'foo');
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw an exception with message 'foo'.");
expect(result.message()).toEqual(
"Expected function not to throw an exception with message 'foo'."
);
});
it("fails if thrown is an Error and the expected is not the same message", function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
it('fails if thrown is an Error and the expected is not the same message', function() {
var matcher = jasmineUnderTest.matchers.toThrowError({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {
throw new Error("foo");
throw new Error('foo');
},
result;
result = matcher.compare(fn, "bar");
result = matcher.compare(fn, 'bar');
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw an exception with message 'bar', but it threw an exception with message 'foo'.");
expect(result.message()).toEqual(
"Expected function to throw an exception with message 'bar', but it threw an exception with message 'foo'."
);
});
it("passes if thrown is an Error and the expected is a RegExp that matches the message", function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
it('passes if thrown is an Error and the expected is a RegExp that matches the message', function() {
var matcher = jasmineUnderTest.matchers.toThrowError({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {
throw new Error("a long message");
throw new Error('a long message');
},
result;
result = matcher.compare(fn, /long/);
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw an exception with a message matching /long/.");
expect(result.message()).toEqual(
'Expected function not to throw an exception with a message matching /long/.'
);
});
it("fails if thrown is an Error and the expected is a RegExp that does not match the message", function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
it('fails if thrown is an Error and the expected is a RegExp that does not match the message', function() {
var matcher = jasmineUnderTest.matchers.toThrowError({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {
throw new Error("a long message");
throw new Error('a long message');
},
result;
result = matcher.compare(fn, /foo/);
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw an exception with a message matching /foo/, but it threw an exception with message 'a long message'.");
expect(result.message()).toEqual(
"Expected function to throw an exception with a message matching /foo/, but it threw an exception with message 'a long message'."
);
});
it("passes if thrown is an Error and the expected the same Error", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
},
matcher = jasmineUnderTest.matchers.toThrowError(),
it('passes if thrown is an Error and the expected the same Error', function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
fn = function() {
throw new Error();
},
@@ -192,15 +220,14 @@ describe("toThrowError", function() {
result = matcher.compare(fn, Error);
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw Error.");
expect(result.message()).toEqual('Expected function not to throw Error.');
});
it("passes if thrown is a custom error that takes arguments and the expected is the same error", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
it('passes if thrown is a custom error that takes arguments and the expected is the same error', function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
CustomError = function CustomError(arg) {
arg.x;
},
matcher = jasmineUnderTest.matchers.toThrowError(),
CustomError = function CustomError(arg) { arg.x },
fn = function() {
throw new CustomError({ x: 1 });
},
@@ -211,14 +238,13 @@ describe("toThrowError", function() {
result = matcher.compare(fn, CustomError);
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw CustomError.");
expect(result.message()).toEqual(
'Expected function not to throw CustomError.'
);
});
it("fails if thrown is an Error and the expected is a different Error", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false)
},
matcher = jasmineUnderTest.matchers.toThrowError(),
it('fails if thrown is an Error and the expected is a different Error', function() {
var matcher = jasmineUnderTest.matchers.toThrowError(),
fn = function() {
throw new Error();
},
@@ -227,89 +253,108 @@ describe("toThrowError", function() {
result = matcher.compare(fn, TypeError);
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw TypeError, but it threw Error.");
expect(result.message()).toEqual(
'Expected function to throw TypeError, but it threw Error.'
);
});
it("passes if thrown is a type of Error and it is equal to the expected Error and message", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
it('passes if thrown is a type of Error and it is equal to the expected Error and message', function() {
var matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrowError(),
matcher = jasmineUnderTest.matchers.toThrowError(matchersUtil),
fn = function() {
throw new TypeError("foo");
throw new TypeError('foo');
},
result;
result = matcher.compare(fn, TypeError, "foo");
result = matcher.compare(fn, TypeError, 'foo');
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw TypeError with message 'foo'.");
expect(result.message()).toEqual(
"Expected function not to throw TypeError with message 'foo'."
);
});
it("passes if thrown is a custom error that takes arguments and it is equal to the expected custom error and message", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
it('passes if thrown is a custom error that takes arguments and it is equal to the expected custom error and message', function() {
var matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrowError(matchersUtil),
CustomError = function CustomError(arg) {
this.message = arg.message;
},
matcher = jasmineUnderTest.matchers.toThrowError(),
CustomError = function CustomError(arg) { this.message = arg.message },
fn = function() {
throw new CustomError({message: "foo"});
throw new CustomError({ message: 'foo' });
},
result;
CustomError.prototype = new Error();
result = matcher.compare(fn, CustomError, "foo");
result = matcher.compare(fn, CustomError, 'foo');
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw CustomError with message 'foo'.");
expect(result.message()).toEqual(
"Expected function not to throw CustomError with message 'foo'."
);
});
it("fails if thrown is a type of Error and the expected is a different Error", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false)
it('fails if thrown is a type of Error and the expected is a different Error', function() {
var matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrowError(),
matcher = jasmineUnderTest.matchers.toThrowError(matchersUtil),
fn = function() {
throw new TypeError("foo");
throw new TypeError('foo');
},
result;
result = matcher.compare(fn, TypeError, "bar");
result = matcher.compare(fn, TypeError, 'bar');
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw TypeError with message 'bar', but it threw TypeError with message 'foo'.");
expect(result.message()).toEqual(
"Expected function to throw TypeError with message 'bar', but it threw TypeError with message 'foo'."
);
});
it("passes if thrown is a type of Error and has the same type as the expected Error and the message matches the expected message", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
it('passes if thrown is a type of Error and has the same type as the expected Error and the message matches the expected message', function() {
var matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrowError(),
matcher = jasmineUnderTest.matchers.toThrowError(matchersUtil),
fn = function() {
throw new TypeError("foo");
throw new TypeError('foo');
},
result;
result = matcher.compare(fn, TypeError, /foo/);
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw TypeError with a message matching /foo/.");
expect(result.message()).toEqual(
'Expected function not to throw TypeError with a message matching /foo/.'
);
});
it("fails if thrown is a type of Error and the expected is a different Error", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false)
it('fails if thrown is a type of Error and the expected is a different Error', function() {
var matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrowError(),
matcher = jasmineUnderTest.matchers.toThrowError(matchersUtil),
fn = function() {
throw new TypeError("foo");
throw new TypeError('foo');
},
result;
result = matcher.compare(fn, TypeError, /bar/);
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw TypeError with a message matching /bar/, but it threw TypeError with message 'foo'.");
expect(result.message()).toEqual(
"Expected function to throw TypeError with a message matching /bar/, but it threw TypeError with message 'foo'."
);
});
});

View File

@@ -1,16 +1,18 @@
describe("toThrowMatching", function() {
it("throws an error when the actual is not a function", function() {
describe('toThrowMatching', function() {
it('throws an error when the actual is not a function', function() {
var matcher = jasmineUnderTest.matchers.toThrowMatching();
expect(function() {
matcher.compare({}, function() { return true; });
matcher.compare({}, function() {
return true;
});
}).toThrowError(/Actual is not a Function/);
});
it("throws an error when the expected is not a function", function() {
it('throws an error when the expected is not a function', function() {
var matcher = jasmineUnderTest.matchers.toThrowMatching(),
fn = function() {
throw new Error("foo");
throw new Error('foo');
};
expect(function() {
@@ -18,56 +20,74 @@ describe("toThrowMatching", function() {
}).toThrowError(/Predicate is not a Function/);
});
it("fails if actual does not throw at all", function() {
it('fails if actual does not throw at all', function() {
var matcher = jasmineUnderTest.matchers.toThrowMatching(),
fn = function() {
return true;
},
result;
result = matcher.compare(fn, function() { return true; });
result = matcher.compare(fn, function() {
return true;
});
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception.");
expect(result.message).toEqual('Expected function to throw an exception.');
});
it("fails with the correct message if thrown is a falsy value", function() {
var matcher = jasmineUnderTest.matchers.toThrowMatching(),
it('fails with the correct message if thrown is a falsy value', function() {
var matcher = jasmineUnderTest.matchers.toThrowMatching({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {
throw undefined;
},
result;
result = matcher.compare(fn, function() { return false; });
result = matcher.compare(fn, function() {
return false;
});
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw an exception matching a predicate, but it threw undefined.");
expect(result.message()).toEqual(
'Expected function to throw an exception matching a predicate, but it threw undefined.'
);
});
it("passes if the argument is a function that returns true when called with the error", function() {
it('passes if the argument is a function that returns true when called with the error', function() {
var matcher = jasmineUnderTest.matchers.toThrowMatching(),
predicate = function(e) { return e.message === "nope" },
predicate = function(e) {
return e.message === 'nope';
},
fn = function() {
throw new TypeError("nope");
throw new TypeError('nope');
},
result;
result = matcher.compare(fn, predicate);
expect(result.pass).toBe(true);
expect(result.message).toEqual("Expected function not to throw an exception matching a predicate.");
expect(result.message).toEqual(
'Expected function not to throw an exception matching a predicate.'
);
});
it("fails if the argument is a function that returns false when called with the error", function() {
var matcher = jasmineUnderTest.matchers.toThrowMatching(),
predicate = function(e) { return e.message === "oh no" },
it('fails if the argument is a function that returns false when called with the error', function() {
var matcher = jasmineUnderTest.matchers.toThrowMatching({
pp: jasmineUnderTest.makePrettyPrinter()
}),
predicate = function(e) {
return e.message === 'oh no';
},
fn = function() {
throw new TypeError("nope");
throw new TypeError('nope');
},
result;
result = matcher.compare(fn, predicate);
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw an exception matching a predicate, but it threw TypeError with message 'nope'.");
expect(result.message()).toEqual(
"Expected function to throw an exception matching a predicate, but it threw TypeError with message 'nope'."
);
});
});

View File

@@ -1,6 +1,5 @@
describe("toThrow", function() {
it("throws an error when the actual is not a function", function() {
describe('toThrow', function() {
it('throws an error when the actual is not a function', function() {
var matcher = jasmineUnderTest.matchers.toThrow();
expect(function() {
@@ -9,7 +8,7 @@ describe("toThrow", function() {
}).toThrowError(/Actual is not a Function/);
});
it("fails if actual does not throw", function() {
it('fails if actual does not throw', function() {
var matcher = jasmineUnderTest.matchers.toThrow(),
fn = function() {
return true;
@@ -19,14 +18,15 @@ describe("toThrow", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(false);
expect(result.message).toEqual("Expected function to throw an exception.");
expect(result.message).toEqual('Expected function to throw an exception.');
});
it("passes if it throws but there is no expected", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
it('passes if it throws but there is no expected', function() {
var matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrow(util),
matcher = jasmineUnderTest.matchers.toThrow(matchersUtil),
fn = function() {
throw 5;
},
@@ -35,11 +35,15 @@ describe("toThrow", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw, but it threw 5.");
expect(result.message()).toEqual(
'Expected function not to throw, but it threw 5.'
);
});
it("passes even if what is thrown is falsy", function() {
var matcher = jasmineUnderTest.matchers.toThrow(),
it('passes even if what is thrown is falsy', function() {
var matcher = jasmineUnderTest.matchers.toThrow({
pp: jasmineUnderTest.makePrettyPrinter()
}),
fn = function() {
throw undefined;
},
@@ -47,14 +51,17 @@ describe("toThrow", function() {
result = matcher.compare(fn);
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw, but it threw undefined.");
expect(result.message()).toEqual(
'Expected function not to throw, but it threw undefined.'
);
});
it("passes if what is thrown is equivalent to what is expected", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true)
it('passes if what is thrown is equivalent to what is expected', function() {
var matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(true),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrow(util),
matcher = jasmineUnderTest.matchers.toThrow(matchersUtil),
fn = function() {
throw 5;
},
@@ -63,30 +70,34 @@ describe("toThrow", function() {
result = matcher.compare(fn, 5);
expect(result.pass).toBe(true);
expect(result.message()).toEqual("Expected function not to throw 5.");
expect(result.message()).toEqual('Expected function not to throw 5.');
});
it("fails if what is thrown is not equivalent to what is expected", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false)
it('fails if what is thrown is not equivalent to what is expected', function() {
var matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrow(util),
matcher = jasmineUnderTest.matchers.toThrow(matchersUtil),
fn = function() {
throw 5;
},
result;
result = matcher.compare(fn, "foo");
result = matcher.compare(fn, 'foo');
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw 'foo', but it threw 5.");
expect(result.message()).toEqual(
"Expected function to throw 'foo', but it threw 5."
);
});
it("fails if what is thrown is not equivalent to undefined", function() {
var util = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false)
it('fails if what is thrown is not equivalent to undefined', function() {
var matchersUtil = {
equals: jasmine.createSpy('delegated-equal').and.returnValue(false),
pp: jasmineUnderTest.makePrettyPrinter()
},
matcher = jasmineUnderTest.matchers.toThrow(util),
matcher = jasmineUnderTest.matchers.toThrow(matchersUtil),
fn = function() {
throw 5;
},
@@ -95,6 +106,8 @@ describe("toThrow", function() {
result = matcher.compare(fn, void 0);
expect(result.pass).toBe(false);
expect(result.message()).toEqual("Expected function to throw undefined, but it threw 5.");
expect(result.message()).toEqual(
'Expected function to throw undefined, but it threw 5.'
);
});
});