Page MenuHomePhabricator

Standardize JEST unit test names and definition
Closed, ResolvedPublic1 Estimated Story Points

Description

As a developer, I would like the unit test to be well structured so that I will easily be able to contribute and add more unit tests and/or modify existing one while working on codex.

Context
The current Jest unit test do not have a common structure and can be hard to contribute too. An example can be the lookup tests. This file is very well tested, but all tests are set in one big group. A developer would need to read all the tests to make sure he can add a new one without duplication.

Change proposes
A suggested change would be to separate the tests into different groups. This can easily be done using nested describes. Furthermore this will allow the test itself to also be simplified as it will allow setup tests to be added into its own group.

  • Break up tests into groups
  • Update the Documentation to explain how to write and breakup tests
  • Simplify tests by moving reusable setup

Event Timeline

There are a very large number of changes, so older changes are hidden. Show Older Changes

Simplify tests by moving reusable setup

I have a patch pending, https://gerrit.wikimedia.org/r/c/design/codex/+/789841, that seeks to do this by adding shortcut expectations for common parts of a test, like that an element is visible. If you want to take a look

Thank you @DannyS712 , I was refering more to increase the use of "beforeEach" and "afterEach" to define standard components and props.

Change 811997 had a related patch set uploaded (by Simone Cuomo; author: Simone Cuomo):

[design/codex@main] [POC] Standardize JEST unit test names and definition

https://gerrit.wikimedia.org/r/811997

I have created a POC patch with updated documentation to be used as a discussion point in JEST writing standards.

The suggestions are very opinionated and should be agreed upon within the team (to be discussed during engineer enclave)

Change 817370 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] tests: Reorganize Checkbox tests per new standards

https://gerrit.wikimedia.org/r/817370

Change 817371 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] tests: reorganize Lookup tests per new standards

https://gerrit.wikimedia.org/r/817371

I've pushed patches to demonstrate some of Simone's recommendations:

  • Checkbox: this is a pretty simple test file and the patch mostly shows how basic organization can help, and how the sentences formed by the "given, when, then" notation are nice both for test file readibility and pinpointing issues when a test fails
  • Lookup: this is a more complex test file and the reorganization led to a lot of significant changes and, IMO, improvements that allow you to get a clearer picture of what's being tested and what is covered by tests when you look at the file. I also updated all the tests that look at whether the menu is expanded to test the actual visibility of the menu, rather than the value of the expanded data property.

Some other notes:

  • There's a lot of leeway in terms of how closely we follow the "given, when, then" notation. For example, I decided not to have more than 3 describe blocks (including the first one, which just includes the name of the component), so in a couple of cases for Lookup, I included another "when" statement in the "then" part. Here's an example of that:
// '...' represents code removed for brevity
describe( 'Lookup', () => {
	describe( 'when the text input is focused', () => {
		...

		describe( 'and there are no menu items to show', () => {
			...

			it( 'if there is no-results slot content, opens menu and shows "no results" message', async () => {
				...
			} );
		} );
	} );
} );
  • One thing I haven't covered in these files is shared code in beforeEach and afterEach. TypeaheadSearch would be a good candidate for demonstrating that, as it already does this to some extent.
AnneT changed the task status from Open to In Progress.Aug 18 2022, 3:18 PM
AnneT claimed this task.

Change 811997 merged by jenkins-bot:

[design/codex@main] docs: Standardize JEST unit test names and structure

https://gerrit.wikimedia.org/r/811997

Change 817370 merged by jenkins-bot:

[design/codex@main] tests: Reorganize Checkbox tests per new standards

https://gerrit.wikimedia.org/r/817370

Change 817371 merged by jenkins-bot:

[design/codex@main] tests: reorganize Lookup tests per new standards

https://gerrit.wikimedia.org/r/817371

Change 828083 had a related patch set uploaded (by Catrope; author: Catrope):

[mediawiki/core@master] Update Codex from v0.1.0-alpha.10 to v0.1.0

https://gerrit.wikimedia.org/r/828083

Change 828083 merged by jenkins-bot:

[mediawiki/core@master] Update Codex from v0.1.0-alpha.10 to v0.1.1

https://gerrit.wikimedia.org/r/828083

ldelench_wmf subscribed.

Hey @AnneT , moved this one into the sprint board since it appears to be in progress - feel free to adjust if that's not correct!

AnneT moved this task from Inbox to Backlog on the Design-System-Team board.

Moving this to the backlog as we will continue converting existing test files to this format as we have time.

ldelench_wmf set the point value for this task to 1.Sep 19 2022, 3:37 PM
egardner lowered the priority of this task from High to Low.Jan 23 2023, 5:20 PM

Change 891613 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] code: Clean up TextInput files

https://gerrit.wikimedia.org/r/891613

Change 891613 merged by jenkins-bot:

[design/codex@main] code: Clean up TextInput files

https://gerrit.wikimedia.org/r/891613

Change 893051 had a related patch set uploaded (by VolkerE; author: VolkerE):

[mediawiki/core@master] Update Codex from v0.6.0 to v0.6.2

