【问题标题】:Unit testing in NodejsNodejs 中的单元测试
【发布时间】:2020-08-25 18:54:33
【问题描述】:

我有一个简单的 hello world 应用程序,我想为它编写一个测试。我是 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}`)
})

【问题讨论】:

    标签: node.js unit-testing npm


    【解决方案1】:

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

    【讨论】:

    • 你能告诉我怎么写吗?
    【解决方案2】:

    在终端;

    npm i supertest
    npm i chai
    

    然后示例 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()
                });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2018-10-01
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      相关资源
      最近更新 更多