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
Improve Tests #1067
Comments
Hello, I am currently trying to write tests for Concerning the general testing issue:
The question is what is important. If I try to write tests, I try to do the following in decreasing importance:
I am fully open to other suggestions. This being a component library, one might also think about screenshot testing but I fear that is another can of worms which I would not recommend touching here. |
Thanks, and no worries. We should probably focus on one test file first, then derive and document conventions and best practices from that before jumping into others. A good place for this might be
This sounds like a good plan! Re: mocking — is there a reason why you're mocking IntersectionObserver? This shouldn't be necessary since the tests are running in real browsers. Another thing that's probably very opinionated, but also very helpful for me and other contributors who jump into tests is not reusing fixtures. In other words, instead of doing this: const createTabGroup = async (): Promise<SlTabGroup> => {
return fixture<SlTabGroup>(html`
<sl-tab-group>
<sl-tab slot="nav" panel="general" data-testid="general-tab-header">General</sl-tab>
<sl-tab slot="nav" panel="custom" data-testid="custom-tab-header">Custom</sl-tab>
<sl-tab slot="nav" panel="advanced" data-testid="advanced-tab-header">Advanced</sl-tab>
<sl-tab slot="nav" panel="disabled" disabled data-testid="disabled-tab-header">Disabled</sl-tab>
<sl-tab-panel name="general" data-testid="general-tab-content">This is the general tab panel.</sl-tab-panel>
<sl-tab-panel name="custom" data-testid="custom-tab-content">This is the custom tab panel.</sl-tab-panel>
<sl-tab-panel name="advanced" data-testid="advanced-tab-content">This is the advanced tab panel.</sl-tab-panel>
<sl-tab-panel name="disabled" data-testid="disabled-tab-content">This is a disabled tab panel.</sl-tab-panel>
</sl-tab-group>
`);
}; Can we recreate the fixture in every test, but reduce them to the minimal code necessary to faciliate the test? it('does something', async () => {
const tabGroup = await fixture<SlTabGroup>(html`
<sl-tab-group>
<sl-tab slot="nav" panel="general">General</sl-tab>
<sl-tab slot="nav" panel="custom">Custom</sl-tab>
<sl-tab slot="nav" panel="advanced">Advanced</sl-tab>
<sl-tab-panel name="general">This is the general tab panel.</sl-tab-panel>
<sl-tab-panel name="custom">This is the custom tab panel.</sl-tab-panel>
<sl-tab-panel name="advanced">This is the advanced tab panel.</sl-tab-panel>
</sl-tab-group>
`);
// ...
}); Yes, this creates more boilerplate, but it allows us to reduce each test case and update them independently as the project evolves without having to worry about breaking other tests as we add new things or make changes to existing fixtures. |
The honest answer is: Because it was required to make the test work. This can be fixed by updating the configuration to
but that has another downside. If you are running the test in headed mode, all the browsers really open. This blocks your PC during test execution and, if observed for the first time, is a bit scary. Apart from that: It takes time and resources to run and I am not sure whether the CI pipeline actually can do it. The screenshot shows how it looks on my machine
This is fully fine for me. I have rewritten the test to not to reuse the fixtures, see 44c7c73840af5dce84a167ae95235bb729b2ecc3 |
Thanks for clarifying. In that case, do you think it makes sense to split out the mocks so they're reusable for other (future) tests? At a glance, I don't see anything specific to tab group in the class, but it's definitely possible I'm overlooking something. |
It took me some time but I managed to extract it (see this commit). Hope the positioning of the files suits you. I also managed to add the first test on scrolling. |
This looks great, thanks!
Amazing! 🎉 |
Just kinda sniping in, but I noticed a number of issues similar about IO events not firing, does setting an initial window size do anything in headless mode? Example: playwrightLauncher({
launchOptions: {
headless: true,
args: ['--window-size=1920,1080'],
},
}), However the above is really only for chromium, I wonder if setting a default viewport size using import { setViewport } from '@web/test-runner-commands';
describe('my component', () => {
it('works on 360x640', async () => {
await setViewport({ width: 360, height: 640 });
})
}) |
Unfortunately this did not work. I tried both variants but still needed the mock for the resizeObserver |
Finally managed to get rid of the mocked observers (see this commit). The reason for it working in non headless browser beforehand was a different timing than in headless browser. Adding the correct waits helped |
@dhellgartner just a heads up that a number of tests were added/updated in It would be better to coordinate than to do this in the dark, as I've been considering doing some reorg in test files this week but I'd hate for that to impact you negatively. |
Sure: Here you go I also merged next branch into the feature branch -- luckily no conflicts Sorry for taking so long for completing these tests. |
After the first test is finally done, how to continue? |
That works for me. At a high level, we need a list of components we can "check off" as they get worked on to prevent other contributors from duplicating efforts. If you want to update My biggest ask is that we strive for consistency and document our approach. Simple > clever. I do this as much as possible with the code, but tests have been contributed from numerous sources so they're currently a mixed bag. I really appreciate your help with this! |
I tried to extend the tests for SlAlert (see #1189). |
Two things:
|
Added tests to sl-animated-image: #1246 |
And a small test for sl-animation: #1274 |
And a bit of testing for sl-split-panel #1343 |
And a round of tests for qr-code #1416 Note that I fixed a small bug along the way (the background color was not passed to the qr code). If you prefer the next time that I open a bug + PR please tell me ... I can also still scrap the PR and reopen the fixing part as a bug. |
Thanks, everyone! Now that we have a team, we'll be taking tests in house to get things cleaned up and more consistent. I really appreciate the effort that's gone into testing thus far! 🙌 |
Any help with tests are appreciated! Things that still need to be done:
docs/resources/contributing.md
I don't care about the number of tests or even coverage — I care about sensibility and making sure the important things are covered.
If you plan on assisting with this, please post below so we can coordinate. Thanks!
The text was updated successfully, but these errors were encountered: