@sanity/cli-test 0.0.0-20260410130107

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +260 -0
  3. package/dist/index.d.ts +531 -0
  4. package/dist/index.js +13 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/test/captureOutput.js +83 -0
  7. package/dist/test/captureOutput.js.map +1 -0
  8. package/dist/test/constants.js +24 -0
  9. package/dist/test/constants.js.map +1 -0
  10. package/dist/test/createTestClient.js +53 -0
  11. package/dist/test/createTestClient.js.map +1 -0
  12. package/dist/test/createTestToken.js +13 -0
  13. package/dist/test/createTestToken.js.map +1 -0
  14. package/dist/test/mockApi.js +16 -0
  15. package/dist/test/mockApi.js.map +1 -0
  16. package/dist/test/mockSanityCommand.js +71 -0
  17. package/dist/test/mockSanityCommand.js.map +1 -0
  18. package/dist/test/mockTelemetry.js +24 -0
  19. package/dist/test/mockTelemetry.js.map +1 -0
  20. package/dist/test/setupFixtures.js +140 -0
  21. package/dist/test/setupFixtures.js.map +1 -0
  22. package/dist/test/snapshotSerializer.js +12 -0
  23. package/dist/test/snapshotSerializer.js.map +1 -0
  24. package/dist/test/testCommand.js +19 -0
  25. package/dist/test/testCommand.js.map +1 -0
  26. package/dist/test/testFixture.js +112 -0
  27. package/dist/test/testFixture.js.map +1 -0
  28. package/dist/test/testHook.js +43 -0
  29. package/dist/test/testHook.js.map +1 -0
  30. package/dist/utils/fileExists.js +13 -0
  31. package/dist/utils/fileExists.js.map +1 -0
  32. package/dist/utils/paths.js +66 -0
  33. package/dist/utils/paths.js.map +1 -0
  34. package/dist/vitest.d.ts +121 -0
  35. package/dist/vitest.js +22 -0
  36. package/dist/vitest.js.map +1 -0
  37. package/dist/vitestWorker.js +135 -0
  38. package/dist/vitestWorker.js.map +1 -0
  39. package/fixtures/basic-app/package.json +25 -0
  40. package/fixtures/basic-app/sanity.cli.ts +12 -0
  41. package/fixtures/basic-app/src/App.css +20 -0
  42. package/fixtures/basic-app/src/App.tsx +26 -0
  43. package/fixtures/basic-app/src/ExampleComponent.css +84 -0
  44. package/fixtures/basic-app/src/ExampleComponent.tsx +38 -0
  45. package/fixtures/basic-app/tsconfig.json +17 -0
  46. package/fixtures/basic-functions/functions/test-function/index.ts +7 -0
  47. package/fixtures/basic-functions/package.json +14 -0
  48. package/fixtures/basic-functions/sanity.blueprint.ts +5 -0
  49. package/fixtures/basic-studio/package.json +28 -0
  50. package/fixtures/basic-studio/sanity.cli.ts +11 -0
  51. package/fixtures/basic-studio/sanity.config.ts +18 -0
  52. package/fixtures/basic-studio/schemaTypes/author.ts +52 -0
  53. package/fixtures/basic-studio/schemaTypes/blockContent.ts +71 -0
  54. package/fixtures/basic-studio/schemaTypes/category.ts +20 -0
  55. package/fixtures/basic-studio/schemaTypes/index.ts +6 -0
  56. package/fixtures/basic-studio/schemaTypes/post.ts +67 -0
  57. package/fixtures/basic-studio/tsconfig.json +17 -0
  58. package/fixtures/federated-studio/package.json +24 -0
  59. package/fixtures/federated-studio/sanity.cli.ts +14 -0
  60. package/fixtures/federated-studio/sanity.config.ts +17 -0
  61. package/fixtures/federated-studio/schemaTypes/index.ts +1 -0
  62. package/fixtures/federated-studio/tsconfig.json +17 -0
  63. package/fixtures/graphql-studio/package.json +25 -0
  64. package/fixtures/graphql-studio/sanity.cli.ts +20 -0
  65. package/fixtures/graphql-studio/sanity.config.ts +37 -0
  66. package/fixtures/graphql-studio/schemaTypes/author.ts +52 -0
  67. package/fixtures/graphql-studio/schemaTypes/blockContent.ts +71 -0
  68. package/fixtures/graphql-studio/schemaTypes/category.ts +20 -0
  69. package/fixtures/graphql-studio/schemaTypes/event.ts +25 -0
  70. package/fixtures/graphql-studio/schemaTypes/index.ts +10 -0
  71. package/fixtures/graphql-studio/schemaTypes/post.ts +67 -0
  72. package/fixtures/graphql-studio/tsconfig.json +17 -0
  73. package/fixtures/multi-workspace-studio/package.json +28 -0
  74. package/fixtures/multi-workspace-studio/sanity.cli.ts +11 -0
  75. package/fixtures/multi-workspace-studio/sanity.config.ts +37 -0
  76. package/fixtures/multi-workspace-studio/schemaTypes/author.ts +52 -0
  77. package/fixtures/multi-workspace-studio/schemaTypes/blockContent.ts +70 -0
  78. package/fixtures/multi-workspace-studio/schemaTypes/category.ts +20 -0
  79. package/fixtures/multi-workspace-studio/schemaTypes/index.ts +6 -0
  80. package/fixtures/multi-workspace-studio/schemaTypes/post.ts +67 -0
  81. package/fixtures/multi-workspace-studio/tsconfig.json +17 -0
  82. package/fixtures/prebuilt-app/README.md +3 -0
  83. package/fixtures/prebuilt-app/dist/favicon.ico +0 -0
  84. package/fixtures/prebuilt-app/dist/index.html +102 -0
  85. package/fixtures/prebuilt-app/dist/static/sanity-CtOxKsdo.css +24 -0
  86. package/fixtures/prebuilt-app/dist/static/sanity-D4a4eOYZ.js +17 -0
  87. package/fixtures/prebuilt-app/package.json +25 -0
  88. package/fixtures/prebuilt-app/sanity.cli.ts +12 -0
  89. package/fixtures/prebuilt-app/src/App.css +20 -0
  90. package/fixtures/prebuilt-app/src/App.tsx +24 -0
  91. package/fixtures/prebuilt-app/tsconfig.json +17 -0
  92. package/fixtures/prebuilt-studio/README.md +3 -0
  93. package/fixtures/prebuilt-studio/dist/favicon.ico +0 -0
  94. package/fixtures/prebuilt-studio/dist/index.html +113 -0
  95. package/fixtures/prebuilt-studio/dist/static/sanity-DxH-rpFr.js +9 -0
  96. package/fixtures/prebuilt-studio/package.json +25 -0
  97. package/fixtures/prebuilt-studio/sanity.cli.ts +11 -0
  98. package/fixtures/prebuilt-studio/sanity.config.ts +11 -0
  99. package/fixtures/prebuilt-studio/tsconfig.json +17 -0
  100. package/fixtures/worst-case-studio/.env +1 -0
  101. package/fixtures/worst-case-studio/README.md +22 -0
  102. package/fixtures/worst-case-studio/fieldComponentsTest.tsx +80 -0
  103. package/fixtures/worst-case-studio/package.json +33 -0
  104. package/fixtures/worst-case-studio/sanity.cli.ts +16 -0
  105. package/fixtures/worst-case-studio/sanity.config.tsx +55 -0
  106. package/fixtures/worst-case-studio/src/browserGlobalsCheck.ts +14 -0
  107. package/fixtures/worst-case-studio/src/defines.ts +8 -0
  108. package/fixtures/worst-case-studio/src/descriptionIcon.svg +7 -0
  109. package/fixtures/worst-case-studio/src/descriptionInput.module.css +13 -0
  110. package/fixtures/worst-case-studio/src/descriptionInput.tsx +55 -0
  111. package/fixtures/worst-case-studio/src/schemaTypes/author.ts +52 -0
  112. package/fixtures/worst-case-studio/src/schemaTypes/blockContent.ts +70 -0
  113. package/fixtures/worst-case-studio/src/schemaTypes/category.ts +20 -0
  114. package/fixtures/worst-case-studio/src/schemaTypes/index.ts +6 -0
  115. package/fixtures/worst-case-studio/src/schemaTypes/post.ts +71 -0
  116. package/fixtures/worst-case-studio/src/typings.d.ts +37 -0
  117. package/fixtures/worst-case-studio/tsconfig.json +22 -0
  118. package/package.json +97 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Sanity.io
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,260 @@
1
+ # @sanity/cli-test
2
+
3
+ Provides test helpers for the Sanity CLI.
4
+
5
+ ## Quick Start
6
+
7
+ ### 1. Set up vitest global setup
8
+
9
+ Add the cli-test vitest setup to your `vitest.config.ts`:
10
+
11
+ ```ts
12
+ import {defineConfig} from 'vitest/config'
13
+
14
+ export default defineConfig({
15
+ test: {
16
+ globalSetup: ['@sanity/cli-test/vitest'],
17
+ },
18
+ })
19
+ ```
20
+
21
+ This will automatically copy and install dependencies for all bundled fixtures before tests run.
22
+
23
+ ### 2. Use test fixtures in your tests
24
+
25
+ ```ts
26
+ import {testFixture} from '@sanity/cli-test'
27
+ import {describe, test} from 'vitest'
28
+
29
+ describe('my test suite', () => {
30
+ test('should work with basic-studio', async () => {
31
+ const cwd = await testFixture('basic-studio')
32
+ // The fixture is now available at `cwd` with dependencies installed
33
+ // Tests that need built output should build explicitly:
34
+ // await buildFixture(cwd)
35
+ })
36
+ })
37
+ ```
38
+
39
+ ## API
40
+
41
+ ### `testFixture(fixtureName: string, options?: TestFixtureOptions): Promise<string>`
42
+
43
+ Creates an isolated copy of a bundled fixture for testing. Returns the absolute path to the temporary directory containing the fixture.
44
+
45
+ **Parameters:**
46
+
47
+ - `fixtureName` - Name of the fixture to copy (e.g., 'basic-app', 'basic-studio')
48
+ - `options.tempDir` - Optional custom temp directory path (defaults to `process.cwd()/tmp`)
49
+
50
+ **Returns:** Absolute path to the temporary fixture directory
51
+
52
+ **Available Fixtures:**
53
+
54
+ - `basic-app` - Basic Sanity application
55
+ - `basic-studio` - Basic Sanity Studio
56
+ - `multi-workspace-studio` - Multi-workspace Sanity Studio
57
+ - `worst-case-studio` - Stress-test Sanity Studio
58
+
59
+ **Example:**
60
+
61
+ ```ts
62
+ import {testFixture} from '@sanity/cli-test'
63
+
64
+ const cwd = await testFixture('basic-studio')
65
+ // Fixture is ready at `cwd` with dependencies installed
66
+ // Note: Fixtures are NOT built by default - tests should build if needed
67
+ ```
68
+
69
+ ### `setup(options?: SetupTestFixturesOptions): Promise<void>`
70
+
71
+ Vitest global setup function that copies fixtures and installs dependencies. This is automatically called by vitest when using `@sanity/cli-test/vitest` in your globalSetup config.
72
+
73
+ **Parameters:**
74
+
75
+ - `options.additionalFixtures` - Glob patterns for additional fixture directories from your local repo to set up alongside the default bundled fixtures (e.g., `['fixtures/*', 'dev/*']`). Only directories containing a `package.json` are included.
76
+ - `options.tempDir` - Custom temp directory path (defaults to `process.cwd()/tmp`)
77
+
78
+ **Note:** Fixtures are NOT built during setup. Tests that need built output should build explicitly.
79
+
80
+ **Adding fixtures from your local repo:**
81
+
82
+ If your repo has its own fixture directories that you want to test alongside the default bundled fixtures, use the `additionalFixtures` option to include them:
83
+
84
+ ```ts
85
+ // vitest.setup.ts
86
+ import {setup as cliTestSetup, teardown} from '@sanity/cli-test/vitest'
87
+
88
+ export {teardown}
89
+
90
+ export async function setup(project) {
91
+ return cliTestSetup(project, {
92
+ additionalFixtures: ['fixtures/*', 'dev/*'],
93
+ })
94
+ }
95
+ ```
96
+
97
+ ```ts
98
+ // vitest.config.ts
99
+ import {defineConfig} from 'vitest/config'
100
+
101
+ export default defineConfig({
102
+ test: {
103
+ globalSetup: ['vitest.setup.ts'],
104
+ },
105
+ })
106
+ ```
107
+
108
+ ### `teardown(options?: TeardownTestFixturesOptions): Promise<void>`
109
+
110
+ Vitest global teardown function that removes the temp directory. This is automatically called by vitest when using `@sanity/cli-test/vitest`.
111
+
112
+ **Parameters:**
113
+
114
+ - `options.tempDir` - Custom temp directory path (defaults to `process.cwd()/tmp`)
115
+
116
+ ### `setupWorkerBuild(filePaths: string[]): Promise<void>`
117
+
118
+ Utility function to compile TypeScript worker files (`.worker.ts`) to JavaScript for use in tests. Must be integrated into a custom vitest global setup file.
119
+
120
+ **Parameters:**
121
+
122
+ - `filePaths` - Array of paths to `.worker.ts` files to compile
123
+
124
+ **Features:**
125
+
126
+ - Compiles TypeScript to JavaScript using SWC for fast compilation
127
+ - Generates source maps for debugging
128
+ - Automatically watches for changes in watch mode (detects `VITEST_WATCH=true` or `--watch` flag)
129
+ - Handles files from both `@sanity/cli` and `@sanity/cli-core` packages
130
+
131
+ **Note:** This is a utility function, NOT automatically called. See the "Worker Files" section for integration examples.
132
+
133
+ **Example:**
134
+
135
+ ```ts
136
+ // test/workerBuild.ts
137
+ import {setupWorkerBuild} from '@sanity/cli-test/vitest'
138
+ import {glob} from 'tinyglobby'
139
+
140
+ export async function setup() {
141
+ const workerFiles = await glob('**/*.worker.ts', {
142
+ ignore: ['**/node_modules/**', '**/dist/**'],
143
+ })
144
+ return setupWorkerBuild(workerFiles)
145
+ }
146
+ ```
147
+
148
+ ### `teardownWorkerBuild(): Promise<void>`
149
+
150
+ Utility function to clean up worker build artifacts and close file watchers. Must be integrated into a custom vitest global setup file.
151
+
152
+ **Features:**
153
+
154
+ - Closes file watchers if in watch mode
155
+ - Deletes all compiled `.js` files that were generated from `.worker.ts` files
156
+ - Clears internal tracking of compiled files
157
+
158
+ **Note:** This is a utility function, NOT automatically called. See the "Worker Files" section for integration examples.
159
+
160
+ ### `testCommand(command: Command, args?: string[])`
161
+
162
+ Runs the given command with the given arguments and returns the output.
163
+
164
+ ```ts
165
+ const {stdout} = await testCommand(DevCommand, ['--host', '0.0.0.0', '--port', '3000'])
166
+ ```
167
+
168
+ ### `mockApi(api: ApiClient)`
169
+
170
+ Mocks the sanity/client calls.
171
+
172
+ ```ts
173
+ mockApi({
174
+ apiVersion: '2024-01-17',
175
+ method: 'get',
176
+ uri: '/users/me',
177
+ query: {
178
+ recordType: 'user',
179
+ },
180
+ }).reply(200, {
181
+ id: 'user-id',
182
+ name: 'John Doe',
183
+ email: 'john.doe@example.com',
184
+ })
185
+ ```
186
+
187
+ ## How It Works
188
+
189
+ This package bundles pre-configured Sanity fixtures that can be used for testing. When you call `testFixture()`:
190
+
191
+ 1. It creates a unique temporary copy of the requested fixture
192
+ 2. Symlinks the node_modules directory from the global setup version (for performance)
193
+ 3. Returns the path to the isolated test directory
194
+
195
+ The fixtures work identically whether this package is used in a monorepo or installed from npm.
196
+
197
+ ## Building Fixtures
198
+
199
+ Fixtures are NOT built during global setup or when calling `testFixture()`. Tests that need built output should build explicitly:
200
+
201
+ ```ts
202
+ import {exec} from 'node:child_process'
203
+ import {promisify} from 'node:util'
204
+
205
+ const execAsync = promisify(exec)
206
+
207
+ const cwd = await testFixture('basic-studio')
208
+ // Build the fixture before running tests that need it
209
+ await execAsync('npx sanity build --yes', {cwd})
210
+ ```
211
+
212
+ ## Worker Files
213
+
214
+ Worker files (`.worker.ts`) are TypeScript files that run in separate threads or processes. This package provides utilities to compile these files for testing, but they must be integrated into a custom vitest global setup file.
215
+
216
+ ### Setting Up Worker Compilation
217
+
218
+ **Step 1: Create a worker setup file** (e.g., `test/workerBuild.ts`):
219
+
220
+ ```ts
221
+ import {setupWorkerBuild, teardownWorkerBuild} from '@sanity/cli-test/vitest'
222
+ import {glob} from 'tinyglobby'
223
+
224
+ export async function setup() {
225
+ // Find all .worker.ts files in your project
226
+ const workerFiles = await glob('**/*.worker.ts', {
227
+ cwd: process.cwd(),
228
+ ignore: ['**/node_modules/**', '**/dist/**'],
229
+ })
230
+
231
+ return setupWorkerBuild(workerFiles)
232
+ }
233
+
234
+ export async function teardown() {
235
+ return teardownWorkerBuild()
236
+ }
237
+ ```
238
+
239
+ **Step 2: Add to vitest config:**
240
+
241
+ ```ts
242
+ // vitest.config.ts
243
+ import {defineConfig} from 'vitest/config'
244
+
245
+ export default defineConfig({
246
+ test: {
247
+ globalSetup: [
248
+ 'test/workerBuild.ts', // Your worker setup
249
+ '@sanity/cli-test/vitest', // Fixture setup
250
+ ],
251
+ },
252
+ })
253
+ ```
254
+
255
+ **Features:**
256
+
257
+ - Compiles TypeScript to JavaScript using SWC for fast compilation
258
+ - Generates source maps for debugging
259
+ - Automatically watches for changes in watch mode (detects `VITEST_WATCH=true` or `--watch` flag)
260
+ - Cleans up compiled `.js` files after tests complete