上下文不可用?"/>
使用 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 时如何修复父上下文不可用?
发布评论