Keep expected results in unit tests
Unit tests compare a pattern with expected results. They preserve both examples that must match and examples that must fail.
- Free
- Included
- Pro
- Included
- Self-hosted
- Included
Add matching and nonmatching cases
- Select JavaScript and Unit Tests mode.
- Enter
^ORD-[0-9]{4}$as the pattern. - Remove all flags, including
m. - Use Add unit test to create one case for each row:
| Input | Target | Criterion |
|---|---|---|
ORD-0042 | regex | does match |
ORD-9999 | regex | does match |
ORD-42 | regex | does not match |
XORD-0001 | regex | does not match |
| Empty input | regex | does not match |
- Select Run tests.
- Verify that all five expectations pass.
To demonstrate a failure, change {4} to {2,4} and run the tests again.
The ORD-42 case now fails its does not match expectation.
Restore {4} before saving the workspace.
Each case has Test string, Assertion target, and Assertion controls. An expectation that compares text also has a Comparison string field. The Test description field identifies the purpose of the case.
Compare a captured value
The regex target supports does match and does not match. The full match and capture group targets support starts with, ends with, contains, and equals.
For \bORD-([0-9]{4})\b on ORD-0042 pending, group 1 equals 0042.
The full match equals ORD-0042.
A comparison expectation inspects the first match. It does not assert every match in a global result.
Capture groups also support is null where the engine exposes that distinction. This criterion is unavailable for Go. is null tests whether a group did not participate. A participating group with empty text does not satisfy that expectation.
Include boundaries
Test the empty string, an extra prefix, excess digits, and a trailing newline.
Without m, JavaScript requires this pattern to reach the end of the input.
The identifier guide explains a strict whole-input check.
Unit tests specify expected outcomes. Multiple test strings show several inputs beside one pattern for exploration.
The example changes {4} to {2,4}. The negative test for ORD-42 fails. Restoring {4} makes both tests pass.