【问题标题】:Express + Mocha + Chai Error: Server is not listeningExpress + Mocha + Chai 错误:服务器未在监听
【发布时间】:2020-12-23 06:37:26
【问题描述】:

我正在编写一个简单的 TypeScript Express 应用程序,它可以获取有关 YouTube 视频的信息。这是路由器(安装到/api):

import express from 'express';
import ytdl    from 'ytdl-core';
import bodyParser from 'body-parser';

const router = express.Router();

router.post('/info', bodyParser.json(), async (req, res, next) => {
    const videoID = req.body.videoID

    if (!ytdl.validateID(videoID)) {
        const error = new Error('Invalid video ID')
        res.status(400).json({error: error.toString()}).send;
        next();
    } else {
        const videoFormats = await (await ytdl.getInfo(videoID)).formats;
        res.setHeader('Content-type', 'application/json');
        res.send(videoFormats);
    } 
});

export default router;

今天我尝试用 Mocha + Chai 编写我的第一个测试。 api/info 端点需要 POST,从正文中获取 videoID 并以 JSON 响应。到目前为止,这是我对端点的测试。

import app from '../index'  
import chaiHttp from 'chai-http'
import chai from 'chai'

chai.use(chaiHttp);

const expect = chai.expect;
const get    = chai.request(app).get;
const post   = chai.request(app).post;

describe('API', () => {
    describe('POST /api/info', () => {
        it('given videoID of a publically available video, responds with 200 OK and JSON that contains an array of objects',  async () => {
            const res = await post('/api/info')
                                .send({"videoID": "OrxmtDw4pVI"});

            expect(res).to.have.status(200);
            expect(res.body).to.be.an('array');            
        }).timeout(5000);

        it('given an invalid videoID, responds with 400 and an Error', async () => {
            const res = await post('/api/info')
                                .send({"videoID": "qwerty"});

            expect(res).to.have.status(400);
        });
    });
});

测试结果如下:

API
    POST /api/info
      √ given videoID of a publically available video, responds with 200 OK and JSON that contains an array of objects (1028ms)
      1) given an invalid videoID, responds with 400 and an Error


  1 passing (1s)
  1 failing

  1) API
       POST /api/info
         given an invalid videoID, responds with 400 and an Error:
     Error: Server is not listening
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! ytb--down@1.0.0 test: `mocha -r ts-node/register src/**/*.spec.ts --exit`
npm ERR! Exit status 1

如您所见,第二次测试由于Server is not listening 错误而失败。如果我skip 第一次测试,它不会发生

我使用 Postman 使用此数据手动测试此端点,没有发现会导致服务器崩溃的问题,也没有引发错误。

我使用npm run test 命令启动测试,这是来自package.json 的脚本:

"scripts": {
    "test": "mocha -r ts-node/register src/**/*.spec.ts --exit"

我怀疑测试套件本身有问题,但我无法确定解决方案。我在这里错过了什么?

【问题讨论】:

    标签: node.js typescript express mocha.js chai


    【解决方案1】:

    我想我找到了。

    似乎我的测试确实包含一个错误,对于像我这样的初学者来说并不是一个明显的错误。

    想要像这样获得getpost 快捷方式:

    const get    = chai.request(app).get;
    const post   = chai.request(app).post;
    

    我认为真正的错误是我在这里调用了request()。相反,应该为chai.request 找到一个快捷方式:

    const request = chai.request;
    

    现在测试工作正常。测试文件现在如下所示。

    import server from '../index'  
    import chaiHttp from 'chai-http'
    import chai from 'chai'
    
    chai.use(chaiHttp);
    const expect  = chai.expect;
    const request = chai.request;
    
    describe('API', () => {
        
        describe('POST /api/info', () => {
            it('given videoID of a publically available video, responds with 200 and JSON that contains an array of objects',  async () => {
                const res = await request(server)
                                    .post('/api/info')
                                    .send({"videoID": "OrxmtDw4pVI"});
    
                expect(res).to.have.status(200);
                expect(res.body).to.be.an('array');            
            });
    
            it('given an invalid videoID, responds with 400 and an Error', async () => {
                const res = await request(server)
                                    .post('/api/info')
                                    .send({"videoID": "qwerty"});
    
                expect(res).to.have.status(400);
                expect(res.body.error).to.include('Error');
            });
        });
    });
    
    

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 1970-01-01
      • 2020-06-06
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多