Comparing version 0.8.3 to 0.9.0
0.9.0 / 2014-01-17 | ||
================== | ||
* add expect(function(res) {}) syntax | ||
0.8.3 / 2014-01-07 | ||
@@ -4,0 +8,0 @@ ================== |
@@ -36,2 +36,3 @@ | ||
this._bodies = []; | ||
this._asserts = []; | ||
this.url = 'string' == typeof app | ||
@@ -75,2 +76,3 @@ ? app + path | ||
* .expect('Content-Type', 'application/json', fn) | ||
* .expect(fn) | ||
* | ||
@@ -85,2 +87,6 @@ * @return {Test} | ||
// callback | ||
if ('function' == typeof a) { | ||
this._asserts.push(a); | ||
return this; | ||
} | ||
if ('function' == typeof b) this.end(b); | ||
@@ -197,2 +203,15 @@ if ('function' == typeof c) this.end(c); | ||
// asserts | ||
for (var i = 0; i < this._asserts.length; i++) { | ||
var check = this._asserts[i]; | ||
var err; | ||
try { | ||
err = check(res); | ||
} catch(e) { | ||
err = e; | ||
} | ||
if (!err) continue; | ||
return fn(err instanceof Error ? err : new Error(err)) | ||
} | ||
fn.call(this, null, res); | ||
@@ -199,0 +218,0 @@ }; |
{ | ||
"name": "supertest", | ||
"version": "0.8.3", | ||
"version": "0.9.0", | ||
"description": "Super-agent driven library for testing HTTP servers", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -120,2 +120,20 @@ # SuperTest | ||
### .expect(function(res) {}) | ||
Pass a custom assertion function. It'll be given the response object to check. If the response is ok, it should return falsy, most commonly by not returning anything. If the check fails, throw an error or return a truthy value like a string that'll be turned into an error. | ||
Here the string or error throwing options are both demonstrated: | ||
```js | ||
request(app) | ||
.get('/') | ||
.expect(hasPreviousAndNextKeys) | ||
.end(done); | ||
function hasPreviousAndNextKeys(res) { | ||
if (!('next' in res.body)) return "missing next key"; | ||
if (!('prev' in res.body)) throw new Error("missing prev key"); | ||
} | ||
``` | ||
### .end(fn) | ||
@@ -122,0 +140,0 @@ |
@@ -403,2 +403,102 @@ | ||
describe('handling arbitrary expect functions', function(){ | ||
it('reports errors',function(done) { | ||
var app = express(); | ||
app.get('/', function(req, res){ | ||
res.send('hey'); | ||
}); | ||
request(app) | ||
.get('/') | ||
.expect(function(res) { | ||
throw new Error("failed") | ||
}) | ||
.end(function(err) { | ||
err.message.should.equal('failed'); | ||
done() | ||
}); | ||
}); | ||
it('ensures truthy non-errors returned from asserts are promoted to errors',function(done){ | ||
var app = express(); | ||
app.get('/', function(req, res){ | ||
res.send('hey'); | ||
}); | ||
request(app) | ||
.get('/') | ||
.expect(function(res) { | ||
return "some descriptive error"; | ||
}) | ||
.end(function(err) { | ||
err.message.should.equal('some descriptive error'); | ||
done() | ||
}); | ||
}); | ||
it("doesn't create false negatives", function(done){ | ||
var app = express(); | ||
app.get('/', function(req, res){ | ||
res.send('hey'); | ||
}); | ||
request(app) | ||
.get('/') | ||
.expect(function(res) {}) | ||
.end(done); | ||
}); | ||
it("handles multiple asserts", function(done){ | ||
var app = express(); | ||
app.get('/', function(req, res){ | ||
res.send('hey'); | ||
}); | ||
var calls = []; | ||
request(app) | ||
.get('/') | ||
.expect(function(res) { calls[0] = 1 }) | ||
.expect(function(res) { calls[1] = 1 }) | ||
.expect(function(res) { calls[2] = 1 }) | ||
.end(function() { | ||
var callCount = [0,1,2].reduce(function(count,i) { | ||
return count + calls[i] | ||
},0); | ||
callCount.should.equal(3,"didn't see all assertions run"); | ||
done(); | ||
}); | ||
}); | ||
it("plays well with normal assertions - no false positives", function(done){ | ||
var app = express(); | ||
app.get('/', function(req, res){ | ||
res.send('hey'); | ||
}); | ||
request(app) | ||
.get('/') | ||
.expect(function(res) {}) | ||
.expect('Content-Type', /json/) | ||
.end(function(err) { | ||
err.message.should.match(/Content-Type/); | ||
done(); | ||
}) | ||
}); | ||
it("plays well with normal assertions - no false negatives", function(done){ | ||
var app = express(); | ||
app.get('/', function(req, res){ | ||
res.send('hey'); | ||
}); | ||
request(app) | ||
.get('/') | ||
.expect(function(res) {}) | ||
.expect('Content-Type', /html/) | ||
.expect(function(res) {}) | ||
.expect('Content-Type', /text/) | ||
.end(done) | ||
}); | ||
}); | ||
describe('handling multiple assertions per field', function(){ | ||
@@ -405,0 +505,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
30257
786
149