“未在全局范围内找到提取,并且没有传递提取器”。在流星中使用spacejam时 [英] "fetch is not found globally and no fetcher passed" when using spacejam in meteor

查看:105
本文介绍了“未在全局范围内找到提取,并且没有传递提取器”。在流星中使用spacejam时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写单元测试来检查我的api。在我与 dev 分支合并我的 git test 分支之前,一切都很好,但后来我开始得到这个错误

 应用程序运行于:http:// localhost:4096 / 
spacejam: meteor已准备好
spacejam:产生phantomjs
phantomjs:使用test-in-console在http:// localhost:4096 / local运行测试
phantomjs:错误:全局找不到fetch而没有fetcher传递,修复传递一个
的提取你的环境,如https://www.npmjs.com/package/unfetch。

例如:
从'unfetch'导入fetch;来自'apollo-link-http'的
import {createHttpLink};

const link = createHttpLink({uri:'/ graphql',fetch:fetch});

这是我的 api.test.js file:

  describe('用户的GraphQL API',()=> {
before(( )=> {
StubCollections.add([Meteor.users]);
StubCollections.stub();
});

after((=) > {
StubCollections.restore();
});

it('应该做的工作',()=> {
const x = 'hello';
expect(x).to.be.a('string');
});
});

最有趣的是我甚至没有 graphql 在我的测试中(虽然,我在我的 meteor 包中使用它)
不幸的是,我没有找到足够的信息(除了 apollo-link-http docs 有示例,但仍然让我感到困惑)。我确实尝试使用那个例子,但它没有帮助,我仍然得到同样的错误

解决方案

我也一样将执行graphql查询的npm模块导入到我的React应用程序时出错。应用程序正在编译但测试失败,因为 window.fetch 在Node.js运行时中不可用。



我通过安装 node-fetch https解决了这个问题://www.npmjs.com/package/node-fetch 并将以下声明添加到 jest.config.js



  const fetch = require('node-fetch')global.fetch = fetchglobal.window = globalglobal.Headers = fetch.Headersglobal.Request = fetch.Requestglobal.Response = fetch.Responseglobal.location = {hostname:''}  



这样做我们指示Jest在Node.js运行时执行前端代码时如何处理 window.fetch 。 / p>

I'm writing unit tests to check my api. Before I merged my git test branch with my dev branch everything was fine, but then I started to get this error:

App running at: http://localhost:4096/
spacejam: meteor is ready
spacejam: spawning phantomjs
phantomjs: Running tests at http://localhost:4096/local using test-in-console
phantomjs: Error: fetch is not found globally and no fetcher passed, to fix pass a fetch for
  your environment like https://www.npmjs.com/package/unfetch.

  For example:
    import fetch from 'unfetch';
    import { createHttpLink } from 'apollo-link-http';

    const link = createHttpLink({ uri: '/graphql', fetch: fetch });

Here's a part of my api.test.js file:

describe('GraphQL API for users', () => {
    before(() => {
      StubCollections.add([Meteor.users]);
      StubCollections.stub();
    });

    after(() => {
      StubCollections.restore();
    });

    it('should do the work', () => {
      const x = 'hello';
      expect(x).to.be.a('string');
    });
  });

The funniest thing is that I don't even have graphql in my tests (although, I use it in my meteor package) Unfortunately, I didn't to find enough information (apart from apollo-link-http docs that has examples, but still puzzles me). I did try to use that example, but it didn't help and I still get the same error

解决方案

I got the same error importing a npm module doing graphql queries into my React application. The app was compiling but tests were failing since window.fetch is not available in the Node.js runtime.

I solved the problem by installing node-fetch https://www.npmjs.com/package/node-fetch and adding the following declarations to jest.config.js:

const fetch = require('node-fetch')

global.fetch = fetch
global.window = global
global.Headers = fetch.Headers
global.Request = fetch.Request
global.Response = fetch.Response
global.location = { hostname: '' }

Doing so we instruct Jest on how to handle window.fetch when it executes frontend code in the Node.js runtime.

这篇关于“未在全局范围内找到提取,并且没有传递提取器”。在流星中使用spacejam时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