【发布时间】:2018-02-09 18:14:42
【问题描述】:
我正在正确地重构我的express-decorator NPM 包的克隆。这包括重构之前使用AVA 完成的the unit tests。我决定使用Mocha 和Chai 重写它们,因为我更喜欢它们定义测试的方式。
那么,我的问题是什么?看看这段代码(我分解了来说明问题):
test('express', (t) => {
@web.basePath('/test')
class Test {
@web.get('/foo/:id')
foo(request, response) {
/* The test in question. */
t.is(parseInt(request.params.id), 5);
response.send();
}
}
let app = express();
let controller = new Test();
web.register(app, controller);
t.plan(1);
return supertest(app)
.get('/test/foo/5')
.expect(200);
});
此代码有效。
这是(基本上)相同的代码,现在使用 Mocha 和 Chai 以及多个测试:
describe('The test express server', () => {
@web.basePath('/test')
class Test {
@web.get('/foo/:id')
foo(request, response) {
/* The test in question. */
it('should pass TEST #1',
() => expect(toInteger(request.params.id)).to.equal(5))
response.send()
}
}
const app = express()
const controller = new Test()
web.register(app, controller)
it('should pass TEST #2', (done) => {
return chai.request(app)
.get('/test/foo/5')
.end((err, res) => {
expect(err).to.be.null
expect(res).to.have.status(200)
done()
})
})
})
问题是 TEST #1 被 Mocha 忽略了,尽管这部分代码在测试期间运行。我尝试在那里console.log 一些东西,它出现在我希望它出现的摩卡日志中。
那么我如何让该测试发挥作用?我的想法是以某种方式将上下文(测试套件)传递给it 函数,但对于 Mocha,这是不可能的,不是吗?
【问题讨论】:
标签: node.js unit-testing typescript mocha.js chai