Migrate unit tests to Vitest#1332
Conversation
|
9f3dcf4 to
d55a66d
Compare
size-limit report 📦
|
48b1b7c to
cc349dc
Compare
| component = await fixture(html` <test-pharos-button>I am a button</test-pharos-button> `); | ||
| }); | ||
|
|
||
| afterEach(() => { |
There was a problem hiding this comment.
Vitest clears the DOM between test file, but not tests. Tests which reference the document directly need to include cleanup like this to prevent cross-test interference
There was a problem hiding this comment.
can you clarify what counts as 'referencing the document directly' here?
There was a problem hiding this comment.
Yeah, no problem, at least this is my understanding 😄
In some of the button tests like this one:
pharos/packages/pharos/src/components/button/pharos-button.test.ts
Lines 295 to 312 in 260a8b9
We are creating a form element, and passing it in to the fixture function as the parent element.
The fixture then mounts the component in that element and appends it to the page DOM using document.body.appendChild:
pharos/packages/pharos/src/test/fixture.ts
Lines 17 to 24 in 260a8b9
The test then runs and asserts thing, but Vitest browser won't clean up that form element from the DOM. It only does that automatically in between different test files, not individual tests.
Then, in the next test we do the same thing: add a form element, mount, etc.
But when it gets to this line, the selector will find 2 form elements in the page DOM, one from the previous test and one from the current test, and fail.
Adding the afterEach block will clear the DOM between tests to keep that from happening.
| await expect(component).toBeAccessible(); | ||
| }); | ||
|
|
||
| it('throws an error for an invalid variant value', async () => { |
There was a problem hiding this comment.
This is arguable a bug in the component we should fix, but for now just wanted to limit changes to the tests
| trigger.setAttribute('data-sheet-id', 'focus-sheet'); | ||
| document.body.appendChild(trigger); | ||
|
|
||
| const button = document.querySelector('#trigger') as HTMLButtonElement; |
There was a problem hiding this comment.
I'm not sure why this was reselcting the trigger with a querySelector before?
lizlove
left a comment
There was a problem hiding this comment.
Congrats ! 🥳
Had a quick clarifying question but otherwise 👏
| component = await fixture(html` <test-pharos-button>I am a button</test-pharos-button> `); | ||
| }); | ||
|
|
||
| afterEach(() => { |
There was a problem hiding this comment.
can you clarify what counts as 'referencing the document directly' here?
The DOM is cleared between each test file run in Vitest, and manually clearing it here breaks tests that rely on DOM setup before the fixture is called. We can manually clear the DOM in tests that need it, but we shouldn't do it in the fixture itself.
e204eb3 to
260a8b9
Compare
This change: (check at least one)
Is this a breaking change? (check one)
Is the: (complete all)
What does this change address?
Closes #1009 and #580
How does this change work?
This updates all of the unit tests to use Vitest, by translating the assertions from open-wc/sinon to vitest.
Additional context
A few notes:
updatedlifecycle method. TheerrorFixturedoesn't work for those case. I added one-off error listening/suppression in these few spots rather that update the fixture to handle them as doing so really complicated it.selectedattribute set on a non-selectable variant of the image card #1331elementUpdated/waitUntilfor components that re-render asynchronously, those are nowvi.waitFor(() => expect(...))calls that wait on the settled state rather than guessing frame counts or timing.