基于这个答案,我发现了一种模拟命名导出的好方法:https://stackoverflow.com/a/38414108/4644522
codeToTest.js:
import {someMethod} from './someNamedExports'
export default () => someMethod()
codeToTestSpec.js:
import * as someNamedExports from './someNamedExports'
import codeToTest from './codeToTest
sinon.stub(someNamedExports, 'someMethod')
codeToTest()
someNamedExports.someMethod.should.have.been.called
someNamedExports.someMethod.restore()
这可以按预期工作。
但是,我似乎遇到了默认导出问题。这是我尝试过的:
codeToTest.js:
import someDefaultExport from './someDefaultExport
export default () => someDefaultExport()
codeToTestSpec.js:
import * as someDefaultExport from './someDefaultExport'
import codeToTest from './codeToTest
sinon.stub(someDefaultExport, 'default')
codeToTest()
someDefaultExport.default.should.have.been.called
someDefaultExport.default.restore()
它似乎没有像预期的那样存在,并抱怨在尝试恢复时没有定义someDefaultExport.default。
我错过了什么,或者这是一个可能的错误?