Я использую Mocha и SuperTest для тестирования моего Express API.Однако мой первый тест всегда кажется успешным, когда внутри .then()
моего request()
.
я передаю String
тесту, который ожидает Array
.Так что обязательно надо пройти тест.
Он выходит за пределы then()
, как и ожидалось, но у меня не будет доступа к res.body
там для выполнения моих тестов.
Вот мой код:
const expect = require('chai').expect;
const request = require('supertest');
const router = require('../../routes/api/playlist.route');
const app = require('../../app');
describe('Playlist Route', function() {
// before((done) => {
// }
describe('Get all playlists by user', function() {
it('Should error out with "No playlists found" if there are no Playlists', function() {
request(app).get('/api/playlists/all')
.then(res => {
const { body } = res;
// Test passes if expect here
expect('sdfb').to.be.an('array');
})
.catch(err => {
console.log('err: ', err);
});
// Test fails if expect here
expect('sdfb').to.be.an('array');
})
})
});
Я нашел эту статью , но я не использую try catch
блок, но я подумал, что, возможно, это как-то связано с обещанием.