feat(sdk): add workflowMock.setEnv() and deprecate WORKFLOW_TEST_ENV_KEY#1186
Draft
toiroakr wants to merge 6 commits into
Draft
feat(sdk): add workflowMock.setEnv() and deprecate WORKFLOW_TEST_ENV_KEY#1186toiroakr wants to merge 6 commits into
toiroakr wants to merge 6 commits into
Conversation
Adds workflowMock.setEnv() so tests using the tailor-runtime Vitest environment can configure the env passed to job bodies through the same helper they use for setJobHandler / setWaitHandler — without touching process.env. The legacy WORKFLOW_TEST_ENV_KEY (TAILOR_TEST_WORKFLOW_ENV) env-var path remains supported as a fallback; the export is now marked @deprecated. workflowMock.setEnv() takes priority when both are set.
🦋 Changeset detectedLatest commit: 3c35a3c The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
⚡ pkg.pr.new@tailor-platform/sdk@tailor-platform/create-sdk
|
This comment has been minimized.
This comment has been minimized.
Self-review revealed two gaps:
- mock.test.ts did not verify that workflowMock.setEnv() wins over
vi.stubEnv(WORKFLOW_TEST_ENV_KEY, ...), or that the deprecated env-var
path still works when setEnv is not called.
- The workflow template's integration test exercised .trigger() but
never called workflowMock.setEnv() / workflowMock.reset(), so it did
not demonstrate the new helper.
Add two mock.test.ts cases for the priority/fallback contract, and
update the template to call workflowMock.reset() in beforeEach and
workflowMock.setEnv({ STAGE: 'test' }) in the integration test.
This comment has been minimized.
This comment has been minimized.
Match the docs decision (testing.md does not mention WORKFLOW_TEST_ENV_KEY either): the changeset still records the deprecation, but no longer names the legacy export. Users who relied on the env var will see the @deprecated JSDoc warning at the import site.
This comment has been minimized.
This comment has been minimized.
- Shallow-copy global env in trigger() so job bodies cannot mutate the source object across triggers, matching the env-var path which returns a fresh object from JSON.parse each call. - Tighten workflowMock.setEnv() parameter from Record<string, unknown> to TailorEnv to match the value type job bodies receive in their context. - Add afterEach(vi.unstubAllEnvs) to workflowMock describe block so a failing test cannot leak env stubs into subsequent tests. - Add drift-guard test asserting WORKFLOW_ENV_GLOBAL_KEY is identical between configure/job.ts and vitest/mock.ts. mock.ts must remain free of @/-alias imports because nested Vitest configs for integration tests do not resolve them, so the constant is re-declared instead of imported. - Reword testing.md "without any mocking" to "with real job bodies" since workflowMock.setEnv() is itself a mock helper.
This comment has been minimized.
This comment has been minimized.
Address Copilot review on #1186: - Validate fromGlobal is a non-null object before spreading. A non-object value (string, number, etc.) accidentally assigned to the global key now falls back to the env-var path instead of producing a surprising env shape. - Update WORKFLOW_ENV_GLOBAL_KEY JSDoc to accurately describe the duplicate declaration in src/vitest/mock.ts (it's re-declared, not re-exported, because the nested tailor-runtime Vitest config does not resolve @/ aliases), and point readers at the drift-guard test. - Add a regression test exercising the non-object fallback path.
This comment has been minimized.
This comment has been minimized.
Address Copilot review on #1186: the previous comment said the fallback path was used for "non-plain-object" values, but the runtime check (typeof === "object" && !== null) actually accepts arrays, class instances, etc. Rewrite the comment to describe what the check really does (reject null and non-object primitives) and note that non-plain-object cases can only arise from direct globalThis misuse, since the setEnv() type constrains callers to TailorEnv. No behavior change.
Code Metrics Report (packages/sdk)
Details | | main (d3d984f) | #1186 (b31316f) | +/- |
|--------------------|----------------|-----------------|-------|
+ | Coverage | 61.9% | 61.9% | +0.0% |
| Files | 363 | 363 | 0 |
| Lines | 12651 | 12657 | +6 |
+ | Covered | 7833 | 7840 | +7 |
+ | Code to Test Ratio | 1:0.4 | 1:0.4 | +0.0 |
| Code | 82915 | 82992 | +77 |
+ | Test | 34645 | 34709 | +64 |Code coverage of files in pull request scope (87.7% → 88.4%)
SDK Configure Bundle Size
Runtime Performance
Type Performance (instantiations)
Reported by octocov |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
workflowMock.setEnv()so tests using thetailor-runtimeVitest environment can configure theenvpassed to job bodies through the same helper used forsetJobHandler/setWaitHandler— without touchingprocess.env..trigger()now readsenvfrom aglobalThiskey written byworkflowMock.setEnv(), falling back toprocess.env[WORKFLOW_TEST_ENV_KEY]when the global is unset.WORKFLOW_TEST_ENV_KEY(TAILOR_TEST_WORKFLOW_ENV) as@deprecated; the export and env-var path remain supported for backwards compatibility.workflowMock.setEnv()takes priority when both are set.packages/create-sdkworkflow template (order-fulfillment.test.ts, README) to demonstrate the new helper instead ofvi.stubEnv(WORKFLOW_TEST_ENV_KEY, ...).packages/sdk/docs/testing.mdaccordingly.Motivation
workflowMockis the canonical entry point for mocking workflow behavior in tests. Routing the env injection through it (instead of a magic env var) removes the only env-var-based mock in the SDK, makes the API discoverable alongside the rest ofworkflowMock, and lets tests scope env state viaworkflowMock.reset()between cases.A longer-term plan to fully align local
.trigger()semantics with the platform'stailor.workflow.triggerJobFunction()is tracked separately; this PR is the incremental step that ships now while preserving the existing public API.Compatibility
Non-breaking. Existing tests using
vi.stubEnv(WORKFLOW_TEST_ENV_KEY, ...)continue to work via the fallback path.