Skip to content
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

AAP-37878: move Chatbot test to vitest-browser-react #1470

Merged

Conversation

goneri
Copy link
Contributor

@goneri goneri commented Dec 19, 2024

Right now, our tests are failing due to some problem with happy-dom, the fake webbrowser
used to run the tests by react-testing-library. jsdom is an alternative but we cannot
use it without adjusting our tests and facing similar issues or limitations.

By moving to vitest-browser-react, we are now able to run the tests against a real browser
like Chrome or Firefox. We still have the option to run the tests "headless" for a CI environment.

vitest-browser-react is directly based on vitest-browser and allow us to stay in a standard
vite ecosystem. It also reuses some elements from react-testing-library, which explains why
the PR is not that large.

Overall, this change reduces the differences between our tests and the production environment,
and allow us to limit our exposure to the limitations of these dummy browser implementations.

ansible_ai_connect_chatbot/src/App.test.tsx Dismissed Show dismissed Hide dismissed
@goneri goneri force-pushed the goneri/AAP-37878-move-Chatbot-test-to-vitest-browser-react_30941 branch 9 times, most recently from 5f3f923 to e8a1a28 Compare December 19, 2024 22:31
Right now, our tests are failing due to some problem with `happy-dom`, the fake webbrowser
used to run the tests by `react-testing-library`. `jsdom` is an alternative but we cannot
use it without adjusting our tests and facing similar issues or limitations.

By moving to `vitest-browser-react`, we are now able to run the tests against a real browser
like Chrome or Firefox. We still have the option to run the tests "headless" for a CI environment.

`vitest-browser-react` is directly based on `vitest-browser` and allow us to stay in a standard
`vite` ecosystem. It also reuses some elements from `react-testing-library`, which explains why
the PR is not that large.

Overall, this change reduces the differences between our tests and the production environment,
and allow use to limit our exposure to the limitations of these dummy browser implementations.
@goneri goneri force-pushed the goneri/AAP-37878-move-Chatbot-test-to-vitest-browser-react_30941 branch from e8a1a28 to 75867e4 Compare December 19, 2024 23:00
@manstis
Copy link
Contributor

manstis commented Dec 19, 2024

Super star 🦸

I'll look tomorrow.

@goneri goneri requested a review from jameswnl December 19, 2024 23:32
Copy link
Contributor

@justjais justjais left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goneri++

Copy link
Contributor

@manstis manstis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 Just a few nit-picks.

I also had to run the following to be able to run tests locally:

npm install <!-- No sh1t Sherlock.
npx playwright install
npx playwright install-deps

The tests run successfully most of the time 🥳

There was one occasion when one failed (no big deal) but it highlighted a (possible) need for ansible_ai_connect_chatbot/src/__screenshots__/ in .gitignore.

document.body.appendChild(debugDiv);
rootDiv = document.createElement("div");
rootDiv.setAttribute("id", "root");
const view = render(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to declare const view = ... as it is unused.

test("Basic chatbot interaction", async () => {
mockAxios(200);
const view = await renderApp();
const textArea = page.getByLabelText("Send a message...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could call sendMessage("Hello") and remove L#128-131.

return view;
}

async function sendMessage(message: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter message is redundant.

});
mockAxios(200);
const view = await renderApp();
const textArea = page.getByLabelText("Send a message...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could call sendMessage("Hello") and remove L#173-176.

}
mockAxios(200, false, false, lotsOfRefDocs);
const view = await renderApp();
const textArea = page.getByLabelText("Send a message...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could call sendMessage("Hello") and remove L#219-222.

expect(ghIssueLinkSpy).toEqual(1);
// // Assert the size of the resulting documents in the query parameter is 30,
// // as the max defined, instead of the 35 being present.
// const url: string | undefined = ghIssueLinkSpy.mock.calls[0][0]?.toString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant.

test("Chat service returns 500", async () => {
mockAxios(500);
const view = await renderApp();
const textArea = page.getByLabelText("Send a message...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could call sendMessage("Hello") and remove L#257-260.

test("Chat service returns a timeout error", async () => {
mockAxios(-1, true, true);
await renderApp();
const textArea = page.getByLabelText("Send a message...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could call sendMessage("Hello") and remove L#269-271.

test("Chat service returns 429 Too Many Requests error", async () => {
mockAxios(429, true);
await renderApp();
const textArea = page.getByLabelText("Send a message...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could call sendMessage("Hello") and remove L#287-289.

test("Chat service returns an unexpected error", async () => {
mockAxios(-1, true);
const view = await renderApp();
const textArea = page.getByLabelText("Send a message...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could call sendMessage("Hello") and remove L#304-306.

@goneri
Copy link
Contributor Author

goneri commented Dec 20, 2024

Great, thank you for rhe reviews.

@goneri goneri merged commit afc1eaf into main Dec 20, 2024
11 checks passed
@goneri goneri deleted the goneri/AAP-37878-move-Chatbot-test-to-vitest-browser-react_30941 branch December 20, 2024 13:52
@TamiTakamiya
Copy link
Contributor

@goneri @manstis Thanks for the change!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants