-
Notifications
You must be signed in to change notification settings - Fork 266
Add tests using async components #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
onError(error, retry, fail, attempts) { | ||
fail() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this onError
is a bit redundant? The option is used to have some retry control, but here we're just failing no matter what.
onError(error, retry, fail, attempts) { | |
fail() | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if these error logs are somewhat expected, and we should just mock console for the test and call it a day… 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had this thought too. Some other tests also throw and error. Is there any benefit to mocking the console? If printing the error is correct, I don't keep it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to mute them to make it the output cleaner in the long run, but I don't care much either. Unless the console error is part of the expected output, then I spy it and assert the times called and so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I still believe the onError
bit can be removed)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can have our own Vue errorHandler
, to easily assert on it without mocking console.error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will make this change, thanks
setTimeout(() => { | ||
expect(wrapper.html()).toContain('Loading Component') | ||
}, 35) | ||
|
||
setTimeout(() => { | ||
expect(wrapper.html()).toContain('Async Component') | ||
done() | ||
}, 100) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use jest.useFakeTimers? Mocking the setTimeout
would make the test 10 times faster.
Something like this seems to work fine, but there may be something I'm not seeing:
setTimeout(() => { | |
expect(wrapper.html()).toContain('Loading Component') | |
}, 35) | |
setTimeout(() => { | |
expect(wrapper.html()).toContain('Async Component') | |
done() | |
}, 100) | |
await jest.advanceTimersByTime(35) | |
expect(wrapper.html()).toContain('Loading Component') | |
jest.advanceTimersByTime(100) | |
await flushPromises() | |
expect(wrapper.html()).toContain('Async Component') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not know jest had a fake timer, that is pretty cool.
@@ -0,0 +1,3 @@ | |||
<template> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just use any sfc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I can use one of the existing ones
}, 100) | ||
}) | ||
|
||
it('works with vue files', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isnt this test the same as the first one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, it is.
I can change this one to test this usage
defineAsyncComponent({
errorComponent: Hello
})
where errorComponent
is a regular import
at the top of the file
onError(error, retry, fail, attempts) { | ||
fail() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can have our own Vue errorHandler
, to easily assert on it without mocking console.error
Great reviews everyone, I will make the requested changes and update the PR. |
@lmiller1990 👍 I'm also in favor of using You could also add a test of a component with an |
For Does anyone have any good ideas for this? I had the idea for a
I am leaning towards the first, if something is simple enough the user can do it in user-and, just let them do it. People should absolutely write helpers if it makes their tests more concise. Give the power to the user! That said, if this going to come up a lot, we can preemptively solve it with a helper. My only fear is we end up many helper functions, which makes the library larger and more complex than it needs to be. I will also make the |
@lmiller1990 I tested a few async components in my app and ended up with the same conclusions 👍 I wanted to open an issue to talk about this, and gather your thoughts. I think it would be nice to offer a helper that would allow to mount an async component easily, either a different mount, or with a mount option, or just a helper to wrap the component to test. I completely understand that this would make the library bigger and that we might not want to do it, and simply write a good cookbook on the topic. |
Alternatively, if we can know ahead of time it's an |
Agree 100% |
I made most of the changes except With |
@afontcu can you confirm which thing you are 100% agreeing to (util function or user-land)? |
EDIT2: Basically the Vue updates the DOM with a micro task, but the |
^ This was my understanding - thanks for clarifying. We will stick with |
both! I'd start with docs and let user-land figure out a way if needed, and if this comes up a lot during alpha/beta stages, then we could reconsider. |
Let's continue discussing mount helpers in another issue: #108 We still have a console warning " is an experimental feature and its API will likely change." Don't know we need to silence this warnings - it's coming from Vue itself, for a good reason. I made the changes, I'll leave this open another day or so for anyone who might like to go over it again. |
#104
They work as expected! Caveats:
flushPromises
to make them resolve. This is pretty common in VTU. Just need good education around this. Evan does something similar in core to test them, see herecannot find innerHTML of parentElement
if an AsyncComponent is the root component.return this.vm.$el.parentElement
is null - I am not sure why. 🤔div
. See my "basic usage" test.Anyone see any other test cases we could add?