@JunQiu
2018-09-18T17:55:20.000000Z
字数 4011
阅读 1173
summary_2018/06
think
git
npm
git push origin HEAD:master
或者
git push origin“your_git_repo”:“your_branch_name”
或者
git push origin master - force
const request = require('supertest');
const express = require('express');
//获取app实例
const app = express();
app.get('/user', function(req, res) {
res.status(200).json({ name: 'john' });
});
//agent=request(app)
const agent=request.agent(app)
test('test',()=>{
agent.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '15')
.expect()
.end(function(err, res) {
if (err) throw err;
});
})
describe('POST /users', function() {
it('responds with json', function(done) {
request(app)
.post('/users')
.send({name: 'john'})
.set('Accept', 'application/json')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
//done()结束嵌套的回调函数,一个测试用例只能调用一次
done();
});
});
});
describe('POST /user', function() {
it('user.name should be an case-insensitive match for "john"', function(done) {
request(app)
.post('/user')
.send('name=john') // x-www-form-urlencoded upload
.set('Accept', 'application/json')
.expect(function(res) {
res.body.id = 'some fixed id';
res.body.name = res.body.name.toUpperCase();
})
.expect(200, {
id: 'some fixed id',
name: 'john'
}, done);
});
});
const agent = request.agent(app);
it('should save cookies', function(done) {
agent
.get('/')
.expect('set-cookie', 'cookie=hey; Path=/', done);
});
it('should send cookies', function(done) {
agent
.get('/return')
.expect('hey', done);
});
.expect(status[, fn])
Assert response status code.
.expect(status, body[, fn])
Assert response status code and body.
.expect(body[, fn])
Assert response body text with a string, regular expression, or parsed body object.
.expect(field, value[, fn])
Assert header field value with a string or regular expression.
.expect(function(res) {})
Pass a custom assertion function. It'll be given the response object to check. If the check fails, throw an error.
.end(fn)
Perform the request and invoke fn(err, res).
/ Don't do this!
test('the data is peanut butter', () => {
function callback(data) {
expect(data).toBe('peanut butter');
}
fetchData(callback);
});
默认情况下,一旦Jest测试到达执行结束,它就会完成测试。但是问题是测试会在fetchData完成之前立即完成,然后再调用回调,不会按预期进行回调。