@sanity/cli-test 0.0.2-alpha.0 → 0.0.2-alpha.10

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 (98) hide show
  1. package/README.md +228 -0
  2. package/dist/index.d.ts +517 -3
  3. package/dist/index.js +8 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/test/constants.js +21 -0
  6. package/dist/test/constants.js.map +1 -0
  7. package/dist/test/createTestClient.js +53 -0
  8. package/dist/test/createTestClient.js.map +1 -0
  9. package/dist/test/createTestToken.js +13 -0
  10. package/dist/test/createTestToken.js.map +1 -0
  11. package/dist/test/mockApi.js +5 -3
  12. package/dist/test/mockApi.js.map +1 -1
  13. package/dist/test/mockSanityCommand.js +68 -0
  14. package/dist/test/mockSanityCommand.js.map +1 -0
  15. package/dist/test/mockTelemetry.js +22 -0
  16. package/dist/test/mockTelemetry.js.map +1 -0
  17. package/dist/test/setupFixtures.js +137 -0
  18. package/dist/test/setupFixtures.js.map +1 -0
  19. package/dist/test/snapshotSerializer.js +12 -0
  20. package/dist/test/snapshotSerializer.js.map +1 -0
  21. package/dist/test/testCommand.js +11 -4
  22. package/dist/test/testCommand.js.map +1 -1
  23. package/dist/test/testFixture.js +112 -0
  24. package/dist/test/testFixture.js.map +1 -0
  25. package/dist/test/testHook.js +23 -7
  26. package/dist/test/testHook.js.map +1 -1
  27. package/dist/utils/fileExists.js +13 -0
  28. package/dist/utils/fileExists.js.map +1 -0
  29. package/dist/utils/paths.js +66 -0
  30. package/dist/utils/paths.js.map +1 -0
  31. package/dist/vitest.d.ts +116 -0
  32. package/dist/vitest.js +22 -0
  33. package/dist/vitest.js.map +1 -0
  34. package/dist/vitestWorker.js +135 -0
  35. package/dist/vitestWorker.js.map +1 -0
  36. package/fixtures/basic-app/package.json +26 -0
  37. package/fixtures/basic-app/sanity.cli.ts +12 -0
  38. package/fixtures/basic-app/src/App.css +20 -0
  39. package/fixtures/basic-app/src/App.tsx +26 -0
  40. package/fixtures/basic-app/src/ExampleComponent.css +84 -0
  41. package/fixtures/basic-app/src/ExampleComponent.tsx +38 -0
  42. package/fixtures/basic-app/tsconfig.json +17 -0
  43. package/fixtures/basic-studio/package.json +28 -0
  44. package/fixtures/basic-studio/sanity.cli.ts +11 -0
  45. package/fixtures/basic-studio/sanity.config.ts +18 -0
  46. package/fixtures/basic-studio/schemaTypes/author.ts +52 -0
  47. package/fixtures/basic-studio/schemaTypes/blockContent.ts +71 -0
  48. package/fixtures/basic-studio/schemaTypes/category.ts +20 -0
  49. package/fixtures/basic-studio/schemaTypes/index.ts +6 -0
  50. package/fixtures/basic-studio/schemaTypes/post.ts +67 -0
  51. package/fixtures/basic-studio/tsconfig.json +17 -0
  52. package/fixtures/multi-workspace-studio/package.json +28 -0
  53. package/fixtures/multi-workspace-studio/sanity.cli.ts +11 -0
  54. package/fixtures/multi-workspace-studio/sanity.config.ts +37 -0
  55. package/fixtures/multi-workspace-studio/schemaTypes/author.ts +52 -0
  56. package/fixtures/multi-workspace-studio/schemaTypes/blockContent.ts +70 -0
  57. package/fixtures/multi-workspace-studio/schemaTypes/category.ts +20 -0
  58. package/fixtures/multi-workspace-studio/schemaTypes/index.ts +6 -0
  59. package/fixtures/multi-workspace-studio/schemaTypes/post.ts +67 -0
  60. package/fixtures/multi-workspace-studio/tsconfig.json +17 -0
  61. package/fixtures/prebuilt-app/README.md +3 -0
  62. package/fixtures/prebuilt-app/dist/favicon.ico +0 -0
  63. package/fixtures/prebuilt-app/dist/index.html +102 -0
  64. package/fixtures/prebuilt-app/dist/static/sanity-CtOxKsdo.css +24 -0
  65. package/fixtures/prebuilt-app/dist/static/sanity-D4a4eOYZ.js +17 -0
  66. package/fixtures/prebuilt-app/package.json +26 -0
  67. package/fixtures/prebuilt-app/sanity.cli.ts +12 -0
  68. package/fixtures/prebuilt-app/src/App.css +20 -0
  69. package/fixtures/prebuilt-app/src/App.tsx +24 -0
  70. package/fixtures/prebuilt-app/tsconfig.json +17 -0
  71. package/fixtures/prebuilt-studio/README.md +3 -0
  72. package/fixtures/prebuilt-studio/dist/favicon.ico +0 -0
  73. package/fixtures/prebuilt-studio/dist/index.html +113 -0
  74. package/fixtures/prebuilt-studio/dist/static/sanity-DxH-rpFr.js +9 -0
  75. package/fixtures/prebuilt-studio/package.json +25 -0
  76. package/fixtures/prebuilt-studio/sanity.cli.ts +11 -0
  77. package/fixtures/prebuilt-studio/sanity.config.ts +11 -0
  78. package/fixtures/prebuilt-studio/tsconfig.json +17 -0
  79. package/fixtures/worst-case-studio/README.md +21 -0
  80. package/fixtures/worst-case-studio/package.json +32 -0
  81. package/fixtures/worst-case-studio/sanity.cli.ts +16 -0
  82. package/fixtures/worst-case-studio/sanity.config.tsx +48 -0
  83. package/fixtures/worst-case-studio/src/defines.ts +8 -0
  84. package/fixtures/worst-case-studio/src/descriptionIcon.svg +7 -0
  85. package/fixtures/worst-case-studio/src/descriptionInput.module.css +13 -0
  86. package/fixtures/worst-case-studio/src/descriptionInput.tsx +55 -0
  87. package/fixtures/worst-case-studio/src/schemaTypes/author.ts +52 -0
  88. package/fixtures/worst-case-studio/src/schemaTypes/blockContent.ts +70 -0
  89. package/fixtures/worst-case-studio/src/schemaTypes/category.ts +20 -0
  90. package/fixtures/worst-case-studio/src/schemaTypes/index.ts +6 -0
  91. package/fixtures/worst-case-studio/src/schemaTypes/post.ts +71 -0
  92. package/fixtures/worst-case-studio/src/typings.d.ts +37 -0
  93. package/fixtures/worst-case-studio/tsconfig.json +22 -0
  94. package/package.json +52 -22
  95. package/dist/test/captureOutput.d.ts +0 -33
  96. package/dist/test/mockApi.d.ts +0 -34
  97. package/dist/test/testCommand.d.ts +0 -6
  98. package/dist/test/testHook.d.ts +0 -8
package/README.md CHANGED
@@ -2,8 +2,161 @@
2
2
 
3
3
  Provides test helpers for the Sanity CLI.
4
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
+
5
39
  ## API
6
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
+
7
160
  ### `testCommand(command: Command, args?: string[])`
8
161
 
9
162
  Runs the given command with the given arguments and returns the output.
@@ -30,3 +183,78 @@ mockApi({
30
183
  email: 'john.doe@example.com',
31
184
  })
32
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