对多个测试使用相同地址的Nock失败

时间:2018-11-20 12:43:53

标签: nock

我正在尝试对多种测试类型测试相同的URL。它可以单独工作,但不能一起工作。我知道以下事实:该特定URL的拦截器一旦使用就被删除,因此我使用了persist()方法,因此不会发生。但这似乎没有什么作用?我也尝试为每个单独的代码添加Nock代码,但这没有帮助。有什么帮助吗? :O

import request from 'supertest';
import { assert } from 'chai';
import app from 'app';
import rawResponse from './raw-json-response';
import tokenResponse from './token-response';
const request2 = request(app);
const config = require('config');
const nock = require('nock');
const isJsonTest = function (json) {
  let str = '';
  str = json.toString();
  try {
    JSON.parse(str);
  } catch (e) {
    return false;
  }
  return true;
};
describe('API', () => {
  beforeEach((done) => {
    if (!nock.isActive()) nock.activate();
    nock('https://login.salesforce.com:443/services/oauth2/token').persist().log(console.log).post('').reply(200, tokenResponse);
    nock(/cloudforce\.com/).persist().log(console.log).get(/\/services\/data\/v41\.0\/query\?q/gm).reply(200, rawResponse);
    done();
  });
  afterEach((done) => {
    nock.cleanAll();
    nock.restore();
    done();
  });
  it('Make sure Nock mock data is pulling through.', (done) => {
    request2.get('/careers-svc/api/vacancies?limit=1').set('Accept', 'application/json').then((response) => {
      assert.equal(JSON.parse(response.text)[0].MF_Location__c, 'The Shard London Bridge', 'Checking if Nock mock data is correct.');
      done();
    });
  });
  it('Vacancies API response is valid JSON', (done) => {
    request2.get('/careers-svc/api/vacancies?limit=1').set('Accept', 'application/json').then((response) => {
      console.log('response: ');
      console.log(response.text);
      const isJson = isJsonTest(response.text);
      assert.isTrue(isJson, 'is valid JSON.');
      done();
    });
  });

0 个答案:

没有答案