Skip to content

test(workbench): consolidate unit-test mocking into cli-test#1556

Draft
gu-stav wants to merge 1 commit into
mainfrom
sdk-1886-workbench-cli
Draft

test(workbench): consolidate unit-test mocking into cli-test#1556
gu-stav wants to merge 1 commit into
mainfrom
sdk-1886-workbench-cli

Conversation

@gu-stav

@gu-stav gu-stav commented Jul 17, 2026

Copy link
Copy Markdown
Member

Description

Follow-up from the undeploy stack (SDK-1886), scoped to @sanity/workbench-cli. The package hand-rolled test scaffolding that @sanity/cli-test already ships: a mock Output factory, a partial ux mock, and the getGlobalCliClient mock triple duplicated across four files.

This adopts the shared exports so there's one source of truth: createMockOutput (promoted to @sanity/cli-test/test/util), mocks/cli-core/ux, and mocks/cli-core/apiClient. It also wires cli-test into the package — devDependency, tsconfig paths, and vitest tsconfigPaths.

What to review

  • createMockOutput lands in cli-test (test/util) and replaces the as unknown as Output casts.
  • The four deploy/undeploy/services tests now mock @sanity/cli-core by spreading the shared apiClient mock instead of hoisting their own getGlobalCliClient; deployWorkbenchApp also swaps its partial ux mock for the shared one.
  • eslint-config-cli now lets __tests__/** import devDependencies — needed by devTestHelpers.ts, which isn't a .test.ts file.

Testing

Covered by the existing workbench-cli unit suite; all tests pass. Typecheck, lint, format, and knip are clean (the pre-existing BrettInterface type noise on main is untouched and out of scope).

workbench-cli hand-rolled the same test scaffolding @sanity/cli-test
already ships: a mock Output factory, a partial ux mock, and the
getGlobalCliClient mock triple. Adopt the shared exports
(createMockOutput, mocks/cli-core/ux, mocks/cli-core/apiClient) and wire
cli-test into the package so there's a single source of truth.

Refs SDK-1886
@gu-stav

gu-stav commented Jul 17, 2026

Copy link
Copy Markdown
Member Author

This change is part of the following stack:

Change managed by git-spice.

@github-actions

github-actions Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

📦 Bundle Stats — @sanity/cli

Compared against main (cc064844)

@sanity/cli

Metric Value vs main (cc06484)
Internal (raw) 2.2 KB -
Internal (gzip) 838 B -
Bundled (raw) 11.20 MB -
Bundled (gzip) 2.11 MB -
Import time 871ms -1ms, -0.1%

bin:sanity

Metric Value vs main (cc06484)
Internal (raw) 782 B -
Internal (gzip) 423 B -
Bundled (raw) 9.90 MB -
Bundled (gzip) 1.78 MB -
Import time 2.26s -41ms, -1.8%

🗺️ View treemap · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — @sanity/cli-core

Compared against main (cc064844)

Metric Value vs main (cc06484)
Internal (raw) 108.6 KB -
Internal (gzip) 27.1 KB -
Bundled (raw) 21.76 MB -
Bundled (gzip) 3.46 MB -
Import time 775ms -13ms, -1.7%

🗺️ View treemap · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — @sanity/cli-build

Compared against main (cc064844)

@sanity/cli-build/_internal/build

Metric Value vs main (cc06484)
Internal (raw) 113.0 KB -
Internal (gzip) 28.4 KB -
Bundled (raw) 18.10 MB -
Bundled (gzip) 3.63 MB -
Import time 1.37s -4ms, -0.3%

@sanity/cli-build/_internal/env

Metric Value vs main (cc06484)
Internal (raw) 1.8 KB -
Internal (gzip) 644 B -
Bundled (raw) 1.31 MB -
Bundled (gzip) 333.8 KB -
Import time 127ms -1ms, -0.5%

@sanity/cli-build/_internal/extract

Metric Value vs main (cc06484)
Internal (raw) 8.6 KB -
Internal (gzip) 2.7 KB -
Bundled (raw) 152.8 KB -
Bundled (gzip) 38.9 KB -
Import time 248ms -0ms, -0.0%

🗺️ ./_internal/env · ./_internal/extract · @sanity/cli-build:./_internal/build treemap too large to embed · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — create-sanity

Compared against main (cc064844)

Metric Value vs main (cc06484)
Internal (raw) 908 B -
Internal (gzip) 483 B -
Bundled (raw) 931 B -
Bundled (gzip) 491 B -
Import time ❌ ChildProcess denied: node -
Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage Delta

No covered files changed in this PR.

Overall Coverage

Metric Coverage
Statements 77.7% (±0%)
Branches 68.9% (±0%)
Functions 73.3% (+ 0.1%)
Lines 78.2% (±0%)

const mockRequest = vi.fn()

vi.mock('@sanity/cli-core', async (importOriginal) => ({
...(await importOriginal<typeof import('@sanity/cli-core')>()),

@filmaj filmaj Jul 21, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two things:

  1. Don't import the entirety of @sanity/cli-core in the source-under-test (deployConfig.ts). If you do, then you are transitively importing JSDOM (a 42MB dependency) and that significantly slows down transpilation, which means tests will be impacted too. Instead, use the granular exports from @sanity/cli-core to pull in only what the module needs. Looks like deployConfig.ts only needs import {type Output, subdebug} from '@sanity/cli-core', so I would split that up into two import calls: import {type Output} from '@sanity/cli-core/types' and import {subdebug} from '@sanity/cli-core/debug'. That a) significantly speeds up building deployConfig.ts and b) also means that when testing the module, neither of those core imports need to be mocked since those particular exports and their import graphs are extremely lightweight.
  2. When you do await importOriginal in vi.mock calls, you are essentially pulling in the entire module you are trying to mock - which, while convenient, also significantly impacts transpilation time. vitest transpiles tests on demand on every test invocation, meaning all of sanity/cli-core is pulled into the tranpilation graph (including 42MB of JSDOM) to run this test. Not good! If you look at the end of the CI runs for the unit tests, there will be an import time breakdown. In CI, a vast majority of time for running tests is spent just processing the import graph and transpiling - on average, only about 10% of time is spent running tests (see e.g. this PR's recent run: 290 seconds importing things, 30 seconds to transpile, and only 25 seconds to run the tests!). Wherever we can, we should use a) the 'thin' cli-core exports and b) avoid these sorts of importOriginal calls for mocking.

The mocks provided by cli-tests for the cli-core exports are there and easy to use. They are extremely lightweight and provide a mock for every single export in cli-core, so in theory you should never need to importOriginal for any cli-core mocks. See e.g. this test: it vi.mocks cli-core/ux, doesn't need to call importOriginal, and just imports the cli-test mock for all the cli-core/ux APIs. Easy and super fast.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants