Unit testing with Jest is the cheapest place to catch a mapping bug in healthcare integration code, before a wrong date, a lost repeat or a swapped identifier reaches a chart. In most designs, a mapper that turns HL7 v2 messages into FHIR resources is a pure function, which makes it ideal for fast, exact tests.
Startup founders and engineering leaders whose products parse HL7 v2 or produce FHIR will find how to build synthetic fixtures, which edge cases every mapper needs, and what must stay out of test files and CI logs.
How should you use unit testing with Jest for HL7 v2 and FHIR mappers?
Use unit testing with Jest to run each mapper against small synthetic HL7 v2 messages and assert the exact FHIR output, element by element, with toStrictEqual. Agnotic Technologies keeps one fixture per edge case, never uses real patient data, and runs the suite on every commit so a mapping regression fails the build.
Building HL7 v2 or FHIR mapping code?
We write the mappers and the Jest suites that keep them correct.
The aim is not coverage for its own sake. Each test pins down one mapping decision, so when someone changes the mapper later, the decision cannot change silently with it.
How should mapping code be structured so it can be unit tested?
Mapping code is easiest to test when parsing, mapping and sending are separate functions. The mapper takes a parsed message and returns resources, with no network, clock or database calls inside it.
- Parser: raw HL7 v2 text in, a typed message object out, with segment and field access by position.
- Mapper: message object in, FHIR resources out, written as a pure function.
- Context passed in: the current time, the sending facility's time zone and the identifier systems arrive as arguments, not globals.
- Sender: posts the resources to the FHIR server and is tested separately with a mocked HTTP client.
With that split, a Jest test for the mapper is one function call and one assertion, and it runs in milliseconds.
How do you build fixtures without patient data?
Build fixtures by hand from synthetic values, one small message per case, and store them as files next to the tests. Never copy a production message into the repository, even with the names changed.
- Synthetic people: invented names, birth dates and identifiers that follow the right format but match no real person.
- One case per fixture: an admission with a missing middle name is its own file, named for the case it covers.
- Minimal segments: only the segments and fields the case needs, so a reader can see what is being tested.
- Expected output beside it: the FHIR resource the mapper must produce, as a JSON file or an inline object.
Real names, medical record numbers, birth dates, addresses and free-text notes from production must not appear in a fixture, a snapshot or a CI log. Build logs are often kept longer and shared more widely than the systems the data came from, so Agnotic treats them as places PHI must never reach.
Which edge cases does every HL7 v2 to FHIR mapper need?
Every mapper needs cases for missing data, repeats, escapes and dates, because those are where real feeds differ from the examples in the specification.
| Case | Fixture contains | Test asserts |
|---|---|---|
| Explicit null versus empty | One field sent as the HL7 v2 null value, one left empty | The two are handled as the design says, and an empty field adds no element |
| Repeating field | Two patient identifiers separated by the repeat character | Both identifiers are mapped, each with the right system |
| Escape sequences | Escaped field and component separators inside a text field | The original characters appear in the FHIR string |
| Time without a zone | A timestamp with hours and minutes but no offset | The facility zone is applied, or the value is reduced to a date |
| Partial date | Year and month only | A FHIR date with the same precision and no invented day |
| Unknown local code | A code missing from the mapping table | The original code is kept with its system and flagged |
HL7 FHIR R4 says that for the dateTime type, if hours and minutes are specified a time zone SHALL be populated, and that dates SHALL be valid dates. The instant type must be given at least to the second and must include a time zone.
That rule is why the time zone case matters. HL7 v2 timestamps often arrive without an offset, so the mapper must apply a known facility time zone or reduce the value to a date, and a test has to pin down which one it does.
Why use toStrictEqual for FHIR output?
Use toStrictEqual because FHIR output is judged by which elements exist, not only by their values. An element present with an undefined value can serialise differently from one that is absent, and toEqual hides that difference.
Jest's expect documentation says toEqual ignores object keys with undefined properties, undefined array items, array sparseness and object type mismatches, while toStrictEqual checks all of them, so an object with a key set to undefined does not strictly equal one without that key.
- Assert whole resources for small cases, so an unexpected extra element fails the test.
- Use expect.objectContaining only where a test is about one element and the rest is covered by other tests.
- Fix generated ids and timestamps through the injected context, instead of ignoring them in assertions.
Should mapper tests use snapshots?
Snapshots can work for large resources, but only when the team reviews every snapshot change like code. A snapshot updated without being read proves only that the output changed.
Jest's snapshot testing documentation says snapshot files should be committed alongside code changes and reviewed as part of code review, and that snapshots are not written automatically on a CI system unless --updateSnapshot is passed.
- Prefer explicit expected objects for edge cases, so the intended output is visible in the test itself.
- Use snapshots for full bundles where writing the expected value by hand is impractical.
- Keep snapshot files free of anything resembling real patient data, since they are committed and reviewed.
How do you test the FHIR side beyond the mapper's own output?
Test that the output is valid FHIR, as well as matching what you expected. A mapper can match its expected object exactly and still produce a resource the server rejects, because the expected object itself was wrong.
- Validate generated resources against the FHIR R4 base specification and any profiles you claim, in a separate test step.
- Check that references inside a bundle resolve to resources in the same bundle.
- Check that identifiers carry a system as well as a value.
- Run a small set of fixtures end to end against a test FHIR server in a slower scheduled job.
How should HL7 v2 parser tests differ from mapper tests?
Parser tests cover the wire format; mapper tests cover the meaning. Keeping them apart means a failing test tells you immediately whether the message was read wrongly or mapped wrongly.
- Parser tests: delimiters taken from MSH, repeats, components and subcomponents, escape sequences, and segments the parser does not recognise.
- Mapper tests: start from an already parsed message object, so they never depend on delimiter handling.
- Sender quirks: one fixture per sending system that deviates from the specification, named after the deviation rather than the customer.
What are the common mistakes when unit testing mappers with Jest?
The common mistakes when unit testing mappers with Jest either let real data into the test suite or make the tests too loose to catch the bugs that matter.
- Copying production messages into fixtures and masking them by hand.
- Testing only the happy-path admission message from the specification.
- Using toEqual, so missing or undefined elements go unnoticed.
- Updating snapshots in bulk whenever tests fail.
- Letting the mapper read the system clock, so tests pass or fail depending on the time of day.
- Printing whole messages to the console, which then ends up in CI logs.
A worked example: an admission message with a time zone gap
An ADT^A01 admission message carries the admit time in PV1-44 as a timestamp with hours and minutes but no offset. The first version of the mapper copies it straight into Encounter.period.start.
The fixture-based test fails strict validation because the dateTime has a time but no time zone. The team passes the facility's time zone into the mapper's context and adds the offset, then adds a second fixture where the zone is unknown and the mapper keeps only the date.
Both cases are now pinned by tests. A later refactor that drops the context argument fails the build before it ships.
What does unit testing mean for your integration build plan?
Plan the fixture library as part of the mapping work, not after it. Each mapping decision made with the integration or clinical lead becomes a fixture and a test on the same day.
- Agree the source feeds and message types in scope, and collect each sender's specification.
- Write the edge-case fixtures before the mapper code for each message type.
- Run Jest on every commit and FHIR validation on every merge.
- Review fixtures and snapshots for anything resembling real patient data as part of code review.
How does Agnotic test healthcare mapping code?
Agnotic writes HL7 v2 parsers and FHIR mappers as pure functions, builds synthetic fixture libraries for every edge case, and runs Jest and FHIR validation in your pipeline. No real patient data goes into the repository, the snapshots or the build logs.
Every mapping decision pinned by a test
Synthetic fixtures, strict assertions and validation on every build.
Related reading on Agnotic
Mapping, FHIR and testing practice.


