Friday, 13 September 2019

How can I mock Javascript file imported in Svelte for unit testing?

I have the following svelte file

Dashboard.svelte

<script>
  import Proxy from "../Proxy.js";

  import { onMount } from "svelte";

  export let data = [];

  onMount(async () => {
    spinner = true;
    let data = await Proxy.getData();
  });
</script>
......
......
......

Dashboard.test.js

import Dashboard from "../src/Dashboard.svelte"
jest.mock("../src/Proxy.js");

describe('Dashboard Component', (next) => {
  it('should update data on mount', () => {
    const target = document.createElement('div');
    const dashboard = new Dashboard({ target });

    Proxy.getData.mockResolvedValue([{
        "id": "sdadasdas"
    }]);

    setTimeout(() => {
      expect(dashboard.data.length).toBe(1);
      next();
    }, 10);
  })
})

Now, it is not mocking the Proxy.js that I am trying to mock and always going into the inside code.

Any idea what is going wrong here?

Thanks for the help.



from How can I mock Javascript file imported in Svelte for unit testing?

1 comment: