Тесты Трэвиса возвращаются неопределенными - PullRequest
0 голосов
/ 07 сентября 2018

Я тестирую с жасмином, и он работает нормально локально. Однако Travis CI возвращает undefined для всех тестов API. Пример

4) Сервер GET / api / v1 / orders Статус 200 Сообщение: Ожидаемый неопределенный будет 200. стек: Ошибка: ожидается, что неопределенное значение будет 200. на

Фрагмент из тестов

  describe('GET /api/v1/orders', function () {
    var data = {};
    beforeAll(function (done) {
      Request.get('http://localhost:3001/api/v1/orders', function (error, response, body) {
        data.status = response.statusCode;
        data.body = JSON.parse(body);
        data.number = data.body.length;
        done();
      });
    });
    it('Status 200', function () {
      expect(data.status).toBe(200);
    });
    it('It should return three Items', function () {
      expect(data.number).toBe(3);
    });
  });

Может быть проблема в URL-адресе http://localhost:3001/api/v1/orders'?

1 Ответ

0 голосов
/ 07 сентября 2018

Похоже, вы нигде не запускаете свой сервер, поэтому localhost:3001 недоступен.

Хорошим решением было бы использовать что-то вроде supertest . Это позволит вам сделать что-то вроде этого:

app.js

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

const routes = require('./api/routes/routes.js')(app);

// We listen only if the file was called directly
if (require.main === module) {
    const server = app.listen(3001, () => {
        console.log('Listening on port %s...', server.address().port);
    });
} else {
// If the file was required, we export our app
    module.exports = app;
}

spec.routes.js

'use strict';

var request = require('supertest');

var app = require('../../app.js');

describe("Test the server", () => {
    // We manually listen here
    const server = app.listen();

    // When all tests are done, we close the server
    afterAll(() => {
        server.close();
    });

    it("should return orders properly", async () => {
        // If async/await isn't supported, use a callback
        await request(server)
            .get('/api/v1/orders')
            .expect(res => {
                expect(res.body.length).toBe(3);
                expect(res.status).toBe(200);
            });
    });
});

Supertest позволяет вам делать запросы, не полагаясь на определенный порт / URL / другой.

...