expect
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -35,2 +35,9 @@ var assert = require('assert'); | ||
/** | ||
* Returns true if the given string contains the value, false otherwise. | ||
*/ | ||
function stringContains(string, value) { | ||
return string.indexOf(value) !== -1; | ||
} | ||
/** | ||
* An Expectation is a wrapper around an assertion that allows it to be written | ||
@@ -67,5 +74,10 @@ * in a more natural style, without the need to remember the order of arguments. | ||
Expectation.prototype.toBeA = function (constructor, message) { | ||
assert(isFunction(constructor), 'The constructor used in toBeA/toBeAn must be a function'); | ||
assert( | ||
isFunction(constructor), | ||
'The constructor used in toBeA/toBeAn must be a function' | ||
); | ||
message = message || inspect(this.actual) + ' is not a ' + (constructor.name || constructor.toString()); | ||
assert(this.actual instanceof constructor, message); | ||
return this; | ||
@@ -75,5 +87,10 @@ }; | ||
Expectation.prototype.toMatch = function (pattern, message) { | ||
assert(isRegExp(pattern), 'The pattern used in toMatch must be a RegExp'); | ||
assert( | ||
isRegExp(pattern), | ||
'The pattern used in toMatch must be a RegExp' | ||
); | ||
message = message || inspect(this.actual) + ' does not match ' + inspect(pattern); | ||
assert(pattern.test(this.actual), message); | ||
return this; | ||
@@ -95,3 +112,6 @@ }; | ||
Expectation.prototype.toInclude = function (value, comparator, message) { | ||
assert(isArray(this.actual), 'The actual value used in toInclude/toContain must be an Array'); | ||
assert( | ||
isArray(this.actual) || typeof this.actual === 'string', | ||
'The actual value used in toInclude/toContain must be an Array or String' | ||
); | ||
@@ -105,6 +125,14 @@ if (typeof comparator === 'string') { | ||
assert( | ||
arrayContains(this.actual, value, comparator), | ||
message | ||
); | ||
if (isArray(this.actual)) { | ||
assert( | ||
arrayContains(this.actual, value, comparator), | ||
message | ||
); | ||
} else { | ||
assert( | ||
stringContains(this.actual, value), | ||
message | ||
); | ||
} | ||
return this; | ||
@@ -114,3 +142,6 @@ }; | ||
Expectation.prototype.toExclude = function (value, comparator, message) { | ||
assert(isArray(this.actual), 'The actual value used in toExclude/toNotContain must be an Array'); | ||
assert( | ||
isArray(this.actual) || typeof this.actual === 'string', | ||
'The actual value used in toExclude/toNotContain must be an Array or String' | ||
); | ||
@@ -123,7 +154,15 @@ if (typeof comparator === 'string') { | ||
message = message || inspect(this.actual) + ' includes ' + inspect(value); | ||
assert( | ||
!arrayContains(this.actual, value, comparator), | ||
message | ||
); | ||
if (isArray(this.actual)) { | ||
assert( | ||
!arrayContains(this.actual, value, comparator), | ||
message | ||
); | ||
} else { | ||
assert( | ||
!stringContains(this.actual, value), | ||
message | ||
); | ||
} | ||
return this; | ||
@@ -130,0 +169,0 @@ }; |
{ | ||
"name": "expect", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Write better assertions", | ||
@@ -8,6 +8,16 @@ "main": "modules/expect.js", | ||
"devDependencies": { | ||
"mocha": "~1.17.1" | ||
"jshint": "^2.5.10", | ||
"karma": "^0.12.28", | ||
"karma-chrome-launcher": "^0.1.7", | ||
"karma-cli": "0.0.4", | ||
"karma-mocha": "^0.1.10", | ||
"karma-sourcemap-loader": "^0.3.2", | ||
"karma-webpack": "^1.3.1", | ||
"mocha": "^2.0.1", | ||
"webpack": "^1.4.13" | ||
}, | ||
"scripts": { | ||
"test": "mocha \"modules/**/__tests__/**/*-test.js\"" | ||
"dist": "webpack modules/expect.js dist/expect.min.js", | ||
"test": "jshint . && mocha --reporter spec 'modules/**/__tests__/*-test.js'", | ||
"test-browser": "karma start" | ||
}, | ||
@@ -14,0 +24,0 @@ "keywords": [ |
@@ -59,2 +59,3 @@ [![npm package](https://img.shields.io/npm/v/expect.svg?style=flat-square)](https://www.npmjs.org/package/expect) | ||
#### expect(object).toBeA(constructor, [message]) | ||
#### expect(object).toBeAn(constructor, [message]) | ||
@@ -65,2 +66,3 @@ Asserts the given `object` is an `instanceof constructor`. | ||
expect(new User).toBeA(User); | ||
expect(new Asset).toBeAn(Asset); | ||
``` | ||
@@ -77,2 +79,3 @@ | ||
#### expect(number).toBeLessThan(value, [message]) | ||
#### expect(number).toBeFewerThan(value, [message]) | ||
@@ -86,2 +89,3 @@ Asserts the given `number` is less than `value`. | ||
#### expect(number).toBeGreaterThan(value, [message]) | ||
#### expect(number).toBeMoreThan(value, [message]) | ||
@@ -95,2 +99,3 @@ Asserts the given `number` is greater than `value`. | ||
#### expect(array).toInclude(value, [comparator], [message]) | ||
#### expect(array).toContain(value, [comparator], [message]) | ||
@@ -104,2 +109,3 @@ 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]) | ||
@@ -112,2 +118,22 @@ 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]) | ||
Asserts the given `string` contains `value`. | ||
```js | ||
expect('hello world').toInclude('world'); | ||
expect('hello world').toContain('world'); | ||
``` | ||
#### expect(string).toExclude(value, [message]) | ||
#### expect(string).toNotContain(value, [message]) | ||
Asserts the given `string` does not contain `value`. | ||
```js | ||
expect('hello world').toExclude('goodbye'); | ||
expect('hello world').toNotContain('goodbye'); | ||
``` | ||
### Chaining Assertions | ||
@@ -130,2 +156,8 @@ | ||
Or, include `dist/expect.min.js` in your page using a `<script>` tag: | ||
```html | ||
<script src="expect.min.js"></script> | ||
``` | ||
### Issues | ||
@@ -142,4 +174,9 @@ | ||
To run the tests in Chrome: | ||
$ npm install | ||
$ npm run test-browser | ||
### License | ||
[MIT](http://opensource.org/licenses/MIT) |
Sorry, the diff of this file is not supported yet
11836
6
179
174
9