如何让诺克和摩卡一起打得好?

时间:2017-01-12 17:25:34

标签: node.js testing asynchronous mocha nock

我正在尝试使用nock拦截/模拟我的应用程序中的某些HTTP流量以进行测试。我们的应用程序对我们的另一个站点进行身份验证,我需要nock模仿HTTP 200(带有JSON数据)和HTTP 401(没有数据)来测试用户是否登录时的行为(分别)。

我有两个测试,它们在单独运行时都能正常工作,但是如果我运行整个测试套件,其中一个总是会失败。我意识到nock是共享状态,因为它修改了node.js本身处理网络流量的方式,我认为这是竞争条件的原因,但我不能成为唯一的人在两个不同的测试中对同一个请求使用了两个不同的nock拦截器,所以我知道我错过了什么。

任何人都可以帮我弄清楚为什么这些测试会相互踩踏?

我的问题与How to retest same URL using Mocha and Nock?有关,但我做了那里建议的事情,但他们没有帮助。

我的测试文件(再次,如果单独调用它们都可以正常工作,但是作为同一测试传递的一部分运行时失败)看起来像这样:

import { expect } from 'chai';
import nock from 'nock';

import * as actionTypes from '../../src/constants/action-types';
import * as panoptes from '../../src/services/panoptes';

import { user } from '../modules/users/test-data';

const stagingHost = 'https://my-staging-server.org';

describe('Panoptes', () => {
  afterEach(function (done) {
    nock.cleanAll();
    nock.disableNetConnect();
    done();
  });

  beforeEach(function (done) {
    nock.cleanAll();
    nock.disableNetConnect();
    done();
  });

  describe('with a valid user', function (done) {
    let lastAction = null;

    const scope = nock(stagingHost)
      .get(/^\/oauth\/authorize/)
      .reply(302, '', {
        'location': 'https://localhost:3000',
        'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
        'X-Frame-Options': 'SAMEORIGIN',
        'X-XSS-Protection': '1; mode=block',
      });

    scope
      .get(/^\/api\/me/)
      .reply(200, {
        users: [user],
      });

    panoptes.checkLoginUser((action) => { lastAction = action; }).then(() => {
      nock.removeInterceptor(scope);
      done();
    });

    it('should know when somebody is logged in', function () {
      expect(lastAction).to.not.be.null;
      expect(lastAction.type).to.equal(actionTypes.SET_LOGIN_USER);
      expect(lastAction.user).to.not.be.null;
      expect(lastAction.user.id).to.equal(user.id);
      expect(lastAction.user.login).to.equal(user.login);
    });
  });
});

import { expect } from 'chai';
import nock from 'nock';

import * as actionTypes from '../../src/constants/action-types';
import * as panoptes from '../../src/services/panoptes';

const stagingHost = 'https://my-staging-server.org';

describe('Panoptes', () => {
  afterEach(function (done) {
    nock.cleanAll();
    nock.disableNetConnect();
    done();
  });

  beforeEach(function (done) {
    nock.cleanAll();
    nock.disableNetConnect();
    done();
  });

  describe('with no user', function (done) {
    let lastAction = null;

    const scope = nock(stagingHost)
      .get(/^\/oauth\/authorize/)
      .reply(302, '', {
        'Cache-Control': 'no-cache',
        'location': 'https://my-staging-server.org/users/sign_in',
        'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
        'X-Frame-Options': 'SAMEORIGIN',
        'X-XSS-Protection': '1; mode=block',
      });

    scope
      .get(/^\/api\/me/)
      .reply(401);

    panoptes.checkLoginUser((action) => { lastAction = action; }).then(() => {
      nock.removeInterceptor(scope);
      done();
    });

    it('should know that nobody is logged in', function () {
      expect(lastAction).to.not.be.null;
      expect(lastAction.type).to.equal(actionTypes.SET_LOGIN_USER);
      expect(lastAction.user).to.be.null;
    });
  });
});

2 个答案:

答案 0 :(得分:2)

我认为问题不在于nock,而在于你的mocha hook执行顺序的顺序:

举个例子:

describe('Panoptes', () => {

  afterEach(function () {
    console.log('ORDER: after each');
  });

  beforeEach(function () {
    console.log('ORDER: before each');
  });

  describe('with a valid user', function () {

    console.log('ORDER: with a valid user');

    it('should know when somebody is logged in', function () {
      console.log('ORDER: should know when somebody is logged in');
    });

  });

  describe('with no user', function () {

    console.log('ORDER: with no user');

    it('should know that nobody is logged in', function () {
      console.log('ORDER: should know that nobody is logged in');
    });

  });

});

当我们运行它时,我们在输出上得到以下顺序:

ORDER: with a valid user
ORDER: with no user
ORDER: before each
ORDER: should know when somebody is logged in
ORDER: after each
ORDER: before each
ORDER: should know that nobody is logged in
ORDER: after each

afterEach / beforeEach在每个it之前和之后运行,但describe正文在调用这些挂钩之前会被评估。你应该将你的每个nocks包裹在before内。 (describe也不使用done参数)

这样的事情应该有效:

describe('with no user', function () {

  before(function() {
    const scope = nock(stagingHost)
      .get(/^\/oauth\/authorize/)
      .reply(302, '', {
        'Cache-Control': 'no-cache',
        'location': 'https://my-staging-server.org/users/sign_in',
        'Strict-Transport-Security': 'max-age=31536000; includeSubDomains',
        'X-Frame-Options': 'SAMEORIGIN',
        'X-XSS-Protection': '1; mode=block',
      });

    scope
      .get(/^\/api\/me/)
      .reply(401);
  });


  it('should know that nobody is logged in', function (done) {
    panoptes.checkLoginUser((action) => {
      expect(action).to.not.be.null;
      expect(action.type).to.equal(actionTypes.SET_LOGIN_USER);
      expect(action.user).to.be.null;
      done();
    });
  });

});

答案 1 :(得分:0)

我使用了sanketh-katta的答案,我对这个问题给予了肯定,但稍微修改了it块,所以我包含了我的完整性代码:< / p>

it('should know when somebody is logged in', function(done) {
  panoptes.checkLoginUser((action) => {
    try {
      expect(action).to.not.be.null;
      expect(action.type).to.equal(actionTypes.SET_LOGIN_USER);
      expect(action.user).to.not.be.null;
      expect(action.user.id).to.equal(user.id);
      expect(action.user.login).to.equal(user.login);
      done();
    } catch (ex) {
      done(ex);
    }
  });
});

之前,当其中一个测试失败时,永远不会达到done()调用,因此我会收到测试已超时而不是特定失败的消息。