airflow icon indicating copy to clipboard operation
airflow copied to clipboard

TS-SDK: Test a task or a whole Dag locally

Open jason810496 opened this issue 1 week ago • 7 comments

Body

Dag authors using the TypeScript SDK have no supported way to test their Dags locally. It came up in the feedback on #76370, most concretely in this comment.

What authors run into today:

  • bundle.getTaskHandler returns the SDK's wrapper, so even a dag.if decider that compares two dates throws getClient() is only available inside a task handler.
  • To test a handler that calls getContext() or getClient(), you have to put a fake scope into the SDK's private storage. Any release can break that.
  • airflow dags test says it cannot find a TypeScript Dag, while the same Dag runs fine when scheduled.

Scope

1. Test a single task

Run one task, or one task handler, with given inputs, Variables, Connections and context. Check its result, its XComs and which downstream tasks it skips.

2. Test the whole Dag

Run the tasks of a Dag in the order its dependencies define, applying trigger rules and branches (dag.if, dag.switch). Check the final state and the XComs of each task.

Open question: where it lives

We have not picked the approach yet. The options so far:

  • A testing package in the SDK, such as @apache-airflow/ts-sdk/testing, that works with any test runner.
  • Expose the test method at Dag or Task level interface.
  • A TypeScript SDK CLI command, next to airflow-ts-pack.
  • The compiled bundle itself, run directly with something like node dist/bundle.min.mjs ..., so what you test is the artifact you deploy.
  • The Airflow CLI, for example airflow dags test working with TypeScript Dags.

Committer

  • [x] I acknowledge that I am a maintainer/committer of the Apache Airflow project.

jason810496 avatar Nov 26 '26 02:11 jason810496

Thanks @jason810496 for opening this issue. I would be interested in contributing to the TypeScript SDK's local testing capability.

My preference would be to start with a narrowly scoped public single-task testing API that allows DAG authors to provide task inputs, Variables, Connections and execution context without accessing private SDK storage.

I would also aim to include regression coverage for task results, XComs and branching behavior, keeping full-DAG execution as a separate follow-up.

Before starting implementation, could you confirm whether a public SDK testing package is the preferred direction and whether the single-task API would be a useful first PR?

I'm happy to work within the maintainers' preferred design and coordinate to avoid duplicating ongoing work.

adiranjan25 avatar Nov 26 '26 02:11 adiranjan25

Thanks for opening this. A few notes from the trial behind that comment.

For a single task, a testing package is what I wanted. My handler tests today put a fake scope in the SDK's private storage (F-09 in https://github.com/JoelEmmanuelCloud/airflow-ts-sdk-trial/blob/main/FRICTION.md). With a package they would only need inputs, Variables and the context.

Running the built bundle would test what actually gets deployed. In 1.0.0 errors there point at the minified file (F-14), so that option needs source maps to be usable.

Cases from the trial I can turn into tests for whichever option you pick:

  • a dag.if decider that only compares two dates (F-07)
  • a branch that skips one task, and the join after dag.switch (F-15)
  • an approval task with inputs

I'd like to take a part once the approach is settled.

JoelEmmanuelCloud avatar Nov 26 '26 04:11 JoelEmmanuelCloud

Thanks @JoelEmmanuelCloud for sharing these concrete scenarios. The examples from your trial are helpful, particularly the private execution-scope workaround and the branching cases.

I agree that a public SDK testing package would be a practical starting point for single-task testing without exposing internal storage.

I would be happy to take ownership of the initial single-task testing API, covering task inputs, Variables, Connections, execution context, results and XComs.

If you're interested, we could coordinate so that your dag.if, dag.switch and approval-task scenarios become regression tests for the public API.

@jason810496, would this division of work align with your preferred direction? I can start with a small API proposal before implementation to avoid overlapping changes.

adiranjan25 avatar Nov 26 '26 04:11 adiranjan25

I'd rather wait for Jason to pick the approach before we split the work.

If the single-task API goes to you, I'd like to take the whole-Dag part: running the tasks in dependency order, with trigger rules and the dag.if and dag.switch branches. The join after dag.switch (F-15) is a case from my trial that needs it.

JoelEmmanuelCloud avatar Nov 26 '26 04:11 JoelEmmanuelCloud

Thanks @JoelEmmanuelCloud, that sounds like a sensible approach.

I agree we should wait for @jason810496 to confirm the preferred architecture and scope before dividing the implementation.

I'm happy to take the single-task testing API if that direction is approved, while you focus on whole-DAG execution, dependency handling, trigger rules, and branching.

Once Jason confirms, we can coordinate the interfaces to ensure both contributions integrate cleanly without overlapping work.

Thanks!

adiranjan25 avatar Nov 26 '26 04:11 adiranjan25

Jarek replied on the Python testing thread (https://lists.apache.org/thread/t63tvnyrfj582wjmkfnsdborl4r70z9z) that a whole-Dag runner without Airflow core would copy the scheduler's rules into the SDK, and the two would drift. He'd rather dag.test() set up a throwaway database so the real scheduling code runs.

The same applies here, so I'd change my part: make airflow dags test work for TypeScript Dags. Today it says the Dag cannot be found (F-18 in my trial). Then a whole TypeScript Dag runs through the real scheduler, and the single-task API stays the Node-only option. I'll check what it needs before starting.

JoelEmmanuelCloud avatar Nov 30 '26 12:11 JoelEmmanuelCloud

Thanks, @JoelEmmanuelCloud, for sharing Jarek's guidance and revisiting the architecture.

I agree that whole-DAG testing should reuse Airflow's existing scheduler rather than reproduce dependency resolution, trigger rules, and branching behavior inside the TypeScript SDK. Extending airflow dags test to support TypeScript DAGs sounds like the more maintainable direction.

I'm still interested in taking ownership of the public single-task testing API as a separate, focused contribution.

My proposed scope would include:

  • Running individual TypeScript task handlers with supplied inputs and execution context.
  • Supporting configurable Variables and Connections without relying on private SDK storage.
  • Capturing task results and XCom behavior.
  • Adding regression coverage for dag.if deciders and relevant branching scenarios.

I would keep full-DAG scheduling and CLI integration outside this initial implementation to avoid overlapping with your work.

@jason810496, could you confirm whether this division aligns with the preferred architecture? If so, I can prepare a focused API design and initial regression tests before proceeding with implementation.

Thanks again for coordinating this.

adiranjan25 avatar Nov 30 '26 19:11 adiranjan25