简体   繁体   English

Jest 虚拟模拟:如何解决此故障?

[英]Jest virtual mock: how do I troubleshoot this failure?

So I have this import statement in a module that I'm trying to test using jest 25.1 running on node 11.1.0.所以我在一个模块中有这个导入语句,我试图使用在节点 11.1.0 上运行的 jest 25.1 来测试它。 The import statement is for a module that is only available when running on the jvm's nashorn runtime, so I'm using jest's virtual mock to control the behavior in the unit tests. import 语句适用于仅在 jvm 的 nashorn 运行时上运行时才可用的模块,因此我使用 jest 的虚拟模拟来控制单元测试中的行为。 Here's what the import statement looks like in the module under test:下面是被测模块中的 import 语句:

import RequestBuilder from 'nashorn-js/request_builder'

...and after the other lines in the import block, this: ...在导入块中的其他行之后,这个:

const request = RequestBuilder.create('some-string')
.sendTo('some-other-string')
.withAction('yet-another-string')
.getResultWith( consumer => consumer.result( consumer.message().body() ) )

export const functionA = () => {...} // uses 'request' variable

export const functionB = () => {...} // uses 'request' variable

In the corresponding .spec file, I have this virtual mock setup:在相应的 .spec 文件中,我有这个虚拟模拟设置:

const mockGetResultWith = {
  getResultWith: jest.fn()
}
const mockWithAction = {
  withAction: jest.fn().mockImplementation(() => mockGetResultWith)
}
const mockSendTo = {
  sendTo: jest.fn().mockImplementation(() => mockWithAction)
}
const mockBuilder = {
  create: jest.fn().mockImplementation(() => mockSendTo)
}
jest.mock(
  'nashorn-js/request_builder',
  () => mockBuilder,
  { virtual: true }
)

require('nashorn-js/request_builder')

import { functionA, functionB } from './module-under-test'

I have been trying unsuccessfully to get past this failure from jest:我一直试图从玩笑中摆脱这种失败,但没有成功:

  ● Test suite failed to run

    TypeError: Cannot read property 'create' of undefined

      35 | }
      36 | 
    > 37 | const verify = RequestBuilder.create('some-string')
         |                               ^
      38 | .sendTo('some-other-string')
      39 | .withAction('yet-another-string')
      40 | .getResultWith( consumer => consumer.result( consumer.message().body() ) )

I've tried all kinds of different mock structures, using require vs import , etc, but haven't found the magic bullet.我尝试了各种不同的模拟结构,使用requireimport等,但还没有找到灵丹妙药。

As near as I can tell, it does not appear that the RequestBuilder import statement is even invoking the virtual mock.据我所知, RequestBuilder导入语句似乎甚至没有调用虚拟模拟。 Or at least, if I add console.log() statements to the virtual mock factory function, I never see those log messages in the output.或者至少,如果我将console.log()语句添加到虚拟模拟工厂函数中,我将不会在输出中看到这些日志消息。

Anybody have any idea what I'm missing or what else to try?任何人都知道我缺少什么或还有什么可以尝试的吗? I have pretty much the same pattern in use in other parts of the code, where this setup works, but for some mystical reason with this module, I can't get the virtual mock working.我在代码的其他部分使用了几乎相同的模式,这个设置在那里工作,但是由于这个模块的一些神秘原因,我无法让虚拟模拟工作。 Any help is greatly appreciated.任何帮助是极大的赞赏。

So, the problem here turned out to be jest's implementation of import vs require in the .spec file.所以,这里的问题原来是 jest 在 .spec 文件中对importrequire的实现。

By changing this line:通过改变这一行:

import { functionA, functionB } from './module-under-test'

To this:对此:

const module = require('./module-under-test')
const functionA = module.functionA
const functionB = module.functionB

The module under test now loads successfully, and the tests run as expected.被测模块现在成功加载,并且测试按预期运行。

I have no explanation for this, and haven't been able to find jest documentation describing why I'd get any different behavior between require vs import .我对此没有任何解释,也找不到描述为什么我会在requireimport之间出现任何不同行为的笑话文档。 In fact, I have the mock configuration setup before any import statements as described here:事实上,我在任何import语句之前都有模拟配置设置,如下所述:

https://github.com/kentcdodds/how-jest-mocking-works https://github.com/kentcdodds/how-jest-mocking-works

If anybody out there understands what's going on with this import behavior, or has a link describing what I'm missing, I'd sure appreciate the info.如果那里有人了解这种import行为发生了什么,或者有一个描述我遗漏的链接,我肯定会感谢这些信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM