使用 ES6 import 语句使用 proxyquire 时如何修复父上下文不可用?

编程入门 行业动态 更新时间:2024-10-05 15:26:03

使用 ES6 import 语句使用 proxyquire 时如何修复父<a href=https://www.elefans.com/category/jswz/34/1770489.html style=上下文不可用?"/>

使用 ES6 import 语句使用 proxyquire 时如何修复父上下文不可用?

我有以下文件要测试

import {getDatabaseName}  from "../conf/maria_db_connector"

export default class MigrationRepository {
    getExistingVersions() {
        return getDatabaseName();
    }

和以下测试文件:

import MigrationRepository from '../../main/repository/migration_repository.js'
import proxyquire from 'proxyquire';
const proxy = proxyquire.noCallThru(); 

describe('MigrationRepository', function () {

  beforeEach(function () {
    this.mockDbPool = sinon.mock()

    proxy.load('../../main/repository/migration_repository.js', {
      '../conf/maria_db_connector.js': {
        getDatabaseName: function () { return "test" },
      }
    })
  });
}

但是,当我使用 mocha 运行此测试时,我发现它在加载函数的以下行中的 proxyquire 中失败了

  return this._withoutCache(this._parent, stubs, request, this._parent.require.bind(this._parent, request))

带有错误信息

 TypeError: Cannot read properties of undefined (reading 'require')

那么如何使用 ES6 导入语句填充父上下文?

回答如下:

更多推荐

使用 ES6 import 语句使用 proxyquire 时如何修复父上下文不可用?

本文发布于:2024-05-13 14:49:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759725.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   不可用   语句   import   proxyquire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!