嘲笑一个常量

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

我想测试一个js文件,该文件中包含来自另一个导入的const引用(parser.js)

const { cp } = CML

我如何模拟这个,而仅模拟这个,而不模拟其余的功能?。它抛出此错误:

ReferenceError: CML is not defined

at Object.<anonymous> (src/state/lib/parser.js:2:16)
      at Object.<anonymous> (src/state/reducers/stateReducer.js:2:1)
      at Object.<anonymous> (src/state/reducers/index.js:4:1)
      at Object.<anonymous> (src/state/store/index.js:4:1)
      at Object.<anonymous> (src/state/store/store.spec.js:4:1)

CML是在其他js资源文件中定义的var。

jestjs
1个回答
0
投票
jest.mock('./state/lib/parser', function() {  // Put the exact path you've imported in your file
  return {
    CML: 123,
  };
});
© www.soinside.com 2019 - 2024. All rights reserved.