小编Suf*_*ane的帖子

开玩笑 ReferenceError:初始化前无法访问 ''

我收到错误:

ReferenceError: Cannot access 'myMock' before initialization

尽管我尊重有关提升的笑话文档: A limitation with the factory parameter is that, since calls to jest.mock() are hoisted to the top of the file, it's not possible to first define a variable and then use it in the factory. An exception is made for variables that start with the word 'mock'.

我正在这样做:

import MyClass from './my_class';
import * as anotherClass from './another_class';

const mockMethod1 = jest.fn();
const mockMethod2 = jest.fn();
jest.mock('./my_class', () => {
  return { …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing node.js typescript jestjs

76
推荐指数
6
解决办法
10万
查看次数

Sinon存根函数用于解构

我希望对当前正在测试的文件中使用的函数存根。像这样的解构需要此函数:

 const { theFunctionIWant } = require('path/to/module')
Run Code Online (Sandbox Code Playgroud)

测试时,永远不会调用存根,而是继续调用实函数。但是当我“正常”地要求它时(即:不破坏结构)

const myModule = require('path/to/module')
Run Code Online (Sandbox Code Playgroud)

然后正确使用存根,一切正常

我认为这是由于解构的工作原理以及sinon直接对对象属性而不是对函数进行存根的事实。无论如何,如果您能为我提供一些见解,我将不胜感激!

unit-testing node.js sinon

7
推荐指数
1
解决办法
999
查看次数

标签 统计

node.js ×2

unit-testing ×2

javascript ×1

jestjs ×1

sinon ×1

typescript ×1