Skip to content

Properly mocking lodash function #5753

Closed Answered by jritzi
jritzi asked this question in Q&A
Discussion options

You must be logged in to vote

Thanks, I was importing as import { debounce } from "lodash" (under the impression that lodash supports ESM, not sure why that works), but it looks like it won't until version 5 and using lodash-es still the recommended way to do it.

Using import { debounce } from "lodash", I was able to get it working using:

vi.mock("lodash", async (importOriginal) => {
  const mod = await importOriginal<{ default: typeof import("lodash") }>();

  return {
    ...mod.default,
    debounce: (fn: unknown) => fn,
  };
});

Switching to import { debounce } from "lodash-es", I was able to get it working using:

vi.mock("lodash-es", async (importOriginal) => {
  const mod = await importOriginal<typeof import("l…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jritzi
Comment options

Answer selected by jritzi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants