开玩笑嘲笑一个const

时间:2020-06-05 13:48:51

标签: jestjs

我想测试一个js文件,该文件中引用了另一个导入文件(parser.js)的const。

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。

这是parser.js文件:

/* global CML */
const { cp } = CML;

// Massaging approvals array data
// Adding status and trimming unused values
export default {
    approvals: (approvals = [], globalActions = []) => (
        approvals.map(approval => {
            let status = 'default';
            let rejected = false;
            let reviewed = 0;
            ...

在stateReducer类中,这是解析器的导入:

import parser from '../lib/parser';

1 个答案:

答案 0 :(得分:0)

jest.mock('./state/lib/parser', function() {  // Put the exact path you've imported in your file
  return {
    CML: 123,
  };
});