如何让Jest看到我为MongoDB Stitch编写的功能? [英] How to get Jest to see the functions I am writing for MongoDB Stitch?

查看:82
本文介绍了如何让Jest看到我为MongoDB Stitch编写的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试Stitch,它是MongoDB中无服务器/托管的JavaScript环境.我的主要目的是帮助我学习现代JavaScript,但是我也试图编写一个有用的应用程序.

I am trying out Stitch, a serverless/hosted JavaScript environment from MongoDB. My main purpose is to help me learn modern JavaScript, but I am trying to write a useful app as well.

我编写了以下功能,并将其保存在我的Stitch应用程序中.我相信这遵循了在Stitch中编写函数的文档记录方法,并且我已经在Stitch管理控制台中对其进行了测试:

I have written the following function, and saved it in my Stitch app. I believe this follows the documented way to write functions in Stitch, and I have tested it from the Stitch administration console:

exports = function(query){
  const http = context.services.get("HTTP");
  const urlBase = context.values.get("stackOverflowApiUrl");
  const options = [
    'order=desc',
    'sort=activity',
    'site=stackoverflow',
    'q=' + encodeURIComponent(query),
    'user=472495',
    'filter=!--uPQ.wqQ0zW'
  ];

  return http
    .get({ url: urlBase + '?' + options.join('&') })
    .then(response => {
      // The response body is encoded as raw BSON.Binary. Parse it to JSON.
      const ejson_body = EJSON.parse(response.body.text());
      return ejson_body.total;
    });
};

此代码非常简单-它获取一个http对象以进行外部API访存,并获取一个URL urlBase要联系的配置值(解析为https://api.stackexchange.com/2.2/search/excerpts),然后调用堆栈溢出数据API.这会针对我的用户运行搜索查询,并返回结果数.

This code is pretty simple - it obtains an http object for making external API fetches, and obtains a configuration value for a URL urlBase to contact (resolving to https://api.stackexchange.com/2.2/search/excerpts) and then makes a call to the Stack Overflow Data API. This runs a search query against my user and returns the number of results.

到目前为止,一切都很好.现在,我想在Jest中本地调用此函数.为此,我已经在本地Docker容器中安装了Node和Jest,并编写了以下测试功能:

So far so good. Now, I want to call this function locally, in Jest. To do this, I have installed Node and Jest in a local Docker container, and have written the following test function:

const callApi = require('./source');

test('Simple fetch with no user', () => {
    expect(callApi('hello')).toBe(123);
});

此操作失败,并出现以下错误:

This fails, with the following error:

~ # jest
 FAIL  functions/callApi/source.test.js
  ✕ Simple fetch with no user (3ms)

  ● Simple fetch with no user

    TypeError: callApi is not a function

      2 | 
      3 | test('Simple fetch with no user', () => {
    > 4 |     expect(callApi('hello')).toBe(123);
        |            ^
      5 | });
      6 | 

      at Object.<anonymous>.test (functions/callApi/source.test.js:4:12)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        1.418s
Ran all test suites.

(实际上,我原以为它会失败,因为它包含Jest无法访问的全局对象context.稍后我将研究如何模拟该对象,但目前Jest甚至无法看到该函数.全部).

(In fact I was expecting it to fail, since it contains a global object context that Jest does not have access to. I will work out how to mock that later, but for now Jest cannot even see the function at all).

我怀疑我能看到原因-在Jest简介文档中,必须为SUT执行此操作:

I suspect I can see the reason - in the Jest introduction docs, one has to do this for the SUT:

module.exports = function() { ... }

但是Stitch文档似乎要求将函数定义为:

However the Stitch docs seem to require functions to be defined as:

exports = function() { ... }

我没有JavaScript的背景知识来理解它们之间的区别.我可以在Stitch中尝试module.exports,但我宁愿不这样做,因为它要么现在不起作用,否则将来会造成损坏.可以指示Jest看到"没有module前缀的裸露exports吗?

I do not have a background in JavaScript to understand the difference. I could try module.exports in Stitch, but I would rather not, since this would either not work now, or cause a breakage in the future. Can Jest be instructed to "see" bare exports without the module prefix?

顺便说一句,我之所以选择Jest是因为它很流行,并且因为我的一些JavaScript同事都支持它.但是,我不愿屈服于此,如果知道它对Stitch开发更有利,我很乐意使用其他方法.

Incidentally, I have picked Jest because it is popular, and because some of my JavaScript colleagues vouch for it. However, I am not wedded to it, and would be happy to use something else if it is known to be better for Stitch development.

下面是来自jperl的有用答案,我发现在Stitch中不可能进行以下构造:

Following the useful answer from jperl below, I find that the following construction is not possible in Stitch:

module.exports = exports = function() {}

我也不能这样做:

exports = function() {}
module.exports = exports

如果我尝试任何一种,都会出现以下错误:

If I try either, I get the following error:

函数验证期间的运行时错误

runtime error during function validation

因此,看来我必须让Jest在不使用module.exports的情况下工作,或创建一个将exports版本导入到module.exports的胶粘文件,其中主要文件由Stitch使用,而胶粘剂导入器为由Jest使用.

So it looks like I have to get Jest to work without module.exports, or create a glue file that imports the exports version into module.exports, with the main file being used by Stitch, and the glue importer being used by Jest.

推荐答案

我建议您阅读此

I suggest you to read this thread. And you're right in thinking it has to do with modules.exports vs exports. The thing is that module.exports and exports first point to the same thing. So something like this works:

//modify the same object that modules.exports is pointing to
exports.a = {}
exports.b = {}

但这不会:

exports = {}

为什么?因为现在exports指向的是module.exports以外的其他内容,所以您所做的根本没有任何作用.

Why? Because now exports points to something else than module.exports so what you're doing has no effect at all.

在评论中进行了一些更新之后,我们认为Stitch似乎不支持Jest要求的导出格式.

Following some updates in the comments, we came to the view that Stitch does not seem to support the export format that Jest requires.

这篇关于如何让Jest看到我为MongoDB Stitch编写的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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