[Vuejs]-Test debounced Vue method with Mocha Chai and Sinon

0👍

There are several potential approaches, but it sounds like you want to mock lodash. I’ll list the steps linearly for ease of presentation, but you’ll probably want to distribute them appropriately within your test blocks and hooks. I’m also assuming the only call to debounce is the method in question. If not, adjust the following accordingly.

import * as Lodash from "lodash";
const debounce = sinon.stub(Lodash, "debounce").returns(() => {});
// create your component
expect(debounce.calledOnce).to.equal(true);
expect(debounce.firstCall.args[0]).to.be.a("function");
expect(debounce.firstCall.args[0]()).to.equal("test");
Lodash.debounce.restore();

Leave a comment