vuejs / vue-jest

Jest Vue transformer
MIT License
746 stars 157 forks source link

Error about the 'A list of blog posts' chapter #432

Open crayonape opened 2 years ago

crayonape commented 2 years ago

https://next.vue-test-utils.vuejs.org/guide/advanced/http-requests.html#a-list-of-blog-posts

// Following lines tell Jest to mock any call to `axios.get`
// and to return `mockPostList` instead
jest.mock('axios', () => ({
  get: jest.fn(() => mockPostList)
}))

The demo in this chapter failed on my laptop.

image image

seems axios finished before starting to find the [role="alert"] element.

crayonape commented 2 years ago

image

I'm using "jest": "^27.4.5", Jest mock seems to make the axios get a sync function, which was async.

jest.mock('axios', () => ({
  get: jest.fn(() => mockPostList)
}))

So, if I make the mock function async, the test will pass.

jest.mock("axios", () => ({
  get: jest.fn(async () => {
    await Promise.resolve().then();  //nextTick() will be the same
    return mockPostList;
  }),
}));
crayonape commented 2 years ago

And, maybe it's not the right place. But as a beginner, I wish I can dive into this project deeper. Sadlly I don't know where to start. Would it be a bother asking you to give me some advice?