你可以自己制作一个包装文件,它最终会lodash/debounce为你导出同一个实例,但这次你可以存根,例如:myutils/lodash/debounce.jsimport lodashDebounce from 'lodash/debounce';const exports = { debounce: lodashDebounce,};export const debounce = () => exports.debounce();export default exports;现在,在您的实际代码中,debounce不是从原始位置导入,而是从这个包装文件导入:前:import debounce from 'lodash/debounce' // this is how we usually do后:import { debounce } from 'myutils/lodash/debounce' // if we want to stub it// all other code-lines remain the sameconst method = () => { debounce(callback, 150)); ...}现在在做test.js 时:import lodashWrapped from 'myutils/lodash/debounce';sinon.stub(lodashWrapped , 'debounce').callsFake((callbackFn) => { // this is stubbed now});// go on, make your tests now