【问题标题】:nock is not intercepting my requestnock 没有拦截我的请求
【发布时间】:2016-10-05 08:12:22
【问题描述】:

我正在尝试使用 karma server 和 nock 创建一些基本测试。 似乎 nock 根本没有拦截我的请求,有人知道吗?我无法弄清楚缺少什么。我仍然得到真实的数据。

nock('https://api.github.com/users/' + username).log(console.log)
.get('/')
.query(true)
.reply(400, {
  statusMessage: 'Bad Request',
  foo: 'foo'
})

http.get('https://api.github.com/users/' + username, function(res) {
  console.log('res', res)
})

我也添加了这个中间件

const middlewares = [thunk];
const mockStore = configureStore(middlewares);

====== 6 月 6 日更新 ======

使用 react-redux 的整个流程 这是我的测试:

import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import axios from 'axios';
import expect from 'expect';
import * as actions from 'actions/test-actions'
import * as types from 'types';
import nock from 'nock'
import { username } from 'constansts'

const middlewares = [thunk];
const mockStore = configureStore(middlewares);

describe('Asynchronous actions', () => {
  it('Basic example', done => {
    nock('https://api.github.com')
    .get('/users/' + username)
    .reply(400, {
      statusMessage: 'Bad Request',
      foo: 'foo'
    })

    var expectedActions = []
    let store = mockStore([], expectedActions, done)

    store.dispatch(actions.testRequest())
      .then(() => {
        console.log('store.getActions() => ', store.getActions())
      })
      .then(done).catch((err) => {
        console.log('ERROR==>', err)
        done()
      })
  })
})

这是行动

export function testRequest () {
  return axios.get('https://api.github.com/users/' + username)
  .then(function (res) {
    console.log('response =>', res.status)
  })
  .catch(function (err) {
    console.log('error =>', err)
  })
}

res.status 是 200,即使我使用 nock 更改为 400

【问题讨论】:

  • 看看:github.com/node-nock/nock/issues/150。尽管我使用的是 axios,但我也无法让我的客户端不点击 nock 服务器。看来它对很多人都不起作用
  • @AlexPanov 谢谢!我还尝试了 fetch-mock 和 isomorphic-fetch,这里也是一样,我仍然可以取回真实数据

标签: node.js testing reactjs karma-mocha nock


【解决方案1】:

你应该在get方法中指定路径:

nock('https://api.github.com').log(console.log)
  .get('/users/' + username)
  .query(true)
  .reply(400, {
    statusMessage: 'Bad Request',
    foo: 'foo'
  });

【讨论】:

  • 我试过这个,也没有用。我在业力配置上遗漏了什么吗?我还尝试了整个 react-redux 流程,使用 mockStore 调用动作。我很确定我错过了一些基本步骤,我无法弄清楚它是什么
【解决方案2】:

我找到了答案!

显然与 axios 不兼容,我也尝试了 'fetch'、'isomorphic-fetch' 但没有运气。

'whatwg-fetch' 是答案

非常感谢,我希望这篇文章对其他人有所帮助。

import 'whatwg-fetch'
export function testRequest () {
  return fetch('https://api.github.com/users/' + username)
  .then(function (res) {
    console.log('response =>', res.status)
  })
  .catch(function (err) {
    console.log('error =>', err)
  })
}

【讨论】:

  • 我认为这些信息并不准确。我们在项目中成功地使用了 axios 和 nock。您可能遇到了一些您没有弄清楚的问题。
  • 我发现最新版本的 axios 和 nock 可以一起工作,(测试 nock v8.0.0)但是旧版本的 nock 不能与 axios 一起工作。 (经过测试的 nock v0.59.1)尝试更新并查看是否有帮助。
  • FWIW 我最近在更新 axios 时发现 axios@0.14.0 与 nock@8.0.0 不兼容。回滚到axios@0.13.1 解决了这个问题。
  • 我有一个问题,如果我直接运行测试文件,它工作正常,但如果我用 nyc 运行它们,它不会拦截,所以它在 npm test nyc --check-coverage -- 中失败分支 50 --functions 41 --lines 59 mocha
  • 我看到它可以在 windows 上运行,但在 linux 下失败
【解决方案3】:

您是在节点环境中还是在 Web 浏览器(如 PhantomJS)中运行测试?

要使用 nock,您必须在 node 中运行测试(使用 Jest 或 mocha),nock 会覆盖 node http 行为,因此它仅适用于 node 而不能在浏览器中(如 PhantomJS)。

要运行测试,您可以:

  • 在节点环境(如 Jest 或 mocha)中运行它
  • 或使用不同的库模拟fetch-mock

【讨论】:

    【解决方案4】:

    这是一个老问题,但我相信答案是您需要设置axios http 适配器:

    import axios from 'axios';
    axios.defaults.adapter = require('axios/lib/adapters/http');
    

    使用jest 运行测试时,您通常在“类似浏览器”的环境中运行它们。要让 axios 使用节点 http 库,您需要明确告诉它使用 http 适配器。

    https://github.com/axios/axios/tree/master/lib/adapters

    【讨论】:

    • 澄清一下:在浏览器中 axios 使用了 nock 无法拦截的 XHR。该修复程序强制 axios 使用 http 适配器进行 nock 使用。在 NodeJS 中 http 是默认的,不需要适配器补丁。
    猜你喜欢
    • 2019-09-26
    • 2019-07-14
    • 1970-01-01
    • 2019-09-19
    • 2017-06-19
    • 2017-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多