jeudi 4 juin 2020

Jest.spyOn Illegal invocation

Currently, I have simple create react app with following code

import React from "react";

export const readFilePromise = (file) => {
  return new Promise((resolve, reject) => {
    let reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => {
      resolve(reader.result);
    };
    reader.onerror = (error) => {
      reject(error);
    };
  });
};

test("test", async () => {
  let spy = jest
    .spyOn(FileReader.prototype, "onload")
    .mockImplementation(() => {
      console.log("+++ onload");
    });

  const result = await readFilePromise();

  spy.mockRestore();
});

When running npm test, I am getting this Illegal invocation

TypeError: Illegal invocation

16 | test("test", async () => {
17 |   let spy = jest
> 18 |     .spyOn(FileReader.prototype, "onload")

Any idea how to fix it?

Aucun commentaire:

Enregistrer un commentaire