测试 expressJS 的基本中间件
Testing a basic middleware of expressJS
有一个非常基础的中间件,我想测试一下。
第一个问题是我使用的是哪种测试。据我了解,我无法为此代码编写单元测试。
我会称之为集成测试。对吗?
第二个问题是测试本身:我 运行 超时了,尽管我已经使用了 done()
。
我究竟做错了什么?这是测试这个中间件的正确方法吗?
/middlewares/graphql.js
module.exports = (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
if (req.is('application/graphql')) {
req.body = { query: req.body }
}
if (req.method === 'OPTIONS') {
res.sendStatus(200)
} else {
next()
}
}
/tests/middlewares/graphql.js
import nodeMocks from 'node-mocks-http'
import middleware from '../middlewares/graphql'
describe('GraphQL middleware', () => {
it('Should return 200 for valid Content-Type header', (done) => {
const req = nodeMocks.createRequest({
headers: {
'Content-Type': 'application/graphql'
},
body: {
content: 'anything'
},
method: 'OPTIONS'
})
const res = nodeMocks.createResponse()
middleware(req, res, (err) => {
expect(res.statusCode).toEqual(200)
expect(res.body.content).toEqual('anything')
expect(err).toBeNull()
done()
})
})
})
middleware
是以(req, res, next)
为参数的函数。您发送 req
、res
和处理断言的回调。 IE。断言测试作为 next()
.
传入
但是当你传入OPTIONS
作为请求方法时,没有理由调用next()
。然后中间件将执行 res.sendStatus(200)
。因此,您必须将中间件作为普通函数来调用(对于此特定测试)。
middleware(req, res);
expect(res.statusCode).to.equal(200);
expect(res.body.content).to.equal('anything');
done();
除了res.body
,它会失败,但那是因为中间件函数是这样写的。
只要你只测试中间件功能,我就称之为单元测试。如果您关心 next()
被调用后会发生什么,我会称之为集成测试。不过叫什么并不重要,只要通过测试就可以了。
当中间件必须调用next()
时,您可以看到测试答案。
有一个非常基础的中间件,我想测试一下。
第一个问题是我使用的是哪种测试。据我了解,我无法为此代码编写单元测试。 我会称之为集成测试。对吗?
第二个问题是测试本身:我 运行 超时了,尽管我已经使用了 done()
。
我究竟做错了什么?这是测试这个中间件的正确方法吗?
/middlewares/graphql.js
module.exports = (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With')
if (req.is('application/graphql')) {
req.body = { query: req.body }
}
if (req.method === 'OPTIONS') {
res.sendStatus(200)
} else {
next()
}
}
/tests/middlewares/graphql.js
import nodeMocks from 'node-mocks-http'
import middleware from '../middlewares/graphql'
describe('GraphQL middleware', () => {
it('Should return 200 for valid Content-Type header', (done) => {
const req = nodeMocks.createRequest({
headers: {
'Content-Type': 'application/graphql'
},
body: {
content: 'anything'
},
method: 'OPTIONS'
})
const res = nodeMocks.createResponse()
middleware(req, res, (err) => {
expect(res.statusCode).toEqual(200)
expect(res.body.content).toEqual('anything')
expect(err).toBeNull()
done()
})
})
})
middleware
是以(req, res, next)
为参数的函数。您发送 req
、res
和处理断言的回调。 IE。断言测试作为 next()
.
但是当你传入OPTIONS
作为请求方法时,没有理由调用next()
。然后中间件将执行 res.sendStatus(200)
。因此,您必须将中间件作为普通函数来调用(对于此特定测试)。
middleware(req, res);
expect(res.statusCode).to.equal(200);
expect(res.body.content).to.equal('anything');
done();
除了res.body
,它会失败,但那是因为中间件函数是这样写的。
只要你只测试中间件功能,我就称之为单元测试。如果您关心 next()
被调用后会发生什么,我会称之为集成测试。不过叫什么并不重要,只要通过测试就可以了。
当中间件必须调用next()
时,您可以看到