https://gerrit.wikimedia.org/r/893051

Change 893051 merged by jenkins-bot:

[mediawiki/core@master] Update Codex from v0.6.0 to v0.6.2

https://gerrit.wikimedia.org/r/893051

Change #1046782 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] Menu: Refactor tests using given-when-then structure

https://gerrit.wikimedia.org/r/1046782

Change #1046782 merged by jenkins-bot:

[design/codex@main] Menu: Refactor tests using given-when-then structure

https://gerrit.wikimedia.org/r/1046782

Change #1049640 had a related patch set uploaded (by Eric Gardner; author: Eric Gardner):

[mediawiki/core@master] Update Codex from 1.7.0 to 1.8.0

https://gerrit.wikimedia.org/r/1049640

Test wiki created on Patch demo by EGardner (WMF) using patch(es) linked to this task:
https://patchdemo.wmflabs.org/wikis/f74be9bc8e/w

Change #1049640 merged by jenkins-bot:

[mediawiki/core@master] Update Codex from 1.7.0 to 1.8.0

https://gerrit.wikimedia.org/r/1049640

Aklapper changed the task status from In Progress to Open.Mar 22 2025, 7:25 AM

Resetting task status from "In Progress" to "Open" as this task has been "in progress" for more than two years.

Test wiki on Patch demo by EGardner (WMF) using patch(es) linked to this task was deleted:

https://patchdemo-legacy.wmcloud.org/wikis/f74be9bc8e/w/

Change #1152683 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] tests: Update tests in docs package

https://gerrit.wikimedia.org/r/1152683

Change #1152684 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] tests: Update tests for composables

https://gerrit.wikimedia.org/r/1152684

Change #1152689 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] tests, Combobox, MenuButton: Add snapshot tests

https://gerrit.wikimedia.org/r/1152689

Change #1152691 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] tests: Fix snapshot test props, reorganize tests

https://gerrit.wikimedia.org/r/1152691

Change #1152692 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] tests: Reorganize tests and remove unneeded ones

https://gerrit.wikimedia.org/r/1152692

Change #1152695 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[design/codex@main] tests: Reorganize unit tests

https://gerrit.wikimedia.org/r/1152695

Change #1152683 merged by jenkins-bot:

[design/codex@main] tests: Update tests in docs package

https://gerrit.wikimedia.org/r/1152683

Change #1152684 merged by jenkins-bot:

[design/codex@main] tests: Update tests for composables

https://gerrit.wikimedia.org/r/1152684

Change #1152778 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[mediawiki/core@master] Update Codex from v2.0.0-rc.2 to v2.0.0

https://gerrit.wikimedia.org/r/1152778

Change #1152778 merged by jenkins-bot:

[mediawiki/core@master] Update Codex from v2.0.0-rc.2 to v2.0.0

https://gerrit.wikimedia.org/r/1152778

Change #1152692 merged by jenkins-bot:

[design/codex@main] tests: Reorganize tests and remove unneeded ones

https://gerrit.wikimedia.org/r/1152692

Change #1155734 had a related patch set uploaded (by Bernard Wang; author: Bernard Wang):

[mediawiki/core@master] Update Codex from v2.0.0 to v2.1.0

https://gerrit.wikimedia.org/r/1155734

Test wiki created on Patch demo by BWang (WMF) using patch(es) linked to this task:
https://patchdemo.wmcloud.org/wikis/25f905bb47/w/

Change #1155734 merged by jenkins-bot:

[mediawiki/core@master] Update Codex from v2.0.0 to v2.1.0

https://gerrit.wikimedia.org/r/1155734

Change #1152695 merged by jenkins-bot:

[design/codex@main] tests: Reorganize unit tests

https://gerrit.wikimedia.org/r/1152695

Change #1152689 merged by jenkins-bot:

[design/codex@main] tests, Combobox, MenuButton: Add snapshot tests

https://gerrit.wikimedia.org/r/1152689

Change #1152691 merged by jenkins-bot:

[design/codex@main] tests: Fix snapshot test props, reorganize tests

https://gerrit.wikimedia.org/r/1152691

@AnneT Removing task assignee as this open task has been assigned for more than two years - See the email sent on 2025-05-22.
Please assign this task to yourself again if you still realistically [plan to] work on this task - it would be welcome!
If this task has been resolved in the meantime, or should not be worked on by anybody ("declined"), please update its task status via "Add Action… 🡒 Change Status".
Also see https://www.mediawiki.org/wiki/Bug_management/Assignee_cleanup for tips how to best manage your individual work in Phabricator. Thanks!

This task has been recently resolved and will be closed after the next Codex release

Change #1163459 had a related patch set uploaded (by Anne Tomasevich; author: Anne Tomasevich):

[mediawiki/core@master] Update Codex from v2.1.0 to v2.2.0

https://gerrit.wikimedia.org/r/1163459

Change #1163459 merged by jenkins-bot:

[mediawiki/core@master] Update Codex from v2.1.0 to v2.2.0

https://gerrit.wikimedia.org/r/1163459