简体   繁体   English

Nodejs 中的单元测试

[英]Unit testing in Nodejs

I have this simple hello world app, I want to write a test for it.我有这个简单的 hello world 应用程序,我想为它编写一个测试。 I am new to Nodejs so want help in getting started我是 Nodejs 的新手,所以需要入门帮助

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World!')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

您可以使用 jest 和 supertest 编写测试。

in terminal;在终端;

npm i supertest
npm i chai

then sample api test is below ;然后示例api测试如下;

describe('Hello World Api Test', () => {
    it('should return 200', (done) => {
        request(app)
            .get('/')
            .expect(200)
            .end(function (err, res) {
                if (err) throw err;
                console.log(res);
                expect(res.text).to.equal('Hello world!');
                done()
            });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM