Supertest和Mocha的快速路线测试失败

时间:2018-04-02 21:51:21

标签: javascript node.js express mocha

我有一组测试来验证我的Express是否正确提供路线。对于我的一个路由launch.js,我收到两个不同的错误,有时候测试会随着长(425ms +)的响应时间而随机传递。有没有更好的方法来解决这个问题?

launch.js

const authUtils = require('../../lib/authUtils');
const express = require('express');
const VError = require('verror');

const router = express.Router();

router.get('/', (req, res) => {
  /**
   * Request conformance object
   */
  authUtils.getConformance((error, response, body) => {
    // If error with HTTP request
    if (error) {
      throw new VError('Conformance object request failed', error);
    // If error from auth server
    } else if (response.body.message) {
      throw new VError('Conformance object request failed', response.body.message);
    // If request for conformance object succeeds
    } else {
      // Parse conformance object for tokenUri and authUri
      authUtils.parseConformanceUris(body, (authUri, tokenUri, parsingError) => {
        // Ensure URIs can be parsed from response
        if (error) {
          throw new VError('Issue while parsing conformance object', parsingError);
        } else {
          /**
           * Data values needed later for /redirect
           * @type {{state: string, tokenUri: string}}
           */
          const launchData = {
            state: authUtils.getUniqueCode(),
            tokenUri,
          };

          // Build URI to request token from auth server
          authUtils.buildGetTokenUri(authUri, launchData.state, (getTokenUri) => {
            // Store state and tokenUri to session and redirect browser
            authUtils.storeLaunchData(req, res, launchData, getTokenUri);
          });
        }
      });
    }
  });
});

module.exports = router;

index.spec.js

const request = require('supertest');
const app = require('../index');

describe('Express server routes', () => {
  describe('GET /launch', () => {
    it('responds with HTTP 302', (done) => {
      request(app).get('/launch').expect(302, done);
    });
  });
});

subject.getConformance

/**
 * Utility function to request conformance object from auth server
 * @param callback
 */
const getConformance = (callback) => {
  request({ url: process.env.CONFORMANCE_URI, json: true, method: 'get' }, (error, response, body) => {
    if (!error && response.statusCode === 200) {
      callback(null, response, body);
    } else {
      callback(error, response, null);
    }
  });
};

错误1

  

未捕获的TypeError:无法读取null的属性“message”         at subject.getConformance(test / authUtils.spec.js:28:27)         在Request.request [as _callback](lib / authUtils.js:7:374)         在Request.self.callback(node_modules / request / request.js:186:22)         在请求。 (node_modules /请求/ request.js:1163:10)         在IncomingMessage。 (node_modules /请求/ request.js:1085:12)         at endReadableNT(_stream_readable.js:1059:12)         at _combinedTickCallback(internal / process / next_tick.js:138:11)         at process._tickCallback(internal / process / next_tick.js:180:9)

错误2

  

Uncaught AssertionError:期望'body'等于undefined         at subject.getConformance(test / authUtils.spec.js:43:16)         在Request.request [as _callback](lib / authUtils.js:7:374)         在Request.self.callback(node_modules / request / request.js:186:22)         在请求。 (node_modules /请求/ request.js:1163:10)         在IncomingMessage。 (node_modules /请求/ request.js:1085:12)         at endReadableNT(_stream_readable.js:1059:12)         at _combinedTickCallback(internal / process / next_tick.js:138:11)         at process._tickCallback(internal / process / next_tick.js:180:9)

1 个答案:

答案 0 :(得分:0)

假设需要来自expressjs服务器的app对象,请尝试

.get('/launch')
      .expect(302)
      .end(function (err, res) {
        res.status.should.be.equal(302);
        done();
      });

如果index.js文件不是服务器,则需要从有效服务器配置app对象。您是从app

的index.js导出app吗?
var express = require("express")
    , app = express();