首页 > 解决方案 > Proxyquire 显示错误“找不到模块”

问题描述

我正在尝试使用proxyquire替换私有函数以在我的 Meteor 应用程序中进行测试。

流星 1.6.1

流星测试:mocha@1.1.2

在我的 parentFunction.js 中:

import { some function } from 'anotherFile';

function childFunction() {
  ...
  return someValue;
}

export default function parentFunction() {
  return childFunction()
}

在我的测试文件中:

const proxyquire = require('proxyquire');

if (Meteor.isServer) {
  ...

  describe('parentFunction', () => {
    it('uses the mocked child function', () => {
      const testThing = proxyquire('./parentFunction', {
        'childFunction': () => ({ 'name': 'bob' }),
      });
  });
}

parentFunction.js 与我的测试文件在同一个文件夹中,只是为了仔细检查路径,我确保它有效:

import parentFunction from './parentFunction';

但是当我运行测试时,我看到了一个错误:

Error: Cannot find module './parentFunction.js'

我究竟做错了什么?我试过绝对路径,没用。据我从文档中可以看出,文件中需要 proxiquire 的相对路径应该没问题。

谢谢你的帮助!

标签: javascripttestingmeteormocha.jsproxyquire

解决方案


推荐阅读