Skip to main content

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
Compare plans

Add matching and nonmatching cases​

  1. Select JavaScript and Unit Tests mode.
  2. Enter ^ORD-[0-9]{4}$ as the pattern.
  3. Remove all flags, including m.
  4. Use Add unit test to create one case for each row:
InputTargetCriterion
ORD-0042regexdoes match
ORD-9999regexdoes match
ORD-42regexdoes not match
XORD-0001regexdoes not match
Empty inputregexdoes not match
  1. Select Run tests.
  2. 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.

Three captured states: both expectations pass, the shorter identifier causes a failure after the pattern changes, then the original pattern restores the result.

The example changes {4} to {2,4}. The negative test for ORD-42 fails. Restoring {4} makes both tests pass.