开玩笑的虚拟模拟:如何解决此故障?

问题描述 投票:0回答:1

因此,我有一个模块的导入语句,该语句仅在我尝试使用在节点11.1.0上运行的jest 25.1进行测试的模块中的vm的nashorn运行时上运行时可用。

import RequestBuilder from 'nashorn-js/request_builder'

...以及导入块中的其他行之后,此:

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

在相应的.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'

我一直未能成功地摆脱开玩笑的失败:

  ● 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() ) )

[我已经尝试过使用requireimport等各种不同的模拟结构,但是还没有找到神奇的子弹。

据我所知,似乎RequestBuilder import语句甚至没有调用虚拟模拟。或者至少,如果我将console.log()语句添加到虚拟模拟工厂函数中,我将永远不会在输出中看到这些日志消息。

任何人都知道我缺少什么,或者还有什么可以尝试的?我在代码的其他部分使用了几乎相同的模式,但在该模块中可以使用此设置,但是由于该模块的某些神秘原因,我无法使虚拟模拟正常工作。非常感谢您的帮助。

所以我有此导入语句用于一个模块,该语句仅在我尝试使用在节点11.1.0上运行的jest 25.1进行测试的模块中的vm的nashorn运行时上运行时可用:导入...

javascript unit-testing jestjs jest
1个回答
0
投票
因此,这里的问题原来是.spec文件中importrequire的玩笑实现。
© www.soinside.com 2019 - 2024. All rights reserved.