Skip to content

Add unit testing guide - #841

Open
mtuchi wants to merge 5 commits into
mainfrom
unit-testing
Open

mtuchi wants to merge 5 commits into
mainfrom
unit-testing

Conversation

@mtuchi

@mtuchi mtuchi commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Short Description

Adds a new docs page, "Writing unit tests for your jobs", covering how to compile job code with openfn compile --exports-only and test the helper functions with a standard JavaScript test runner. Cross-linked from the job writing guide and the two CLI pages.

Closes #840

Details

  • New page docs/jobs/unit-testing-jobs.md, added to the sidebar under Write Jobs, after jobs/compilation
  • docs/jobs/job-writing-guide.md picks up a short "your helpers can be unit tested" pointer in Next Steps
  • docs/build-for-developers/cli-intro.md gains a bullet in the "you can use the OpenFn CLI to..." list
  • docs/build-for-developers/cli-usage.md gains a "Compile job code for unit testing" section

Walk through video

AI Usage

  • I have used Claude Code
  • I have used another model
  • I have not used AI

You can read more details in our
Responsible AI Policy

@mtuchi mtuchi changed the title wip: unit testing docs Add unit testing guide Sep 16, 2026
@mtuchi
mtuchi requested a review from PiusKariuki September 16, 2026 09:03
@mtuchi
mtuchi marked this pull request as ready for review September 16, 2026 09:03
Comment thread docs/jobs/job-writing-guide.md Outdated

@PiusKariuki PiusKariuki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is so good @mtuchi . I have just one minor comment for you.

Comment thread docs/build-for-developers/cli-usage.md Outdated
```

Compiled files are written to `dist/` as `.mjs`, mirroring your workflow
folders. Operations (`fn`, `get`, `each`) are stripped, so what's left is the

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This bit about operations is SUPER important

I think we need some kind of introduction saying that you can unit test the pure JS functions of your job code, but not the operations themselves.

This isn't just a detail or a caveat, it's core to understanding what unit testing a job means

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Also you can compile without exports-only and the operations are left in your code. They're just unusable!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@josephjclark, there are some weird hack you have to do if your job code is using collections
i was trying to avoid that

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have break down what it means "job code unit testing" and explain what is testable

Comment thread docs/build-for-developers/cli-intro.md Outdated
Comment thread docs/build-for-developers/cli-intro.md Outdated
Comment thread docs/build-for-developers/cli-usage.md Outdated
Comment thread docs/build-for-developers/cli-usage.md Outdated
Comment thread docs/jobs/job-writing-guide.md Outdated
Comment thread docs/jobs/unit-testing-jobs.md Outdated
@mtuchi
mtuchi requested a review from josephjclark September 17, 2026 14:29

@josephjclark josephjclark left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

AI isn't ready to write out docsite yet.

I don't like it. The more closely I read this the more problems I find :(

I think I'd rather have a really short, concise guide to unit testing which doesn't have all this explanation. It's long and repetetive and confusing - I'm not convinced we need 500 lines of this.


### Compile job code for unit testing

So you want to write unit tests against your job code? Start here.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should you start here? Or should you start in unit-testing-jobs?

I've just looked closely at where these docs are and they're fartoo verbose for the basic usage section of the CLI.

You should just list the basic commands for testing and then refernce the main unit-testing-jobs page


:::

## Why you need to compile first

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We haven't mentioned compilation yet. Why are you talking about compilation right at the top of the unit testing docs? Confusing for new readers.

`openfn compile` turns job expressions into ordinary ES modules on disk. Once
that's done, your test runner can import them like any other JavaScript.

## What you can and can't unit test

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I get the point but this feels a little over-explained? The table makes this feel way more complicated than this is


## Step 1: Export any helper you want to test

This is the most important thing on this page.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Is it? 🤔


This is the most important thing on this page.

When you compile with `--exports-only`, the compiler keeps **only exported

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We haven't explained compuling with --exports-only yet, so it's weird that we're explaining this now

`dirs.compiled` in `openfn.yaml` - ignore that folder instead. Your `workflows/`
folder and `openfn.yaml` are source, and should stay tracked.

### Install your adaptors as devDependencies

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

hmm. Isn't this only here if you don't do --exports-only? This feels like a real stretch. Also the example assumes github I think. What if you're using circleCI? GitLab? Azure something?


`npm test` runs `compile` first, so the tests always run against fresh output.

## Troubleshooting

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't love it - are you sure all these are useful problems to include all these docs for? It feels like bloat to me.


## Reference

### `openfn compile` flags

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe better to just say run openfn compile --help here? No way will I remember to update this list when updating the compile command

compiled: dist # where openfn compile writes output
```

## Next steps

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

None of these are "next steps". They might be Related Pages I guess?


- [Compilation](/documentation/jobs/compilation) - what the compiler does and
why
- [Best Practices](/documentation/jobs/best-practices) - writing job code that's

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That's a great point does our best practice doc encourage writing testable functions?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

add a guide on unit testing job code

3 participants