Comparing version 1.4.0 to 1.5.0
109
index.js
var assert = require('assert'); | ||
var inspect = require('util').inspect; | ||
var isRegExp = require('util').isRegExp; | ||
var formatString = require('util').format; | ||
var isArray = Array.isArray; | ||
@@ -34,2 +35,4 @@ | ||
var expect = Expectation; | ||
/** | ||
@@ -76,3 +79,3 @@ * Returns true if the given string contains the value, false otherwise. | ||
isFunction(value) || typeof value === 'string', | ||
'The expected value used in toBeA/toBeAn must be a function or string' | ||
'The expected value used in toBeA(n) must be a function or string' | ||
); | ||
@@ -171,2 +174,106 @@ | ||
Expectation.prototype.toHaveBeenCalled = function (message) { | ||
var spy = this.actual; | ||
assert( | ||
spy.__isSpy, | ||
'The actual value used in toHaveBeenCalled must be a spy' | ||
); | ||
expect(spy.calls.length).toBeGreaterThan(0, message || 'spy was not called'); | ||
return this; | ||
}; | ||
Expectation.prototype.toHaveBeenCalledWith = function () { | ||
var spy = this.actual; | ||
assert( | ||
spy.__isSpy, | ||
'The actual value used in toHaveBeenCalledWith must be a spy' | ||
); | ||
var expectedArguments = Array.prototype.slice.call(arguments, 0); | ||
assert( | ||
spy.calls.some(function (call) { | ||
try { | ||
expect(call.arguments).toEqual(expectedArguments); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}), | ||
formatString('spy was never called with %s', inspect(expectedArguments)) | ||
); | ||
return this; | ||
}; | ||
Expectation.createSpy = createSpy; | ||
function createSpy(fn) { | ||
expect(fn).toBeA(Function); | ||
var targetFn, thrownValue, returnValue; | ||
var spy = function () { | ||
spy.calls.push({ | ||
context: this, | ||
arguments: Array.prototype.slice.call(arguments, 0) | ||
}); | ||
if (targetFn) | ||
return targetFn.apply(this, arguments); | ||
if (thrownValue) | ||
throw thrownValue; | ||
return returnValue; | ||
}; | ||
spy.calls = []; | ||
spy.andCall = function (fn) { | ||
targetFn = fn; | ||
return spy; | ||
}; | ||
spy.andCallThrough = function () { | ||
return spy.andCall(fn); | ||
}; | ||
spy.andThrow = function (object) { | ||
thrownValue = object; | ||
return spy; | ||
}; | ||
spy.andReturn = function (value) { | ||
returnValue = value; | ||
return spy; | ||
}; | ||
spy.getLastCall = function () { | ||
return spy.calls[spy.calls.length - 1]; | ||
}; | ||
spy.__isSpy = true; | ||
return spy; | ||
} | ||
Expectation.spyOn = spyOn; | ||
function spyOn(object, methodName) { | ||
expect(object[methodName]).toBeA( | ||
Function, | ||
formatString('%s does not have a method named "%s"', inspect(object), methodName) | ||
); | ||
if (!object[methodName].__isSpy) | ||
object[methodName] = createSpy(object[methodName]); | ||
return object[methodName]; | ||
} | ||
var aliases = { | ||
@@ -173,0 +280,0 @@ toBeAn: 'toBeA', |
{ | ||
"name": "expect", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Write better assertions", | ||
@@ -5,0 +5,0 @@ "main": "expect.js", |
@@ -12,19 +12,19 @@ [![npm package](https://img.shields.io/npm/v/expect.svg?style=flat-square)](https://www.npmjs.org/package/expect) | ||
#### expect(object).toBe(value, [message]) | ||
##### expect(object).toBe(value, [message]) | ||
Asserts that `object` is strictly equal to `value` using [assert.strictEqual](http://nodejs.org/api/assert.html#assert_assert_strictequal_actual_expected_message). | ||
#### expect(object).toNotBe(value, [message]) | ||
##### expect(object).toNotBe(value, [message]) | ||
Asserts that `object` is not strictly equal to `value` using [assert.notStrictEqual](http://nodejs.org/api/assert.html#assert_assert_notstrictequal_actual_expected_message). | ||
#### expect(object).toEqual(value, [message]) | ||
##### expect(object).toEqual(value, [message]) | ||
Asserts that the given `object` equals `value` using [assert.equal](http://nodejs.org/api/assert.html#assert_assert_equal_actual_expected_message). | ||
#### expect(object).toNotEqual(value, [message]) | ||
##### expect(object).toNotEqual(value, [message]) | ||
Asserts that the given `object` is not equal to `value` using [assert.notEqual](http://nodejs.org/api/assert.html#assert_assert_notequal_actual_expected_message). | ||
#### expect(block).toThrow([error], [message]) | ||
##### expect(block).toThrow([error], [message]) | ||
@@ -39,7 +39,7 @@ Asserts that the given `block` throws an error using [assert.throws](http://nodejs.org/api/assert.html#assert_assert_throws_block_error_message). The `error` argument may be a constructor, `RegExp`, or validation function. | ||
#### expect(block).toNotThrow([message]) | ||
##### expect(block).toNotThrow([message]) | ||
Asserts that the given `block` does not throw using [assert.doesNotThrow](http://nodejs.org/api/assert.html#assert_assert_doesnotthrow_block_message). | ||
#### expect(object).toExist([message]) | ||
##### expect(object).toExist([message]) | ||
@@ -52,3 +52,3 @@ Asserts the given `object` is truthy. | ||
#### expect(object).toNotExist([message]) | ||
##### expect(object).toNotExist([message]) | ||
@@ -61,4 +61,4 @@ Asserts the given `object` is falsy. | ||
#### expect(object).toBeA(constructor, [message]) | ||
#### expect(object).toBeAn(constructor, [message]) | ||
##### expect(object).toBeA(constructor, [message]) | ||
##### expect(object).toBeAn(constructor, [message]) | ||
@@ -72,3 +72,3 @@ Asserts the given `object` is an `instanceof constructor`. | ||
#### expect(object).toBeA(string, [message]) | ||
##### expect(object).toBeA(string, [message]) | ||
@@ -81,3 +81,3 @@ Asserts the `typeof` the given `object` is `string`. | ||
#### expect(string).toMatch(pattern, [message]) | ||
##### expect(string).toMatch(pattern, [message]) | ||
@@ -90,4 +90,4 @@ Asserts the given `string` matches `pattern`, which must be a `RegExp`. | ||
#### expect(number).toBeLessThan(value, [message]) | ||
#### expect(number).toBeFewerThan(value, [message]) | ||
##### expect(number).toBeLessThan(value, [message]) | ||
##### expect(number).toBeFewerThan(value, [message]) | ||
@@ -100,4 +100,4 @@ Asserts the given `number` is less than `value`. | ||
#### expect(number).toBeGreaterThan(value, [message]) | ||
#### expect(number).toBeMoreThan(value, [message]) | ||
##### expect(number).toBeGreaterThan(value, [message]) | ||
##### expect(number).toBeMoreThan(value, [message]) | ||
@@ -110,4 +110,4 @@ Asserts the given `number` is greater than `value`. | ||
#### expect(array).toInclude(value, [comparator], [message]) | ||
#### expect(array).toContain(value, [comparator], [message]) | ||
##### expect(array).toInclude(value, [comparator], [message]) | ||
##### expect(array).toContain(value, [comparator], [message]) | ||
@@ -120,4 +120,4 @@ Asserts the given `array` contains `value`. The `comparator` function, if given, should compare two objects and either `return false` or `throw` if they are not equal. It defaults to `assert.deepEqual`. | ||
#### expect(array).toExclude(value, [comparator], [message]) | ||
#### expect(array).toNotContain(value, [comparator], [message]) | ||
##### expect(array).toExclude(value, [comparator], [message]) | ||
##### expect(array).toNotContain(value, [comparator], [message]) | ||
@@ -130,4 +130,4 @@ Asserts the given `array` does not contain `value`. The `comparator` function, if given, should compare two objects and either `return false` or `throw` if they are not equal. It defaults to `assert.deepEqual`. | ||
#### expect(string).toInclude(value, [message]) | ||
#### expect(string).toContain(value, [message]) | ||
##### expect(string).toInclude(value, [message]) | ||
##### expect(string).toContain(value, [message]) | ||
@@ -141,4 +141,4 @@ Asserts the given `string` contains `value`. | ||
#### expect(string).toExclude(value, [message]) | ||
#### expect(string).toNotContain(value, [message]) | ||
##### expect(string).toExclude(value, [message]) | ||
##### expect(string).toNotContain(value, [message]) | ||
@@ -152,2 +152,24 @@ Asserts the given `string` does not contain `value`. | ||
### Spies | ||
expect.js also includes the ability to create spy functions that can track the calls that are made to other functions and make various assertions based on the arguments and context that were used. | ||
```js | ||
var video = { | ||
play: function () {}, | ||
pause: function () {}, | ||
rewind: function () {} | ||
}; | ||
var spy = expect.spyOn(video, 'play'); | ||
video.play('some', 'args'); | ||
expect(spy.calls.length).toEqual(1); | ||
expect(spy.calls[0].context).toBe(video); | ||
expect(spy.calls[0].arguments).toEqual([ 'some', 'args' ]); | ||
expect(spy).toHaveBeenCalled(); | ||
expect(spy).toHaveBeenCalledWith('some', 'args'); | ||
``` | ||
### Chaining Assertions | ||
@@ -154,0 +176,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14252
227
204
0