Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

fix: useFetch in jest unit tests #437

Open
FloryanFilip opened this issue Apr 2, 2021 · 6 comments
Open

fix: useFetch in jest unit tests #437

FloryanFilip opened this issue Apr 2, 2021 · 6 comments
Assignees
Labels
documentation Improvements or additions to documentation needs-repro Waiting for code that can reproduce the issue

Comments

@FloryanFilip
Copy link

🐛 The bug
When unit testing a component that uses useFetch composable, I'm getting weird error.
Zrzut ekranu 2021-04-2 o 22 29 09
Zrzut ekranu 2021-04-2 o 22 29 15

🛠️ To reproduce
Steps to reproduce the behavior:

  1. Create a component that uses setup function with useFetch composable inside of it
  2. Write a test suite with any unit test of said component
  3. See error

I'd be very grateful for a link to a gist or repo that reproduces the bug.

🌈 Expected behaviour
The test suite runs smoothly

ℹ️ Additional context
My component

export default defineComponent({
  setup() {

    const { fetch } = useFetch(async () => {
      //
    });

    return {
      //
    };
  },

My test suite


describe('MainSearch', () => {
  let wrapper!: Wrapper<any>;
  const factory = (data?: object) => shallowMount(MainSearchComponent, {
    stubs: {
      ...
    },
    vuetify: new Vuetify(),
  });

  beforeEach(() => {
    wrapper = factory();
  });

  afterEach(() => {
    wrapper.destroy();
  });

  it('should match snapshot', () => {
    expect(wrapper.html())
      .toMatchSnapshot();
  });
@FloryanFilip FloryanFilip added the bug Something isn't working label Apr 2, 2021
Copy link
Member

@FloryanFilip I can't reproduce the error with your code. Note that useFetch is quite tightly coupled with Nuxt and it needs a number of global properties to be present on the instance in order to actually execute fetch(), which is probably why you're getting these errors (though not in your code snippets above).

@danielroe danielroe added the needs-repro Waiting for code that can reproduce the issue label Apr 12, 2021 — with Volta.net
@FloryanFilip
Copy link
Author

@danielroe what do you mean by number of global properties? How can I make sure i have these?

@treebee
Copy link

treebee commented Apr 26, 2021

I would also be interested in what setup steps are missing. I created a reproduction repo here

@danielroe danielroe added documentation Improvements or additions to documentation and removed bug Something isn't working labels Jul 27, 2021
@jesus4497
Copy link

jesus4497 commented May 26, 2022

I'm facing the same issue! any updates on this @danielroe ?

@ashish141199
Copy link

Any updates ? Same issue.

@odranoelBR
Copy link

Yea, same here.
Can't test a composable with "useFetch" inside.

The useFetch don't execute outside of setup env.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
documentation Improvements or additions to documentation needs-repro Waiting for code that can reproduce the issue
Projects
None yet
Development

No branches or pull requests

6 participants