#node.js #unit-testing #mocha.js #web-api-testing
Вопрос:
Я прочитал документ мокко, но не могу следовать инструкции. Потому что у меня есть такой Экспресс-класс
// ExpressServer.js
const http = require('http');
const express = require('express');
class ExpressServer {
constructor(port, openApiYaml) {
this.port = port;
this.app = express();
}
launch() {
http.createServer(this.app).listen(this.port);
}
}
// setUpMocha.js
const { ExpressServer } = require('../expressServer');
const config = require('../config');
let expressServer;
const mochaGlobalSetup = async () => {
expressServer = new ExpressServer(config.URL_PORT, config.OPENAPI_YAML);
expressServer.launch();
};
module.exports = {
mochaGlobalSetup,
expressServer
}
// тестАПИс
enter code here
const chai = require('chai');
const chaiHttp = require('chai-http');
const { expressServer } = require('./setUp');
const configTest = require('./configTest');
describe('Test all public APIS', function(){
it('should get all diagrams belong to any site', function() {
chai.request(expressServer.app)
.get('/site/diagrams')
.set('authorization', configTest.accessToken)
.end(function(err, res){
console.log(res);
});
});
});
Поэтому, когда я звоню
mocha --require ./test/setUp.js ./test/publicAPIs.js
В нем говорилось, что
TypeError: Cannot read property 'app' of undefined of line chai.request(expressServer.app)
Приведенный выше файл является примером моей проблемы. Я проверил, что мне требуются все необходимые библиотеки. Некоторые думают, что это неправильно в настройке, в том, как работает мокко ?
Поэтому я очень смущен «Как я могу это исправить, чтобы подготовить сервер к тестированию API REST’