@sanity/cli-test 0.0.2-alpha.7 → 0.0.2-alpha.8
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.
- package/README.md +31 -31
- package/dist/index.d.ts +517 -9
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/test/constants.js +19 -6
- package/dist/test/constants.js.map +1 -1
- package/dist/test/createTestClient.js +2 -0
- package/dist/test/createTestClient.js.map +1 -1
- package/dist/test/createTestToken.js +8 -1
- package/dist/test/createTestToken.js.map +1 -1
- package/dist/test/mockApi.js +5 -3
- package/dist/test/mockApi.js.map +1 -1
- package/dist/test/mockSanityCommand.js +16 -0
- package/dist/test/mockSanityCommand.js.map +1 -1
- package/dist/test/mockTelemetry.js +22 -0
- package/dist/test/mockTelemetry.js.map +1 -0
- package/dist/test/{setupExamples.js → setupFixtures.js} +43 -35
- package/dist/test/setupFixtures.js.map +1 -0
- package/dist/test/snapshotSerializer.js +12 -0
- package/dist/test/snapshotSerializer.js.map +1 -0
- package/dist/test/testCommand.js +6 -1
- package/dist/test/testCommand.js.map +1 -1
- package/dist/test/{testExample.js → testFixture.js} +46 -27
- package/dist/test/testFixture.js.map +1 -0
- package/dist/test/testHook.js +23 -7
- package/dist/test/testHook.js.map +1 -1
- package/dist/utils/paths.js +43 -7
- package/dist/utils/paths.js.map +1 -1
- package/dist/vitest.d.ts +104 -8
- package/dist/vitest.js +3 -2
- package/dist/vitest.js.map +1 -1
- package/dist/vitestWorker.js +4 -0
- package/dist/vitestWorker.js.map +1 -1
- package/{examples → fixtures}/basic-app/package.json +5 -6
- package/{examples → fixtures}/basic-studio/package.json +5 -6
- package/{examples → fixtures}/multi-workspace-studio/package.json +5 -6
- package/fixtures/prebuilt-app/README.md +3 -0
- package/fixtures/prebuilt-app/dist/favicon.ico +0 -0
- package/fixtures/prebuilt-app/dist/index.html +102 -0
- package/fixtures/prebuilt-app/dist/static/sanity-CtOxKsdo.css +24 -0
- package/fixtures/prebuilt-app/dist/static/sanity-D4a4eOYZ.js +17 -0
- package/fixtures/prebuilt-app/package.json +26 -0
- package/fixtures/prebuilt-app/sanity.cli.ts +12 -0
- package/fixtures/prebuilt-app/src/App.css +20 -0
- package/fixtures/prebuilt-app/src/App.tsx +24 -0
- package/fixtures/prebuilt-app/tsconfig.json +17 -0
- package/fixtures/prebuilt-studio/README.md +3 -0
- package/fixtures/prebuilt-studio/dist/favicon.ico +0 -0
- package/fixtures/prebuilt-studio/dist/index.html +113 -0
- package/fixtures/prebuilt-studio/dist/static/sanity-DxH-rpFr.js +9 -0
- package/fixtures/prebuilt-studio/package.json +25 -0
- package/fixtures/prebuilt-studio/sanity.cli.ts +11 -0
- package/fixtures/prebuilt-studio/sanity.config.ts +11 -0
- package/fixtures/prebuilt-studio/tsconfig.json +17 -0
- package/{examples → fixtures}/worst-case-studio/package.json +7 -8
- package/package.json +21 -19
- package/dist/test/captureOutput.d.ts +0 -33
- package/dist/test/constants.d.ts +0 -2
- package/dist/test/createTestClient.d.ts +0 -44
- package/dist/test/createTestToken.d.ts +0 -1
- package/dist/test/mockApi.d.ts +0 -34
- package/dist/test/mockSanityCommand.d.ts +0 -45
- package/dist/test/setupExamples.d.ts +0 -60
- package/dist/test/setupExamples.js.map +0 -1
- package/dist/test/testCommand.d.ts +0 -21
- package/dist/test/testExample.d.ts +0 -46
- package/dist/test/testExample.js.map +0 -1
- package/dist/test/testHook.d.ts +0 -8
- package/dist/utils/fileExists.d.ts +0 -9
- package/dist/utils/paths.d.ts +0 -22
- package/dist/vitestWorker.d.ts +0 -23
- /package/{examples → fixtures}/basic-app/sanity.cli.ts +0 -0
- /package/{examples → fixtures}/basic-app/src/App.css +0 -0
- /package/{examples → fixtures}/basic-app/src/App.tsx +0 -0
- /package/{examples → fixtures}/basic-app/src/ExampleComponent.css +0 -0
- /package/{examples → fixtures}/basic-app/src/ExampleComponent.tsx +0 -0
- /package/{examples → fixtures}/basic-app/tsconfig.json +0 -0
- /package/{examples → fixtures}/basic-studio/sanity.cli.ts +0 -0
- /package/{examples → fixtures}/basic-studio/sanity.config.ts +0 -0
- /package/{examples → fixtures}/basic-studio/schemaTypes/author.ts +0 -0
- /package/{examples → fixtures}/basic-studio/schemaTypes/blockContent.ts +0 -0
- /package/{examples → fixtures}/basic-studio/schemaTypes/category.ts +0 -0
- /package/{examples → fixtures}/basic-studio/schemaTypes/index.ts +0 -0
- /package/{examples → fixtures}/basic-studio/schemaTypes/post.ts +0 -0
- /package/{examples → fixtures}/basic-studio/tsconfig.json +0 -0
- /package/{examples → fixtures}/multi-workspace-studio/sanity.cli.ts +0 -0
- /package/{examples → fixtures}/multi-workspace-studio/sanity.config.ts +0 -0
- /package/{examples → fixtures}/multi-workspace-studio/schemaTypes/author.ts +0 -0
- /package/{examples → fixtures}/multi-workspace-studio/schemaTypes/blockContent.ts +0 -0
- /package/{examples → fixtures}/multi-workspace-studio/schemaTypes/category.ts +0 -0
- /package/{examples → fixtures}/multi-workspace-studio/schemaTypes/index.ts +0 -0
- /package/{examples → fixtures}/multi-workspace-studio/schemaTypes/post.ts +0 -0
- /package/{examples → fixtures}/multi-workspace-studio/tsconfig.json +0 -0
- /package/{examples → fixtures}/worst-case-studio/README.md +0 -0
- /package/{examples → fixtures}/worst-case-studio/sanity.cli.ts +0 -0
- /package/{examples → fixtures}/worst-case-studio/sanity.config.tsx +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/defines.ts +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/descriptionIcon.svg +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/descriptionInput.module.css +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/descriptionInput.tsx +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/schemaTypes/author.ts +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/schemaTypes/blockContent.ts +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/schemaTypes/category.ts +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/schemaTypes/index.ts +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/schemaTypes/post.ts +0 -0
- /package/{examples → fixtures}/worst-case-studio/src/typings.d.ts +0 -0
- /package/{examples → fixtures}/worst-case-studio/tsconfig.json +0 -0
package/README.md
CHANGED
|
@@ -18,38 +18,38 @@ export default defineConfig({
|
|
|
18
18
|
})
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
This will automatically copy and install dependencies for all bundled
|
|
21
|
+
This will automatically copy and install dependencies for all bundled fixtures before tests run.
|
|
22
22
|
|
|
23
|
-
### 2. Use test
|
|
23
|
+
### 2. Use test fixtures in your tests
|
|
24
24
|
|
|
25
25
|
```ts
|
|
26
|
-
import {
|
|
26
|
+
import {testFixture} from '@sanity/cli-test'
|
|
27
27
|
import {describe, test} from 'vitest'
|
|
28
28
|
|
|
29
29
|
describe('my test suite', () => {
|
|
30
30
|
test('should work with basic-studio', async () => {
|
|
31
|
-
const cwd = await
|
|
32
|
-
// The
|
|
31
|
+
const cwd = await testFixture('basic-studio')
|
|
32
|
+
// The fixture is now available at `cwd` with dependencies installed
|
|
33
33
|
// Tests that need built output should build explicitly:
|
|
34
|
-
// await
|
|
34
|
+
// await buildFixture(cwd)
|
|
35
35
|
})
|
|
36
36
|
})
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
## API
|
|
40
40
|
|
|
41
|
-
### `
|
|
41
|
+
### `testFixture(fixtureName: string, options?: TestFixtureOptions): Promise<string>`
|
|
42
42
|
|
|
43
|
-
Creates an isolated copy of a bundled
|
|
43
|
+
Creates an isolated copy of a bundled fixture for testing. Returns the absolute path to the temporary directory containing the fixture.
|
|
44
44
|
|
|
45
45
|
**Parameters:**
|
|
46
46
|
|
|
47
|
-
- `
|
|
47
|
+
- `fixtureName` - Name of the fixture to copy (e.g., 'basic-app', 'basic-studio')
|
|
48
48
|
- `options.tempDir` - Optional custom temp directory path (defaults to `process.cwd()/tmp`)
|
|
49
49
|
|
|
50
|
-
**Returns:** Absolute path to the temporary
|
|
50
|
+
**Returns:** Absolute path to the temporary fixture directory
|
|
51
51
|
|
|
52
|
-
**Available
|
|
52
|
+
**Available Fixtures:**
|
|
53
53
|
|
|
54
54
|
- `basic-app` - Basic Sanity application
|
|
55
55
|
- `basic-studio` - Basic Sanity Studio
|
|
@@ -59,27 +59,27 @@ Creates an isolated copy of a bundled example for testing. Returns the absolute
|
|
|
59
59
|
**Example:**
|
|
60
60
|
|
|
61
61
|
```ts
|
|
62
|
-
import {
|
|
62
|
+
import {testFixture} from '@sanity/cli-test'
|
|
63
63
|
|
|
64
|
-
const cwd = await
|
|
65
|
-
//
|
|
66
|
-
// Note:
|
|
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
67
|
```
|
|
68
68
|
|
|
69
|
-
### `setup(options?:
|
|
69
|
+
### `setup(options?: SetupTestFixturesOptions): Promise<void>`
|
|
70
70
|
|
|
71
|
-
Vitest global setup function that copies
|
|
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
72
|
|
|
73
73
|
**Parameters:**
|
|
74
74
|
|
|
75
|
-
- `options.
|
|
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
76
|
- `options.tempDir` - Custom temp directory path (defaults to `process.cwd()/tmp`)
|
|
77
77
|
|
|
78
|
-
**Note:**
|
|
78
|
+
**Note:** Fixtures are NOT built during setup. Tests that need built output should build explicitly.
|
|
79
79
|
|
|
80
|
-
**Adding
|
|
80
|
+
**Adding fixtures from your local repo:**
|
|
81
81
|
|
|
82
|
-
If your repo has its own
|
|
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
83
|
|
|
84
84
|
```ts
|
|
85
85
|
// vitest.setup.ts
|
|
@@ -89,7 +89,7 @@ export {teardown}
|
|
|
89
89
|
|
|
90
90
|
export async function setup(project) {
|
|
91
91
|
return cliTestSetup(project, {
|
|
92
|
-
|
|
92
|
+
additionalFixtures: ['fixtures/*', 'dev/*'],
|
|
93
93
|
})
|
|
94
94
|
}
|
|
95
95
|
```
|
|
@@ -105,7 +105,7 @@ export default defineConfig({
|
|
|
105
105
|
})
|
|
106
106
|
```
|
|
107
107
|
|
|
108
|
-
### `teardown(options?:
|
|
108
|
+
### `teardown(options?: TeardownTestFixturesOptions): Promise<void>`
|
|
109
109
|
|
|
110
110
|
Vitest global teardown function that removes the temp directory. This is automatically called by vitest when using `@sanity/cli-test/vitest`.
|
|
111
111
|
|
|
@@ -186,17 +186,17 @@ mockApi({
|
|
|
186
186
|
|
|
187
187
|
## How It Works
|
|
188
188
|
|
|
189
|
-
This package bundles pre-configured Sanity
|
|
189
|
+
This package bundles pre-configured Sanity fixtures that can be used for testing. When you call `testFixture()`:
|
|
190
190
|
|
|
191
|
-
1. It creates a unique temporary copy of the requested
|
|
191
|
+
1. It creates a unique temporary copy of the requested fixture
|
|
192
192
|
2. Symlinks the node_modules directory from the global setup version (for performance)
|
|
193
193
|
3. Returns the path to the isolated test directory
|
|
194
194
|
|
|
195
|
-
The
|
|
195
|
+
The fixtures work identically whether this package is used in a monorepo or installed from npm.
|
|
196
196
|
|
|
197
|
-
## Building
|
|
197
|
+
## Building Fixtures
|
|
198
198
|
|
|
199
|
-
|
|
199
|
+
Fixtures are NOT built during global setup or when calling `testFixture()`. Tests that need built output should build explicitly:
|
|
200
200
|
|
|
201
201
|
```ts
|
|
202
202
|
import {exec} from 'node:child_process'
|
|
@@ -204,8 +204,8 @@ import {promisify} from 'node:util'
|
|
|
204
204
|
|
|
205
205
|
const execAsync = promisify(exec)
|
|
206
206
|
|
|
207
|
-
const cwd = await
|
|
208
|
-
// Build the
|
|
207
|
+
const cwd = await testFixture('basic-studio')
|
|
208
|
+
// Build the fixture before running tests that need it
|
|
209
209
|
await execAsync('npx sanity build --yes', {cwd})
|
|
210
210
|
```
|
|
211
211
|
|
|
@@ -246,7 +246,7 @@ export default defineConfig({
|
|
|
246
246
|
test: {
|
|
247
247
|
globalSetup: [
|
|
248
248
|
'test/workerBuild.ts', // Your worker setup
|
|
249
|
-
'@sanity/cli-test/vitest', //
|
|
249
|
+
'@sanity/cli-test/vitest', // Fixture setup
|
|
250
250
|
],
|
|
251
251
|
},
|
|
252
252
|
})
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,517 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import {CliConfig} from '@sanity/cli-core'
|
|
2
|
+
import {ClientConfig} from '@sanity/client'
|
|
3
|
+
import {CLITelemetryStore} from '@sanity/cli-core'
|
|
4
|
+
import {Command} from '@oclif/core'
|
|
5
|
+
import {Config} from '@oclif/core'
|
|
6
|
+
import {Errors} from '@oclif/core'
|
|
7
|
+
import {Hook} from '@oclif/core/hooks'
|
|
8
|
+
import {Hooks} from '@oclif/core/hooks'
|
|
9
|
+
import {Mock} from 'vitest'
|
|
10
|
+
import nock from 'nock'
|
|
11
|
+
import {ProjectRootResult} from '@sanity/cli-core'
|
|
12
|
+
import {RawRequestOptions} from '@sanity/client'
|
|
13
|
+
import {SanityClient} from 'sanity'
|
|
14
|
+
import {SanityCommand} from '@sanity/cli-core'
|
|
15
|
+
import {TestProject} from 'vitest/node'
|
|
16
|
+
|
|
17
|
+
declare interface CaptureOptions {
|
|
18
|
+
/**
|
|
19
|
+
* Whether to print the output to the console
|
|
20
|
+
*/
|
|
21
|
+
print?: boolean
|
|
22
|
+
/**
|
|
23
|
+
* Whether to strip ANSI escape codes from the output
|
|
24
|
+
*/
|
|
25
|
+
stripAnsi?: boolean
|
|
26
|
+
testNodeEnv?: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare interface CaptureResult<T = unknown> {
|
|
30
|
+
stderr: string
|
|
31
|
+
stdout: string
|
|
32
|
+
error?: Error & Partial<Errors.CLIError>
|
|
33
|
+
result?: T
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
declare type CommandClass = (new (argv: string[], config: Config) => Command) & typeof Command
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Converts Unix-style paths to platform-appropriate paths.
|
|
40
|
+
* On Windows:
|
|
41
|
+
* - Absolute paths starting with '/': adds drive letter and converts to backslashes
|
|
42
|
+
* - Relative/partial paths: converts forward slashes to backslashes
|
|
43
|
+
* On Unix: keeps paths as-is.
|
|
44
|
+
*
|
|
45
|
+
* @param pathStr - Unix-style path (e.g., '/test/path' or '.config/file.json')
|
|
46
|
+
* @returns Platform-appropriate path
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
export declare function convertToSystemPath(pathStr: string): string
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Creates a real Sanity client instance for testing that makes actual HTTP requests.
|
|
53
|
+
* Use with mockApi() to intercept and mock the HTTP calls.
|
|
54
|
+
*
|
|
55
|
+
* @public
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* // Mock getGlobalCliClient to return a test client
|
|
60
|
+
* vi.mock('@sanity/cli-core', async (importOriginal) => {
|
|
61
|
+
* const actual = await importOriginal<typeof import('@sanity/cli-core')>()
|
|
62
|
+
* const {createTestClient} = await import('@sanity/cli-test')
|
|
63
|
+
*
|
|
64
|
+
* return {
|
|
65
|
+
* ...actual,
|
|
66
|
+
* getGlobalCliClient: vi.fn().mockImplementation((opts) => {
|
|
67
|
+
* return Promise.resolve(createTestClient({
|
|
68
|
+
* apiVersion: opts.apiVersion,
|
|
69
|
+
* }))
|
|
70
|
+
* }),
|
|
71
|
+
* }
|
|
72
|
+
* })
|
|
73
|
+
*
|
|
74
|
+
* // Then use mockApi to intercept requests
|
|
75
|
+
* mockApi({
|
|
76
|
+
* apiVersion: 'v2025-02-19',
|
|
77
|
+
* method: 'get',
|
|
78
|
+
* uri: '/media-libraries',
|
|
79
|
+
* }).reply(200, {data: [...]})
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function createTestClient(options: CreateTestClientOptions): {
|
|
83
|
+
client: SanityClient
|
|
84
|
+
request: Mock<(options: RawRequestOptions) => Promise<any>>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Options for createTestClient
|
|
89
|
+
*
|
|
90
|
+
* @public
|
|
91
|
+
*/
|
|
92
|
+
export declare interface CreateTestClientOptions extends ClientConfig {
|
|
93
|
+
/**
|
|
94
|
+
* API version for the client
|
|
95
|
+
*/
|
|
96
|
+
apiVersion: string
|
|
97
|
+
/**
|
|
98
|
+
* Authentication token
|
|
99
|
+
*/
|
|
100
|
+
token: string
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Creates a test token for the Sanity CLI
|
|
105
|
+
*
|
|
106
|
+
* @public
|
|
107
|
+
*
|
|
108
|
+
* @param token - The token to create
|
|
109
|
+
* @returns void
|
|
110
|
+
*/
|
|
111
|
+
export declare function createTestToken(token: string): void
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Default fixtures bundled with the package and their options.
|
|
115
|
+
*
|
|
116
|
+
* @public
|
|
117
|
+
*/
|
|
118
|
+
export declare const DEFAULT_FIXTURES: Record<FixtureName, FixtureOptions>
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @deprecated Use {@link FixtureName} instead. This type alias will be removed in a future release.
|
|
122
|
+
* @public
|
|
123
|
+
*/
|
|
124
|
+
export declare type ExampleName = FixtureName
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Valid fixture name type.
|
|
128
|
+
* @public
|
|
129
|
+
*/
|
|
130
|
+
export declare type FixtureName =
|
|
131
|
+
| 'basic-app'
|
|
132
|
+
| 'basic-studio'
|
|
133
|
+
| 'multi-workspace-studio'
|
|
134
|
+
| 'prebuilt-app'
|
|
135
|
+
| 'prebuilt-studio'
|
|
136
|
+
| 'worst-case-studio'
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Options for each fixture.
|
|
140
|
+
* @public
|
|
141
|
+
*/
|
|
142
|
+
export declare interface FixtureOptions {
|
|
143
|
+
includeDist?: boolean
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Gets the current Windows drive letter from process.cwd().
|
|
148
|
+
* Falls back to 'C:\\' if detection fails.
|
|
149
|
+
*
|
|
150
|
+
* @returns Drive letter with backslash (e.g., 'C:\\', 'D:\\') or empty string on Unix
|
|
151
|
+
* @internal
|
|
152
|
+
*/
|
|
153
|
+
export declare function getCurrentDrive(): string
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Gets the path to the fixtures directory bundled with this package.
|
|
157
|
+
*
|
|
158
|
+
* The fixtures are copied during build and bundled with the published package.
|
|
159
|
+
* This function works the same whether the package is used in a monorepo
|
|
160
|
+
* or installed from npm.
|
|
161
|
+
*
|
|
162
|
+
* @returns Absolute path to the fixtures directory
|
|
163
|
+
* @internal
|
|
164
|
+
*/
|
|
165
|
+
export declare function getFixturesPath(): string
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Gets the path to the temporary directory for test fixtures.
|
|
169
|
+
*
|
|
170
|
+
* Uses the initial working directory captured when this module was first loaded,
|
|
171
|
+
* not process.cwd() which may change during test execution.
|
|
172
|
+
*
|
|
173
|
+
* @param customTempDir - Optional custom temp directory path
|
|
174
|
+
* @returns Absolute path to temp directory (default: initial cwd/tmp)
|
|
175
|
+
* @internal
|
|
176
|
+
*/
|
|
177
|
+
export declare function getTempPath(customTempDir?: string): string
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Mocks the API calls, add some defaults so it doesn't cause too much friction
|
|
181
|
+
*
|
|
182
|
+
* @internal
|
|
183
|
+
*/
|
|
184
|
+
export declare function mockApi({
|
|
185
|
+
apiHost,
|
|
186
|
+
apiVersion,
|
|
187
|
+
includeQueryTag,
|
|
188
|
+
method,
|
|
189
|
+
projectId,
|
|
190
|
+
query,
|
|
191
|
+
uri,
|
|
192
|
+
}: MockApiOptions): nock.Interceptor
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @internal
|
|
196
|
+
*/
|
|
197
|
+
export declare interface MockApiOptions {
|
|
198
|
+
/**
|
|
199
|
+
* Uri to mock
|
|
200
|
+
*/
|
|
201
|
+
uri: string
|
|
202
|
+
/**
|
|
203
|
+
* Api host to mock, defaults to `https://api.sanity.io`
|
|
204
|
+
*/
|
|
205
|
+
apiHost?: string
|
|
206
|
+
/**
|
|
207
|
+
* Api version to mock, defaults to `v2025-05-14`
|
|
208
|
+
*/
|
|
209
|
+
apiVersion?: string
|
|
210
|
+
/**
|
|
211
|
+
* Whether to include `tag: 'sanity.cli'` in query parameters.
|
|
212
|
+
* Defaults to `true`. Set to `false` for endpoints that don't use CLI tagging.
|
|
213
|
+
*/
|
|
214
|
+
includeQueryTag?: boolean
|
|
215
|
+
/**
|
|
216
|
+
* HTTP method to mock
|
|
217
|
+
*
|
|
218
|
+
* Defaults to 'get'
|
|
219
|
+
*/
|
|
220
|
+
method?: 'delete' | 'get' | 'patch' | 'post' | 'put'
|
|
221
|
+
/**
|
|
222
|
+
* Project ID to mock. When provided, constructs apiHost as `https://{projectId}.api.sanity.io`
|
|
223
|
+
* Takes precedence over apiHost if both are provided.
|
|
224
|
+
*/
|
|
225
|
+
projectId?: string
|
|
226
|
+
/**
|
|
227
|
+
* Query parameters to mock
|
|
228
|
+
*/
|
|
229
|
+
query?: Record<string, string>
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Creates a testable subclass of a command with mocked SanityCommand dependencies.
|
|
234
|
+
*
|
|
235
|
+
* @public
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* ```ts
|
|
239
|
+
* // Basic config mocking
|
|
240
|
+
* const TestAdd = mockSanityCommand(Add, {
|
|
241
|
+
* cliConfig: { api: { projectId: 'test-project' } }
|
|
242
|
+
* })
|
|
243
|
+
*
|
|
244
|
+
* // With mock API client
|
|
245
|
+
* const mockClient = {
|
|
246
|
+
* getDocument: vi.fn().mockResolvedValue({ _id: 'doc1', title: 'Test' }),
|
|
247
|
+
* fetch: vi.fn().mockResolvedValue([]),
|
|
248
|
+
* }
|
|
249
|
+
* const TestGet = mockSanityCommand(GetDocumentCommand, {
|
|
250
|
+
* cliConfig: { api: { projectId: 'test-project', dataset: 'production' } },
|
|
251
|
+
* projectApiClient: mockClient,
|
|
252
|
+
* })
|
|
253
|
+
*
|
|
254
|
+
* const {stdout} = await testCommand(TestGet, ['doc1'])
|
|
255
|
+
* expect(mockClient.getDocument).toHaveBeenCalledWith('doc1')
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
export declare function mockSanityCommand<T extends typeof SanityCommand<typeof Command>>(
|
|
259
|
+
CommandClass: T,
|
|
260
|
+
options?: MockSanityCommandOptions,
|
|
261
|
+
): T
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* @public
|
|
265
|
+
*/
|
|
266
|
+
export declare interface MockSanityCommandOptions {
|
|
267
|
+
/**
|
|
268
|
+
* Mock CLI config (required if command uses getCliConfig or getProjectId)
|
|
269
|
+
*/
|
|
270
|
+
cliConfig?: CliConfig
|
|
271
|
+
/**
|
|
272
|
+
* Mock whether the terminal is interactive (used by isUnattended)
|
|
273
|
+
*/
|
|
274
|
+
isInteractive?: boolean
|
|
275
|
+
/**
|
|
276
|
+
* Mock project root result (required if command uses getProjectRoot)
|
|
277
|
+
*/
|
|
278
|
+
projectRoot?: ProjectRootResult
|
|
279
|
+
/**
|
|
280
|
+
* Mock authentication token (passed to API clients, bypasses getCliToken)
|
|
281
|
+
*/
|
|
282
|
+
token?: string
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* @public
|
|
287
|
+
* @param options - Options for mocking the telemetry store.
|
|
288
|
+
* @returns The mocked telemetry store.
|
|
289
|
+
*/
|
|
290
|
+
export declare const mockTelemetry: (options?: MockTelemetryOptions) => CLITelemetryStore
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* @public
|
|
294
|
+
*/
|
|
295
|
+
export declare interface MockTelemetryOptions {
|
|
296
|
+
trace?: () => void
|
|
297
|
+
updateUserProperties?: () => void
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
declare interface Options {
|
|
301
|
+
config: Config
|
|
302
|
+
Command?: Command.Class
|
|
303
|
+
context?: Hook.Context
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Global setup function for initializing test fixtures.
|
|
308
|
+
*
|
|
309
|
+
* Copies fixtures from the bundled location to a temp directory
|
|
310
|
+
* and installs dependencies.
|
|
311
|
+
*
|
|
312
|
+
* Note: Fixtures are NOT built during setup. Tests that need built
|
|
313
|
+
* fixtures should build them as part of the test.
|
|
314
|
+
*
|
|
315
|
+
* This function is designed to be used with vitest globalSetup.
|
|
316
|
+
*
|
|
317
|
+
* @public
|
|
318
|
+
*
|
|
319
|
+
* @param options - Configuration options
|
|
320
|
+
* @example
|
|
321
|
+
* ```typescript
|
|
322
|
+
* // In vitest.config.ts
|
|
323
|
+
* export default defineConfig({
|
|
324
|
+
* test: {
|
|
325
|
+
* globalSetup: ['@sanity/cli-test/vitest']
|
|
326
|
+
* }
|
|
327
|
+
* })
|
|
328
|
+
* ```
|
|
329
|
+
*/
|
|
330
|
+
export declare function setup(_: TestProject, options?: SetupTestFixturesOptions): Promise<void>
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Options for setupTestFixtures
|
|
334
|
+
*
|
|
335
|
+
* @public
|
|
336
|
+
*/
|
|
337
|
+
export declare interface SetupTestFixturesOptions {
|
|
338
|
+
/**
|
|
339
|
+
* Glob patterns for additional fixture directories to set up.
|
|
340
|
+
*
|
|
341
|
+
* Each pattern is matched against directories in the current working directory.
|
|
342
|
+
* Only directories containing a `package.json` file are included.
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* ```typescript
|
|
346
|
+
* ['fixtures/*', 'dev/*']
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
additionalFixtures?: string[]
|
|
350
|
+
/**
|
|
351
|
+
* Custom temp directory path. Defaults to process.cwd()/tmp
|
|
352
|
+
*/
|
|
353
|
+
tempDir?: string
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Teardown function to clean up test fixtures.
|
|
358
|
+
*
|
|
359
|
+
* Removes the temp directory created by setupTestFixtures.
|
|
360
|
+
*
|
|
361
|
+
* This function is designed to be used with vitest globalSetup.
|
|
362
|
+
*
|
|
363
|
+
* @public
|
|
364
|
+
*
|
|
365
|
+
* @param options - Configuration options
|
|
366
|
+
*/
|
|
367
|
+
export declare function teardown(options?: TeardownTestFixturesOptions): Promise<void>
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Options for teardownTestFixtures
|
|
371
|
+
*
|
|
372
|
+
* @public
|
|
373
|
+
*/
|
|
374
|
+
export declare interface TeardownTestFixturesOptions {
|
|
375
|
+
/**
|
|
376
|
+
* Custom temp directory path. Defaults to process.cwd()/tmp
|
|
377
|
+
*/
|
|
378
|
+
tempDir?: string
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @public
|
|
383
|
+
*/
|
|
384
|
+
export declare function testCommand(
|
|
385
|
+
command: CommandClass,
|
|
386
|
+
args?: string[],
|
|
387
|
+
options?: TestCommandOptions,
|
|
388
|
+
): Promise<CaptureResult<unknown>>
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* @public
|
|
392
|
+
*/
|
|
393
|
+
export declare interface TestCommandOptions {
|
|
394
|
+
/**
|
|
395
|
+
* Options for capturing output
|
|
396
|
+
*/
|
|
397
|
+
capture?: CaptureOptions
|
|
398
|
+
/**
|
|
399
|
+
* Partial oclif config overrides
|
|
400
|
+
*/
|
|
401
|
+
config?: Partial<Config>
|
|
402
|
+
/**
|
|
403
|
+
* Mock options for SanityCommand dependencies (config, project root, API clients).
|
|
404
|
+
* When provided, the command is automatically wrapped with mockSanityCommand.
|
|
405
|
+
*/
|
|
406
|
+
mocks?: MockSanityCommandOptions & MockTelemetryOptions
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Recursively copy a directory, skipping specified folders.
|
|
411
|
+
*
|
|
412
|
+
* @param srcDir - Source directory to copy from
|
|
413
|
+
* @param destDir - Destination directory to copy to
|
|
414
|
+
* @param skip - Array of directory/file names to skip (e.g., ['node_modules', 'dist'])
|
|
415
|
+
* @internal
|
|
416
|
+
*/
|
|
417
|
+
export declare function testCopyDirectory(
|
|
418
|
+
srcDir: string,
|
|
419
|
+
destDir: string,
|
|
420
|
+
skip?: string[],
|
|
421
|
+
): Promise<void>
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* @deprecated Use {@link testFixture} instead. This function will be removed in a future release.
|
|
425
|
+
*
|
|
426
|
+
* Clones an example (now called fixture) directory into a temporary directory with an isolated copy.
|
|
427
|
+
*
|
|
428
|
+
* @param exampleName - The name of the example/fixture to clone (e.g., 'basic-app', 'basic-studio')
|
|
429
|
+
* @param options - Configuration options
|
|
430
|
+
* @returns The absolute path to the temporary directory containing the example/fixture
|
|
431
|
+
*
|
|
432
|
+
* @public
|
|
433
|
+
*/
|
|
434
|
+
export declare function testExample(
|
|
435
|
+
exampleName: FixtureName | (string & {}),
|
|
436
|
+
options?: TestFixtureOptions,
|
|
437
|
+
): Promise<string>
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* @deprecated Use {@link TestFixtureOptions} instead. This type alias will be removed in a future release.
|
|
441
|
+
* @public
|
|
442
|
+
*/
|
|
443
|
+
export declare type TestExampleOptions = TestFixtureOptions
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Clones a fixture directory into a temporary directory with an isolated copy.
|
|
447
|
+
*
|
|
448
|
+
* The function creates a unique temporary copy of the specified fixture with:
|
|
449
|
+
* - A random unique ID to avoid conflicts between parallel tests
|
|
450
|
+
* - Symlinked node_modules for performance (from the global setup version)
|
|
451
|
+
* - Modified package.json name to prevent conflicts
|
|
452
|
+
*
|
|
453
|
+
* The fixture is first looked up in the temp directory (if global setup ran),
|
|
454
|
+
* otherwise it falls back to the bundled fixtures in the package.
|
|
455
|
+
*
|
|
456
|
+
* @param fixtureName - The name of the fixture to clone (e.g., 'basic-app', 'basic-studio')
|
|
457
|
+
* @param options - Configuration options
|
|
458
|
+
* @returns The absolute path to the temporary directory containing the fixture
|
|
459
|
+
*
|
|
460
|
+
* @public
|
|
461
|
+
*
|
|
462
|
+
* @example
|
|
463
|
+
* ```typescript
|
|
464
|
+
* import {testFixture} from '@sanity/cli-test'
|
|
465
|
+
* import {describe, test} from 'vitest'
|
|
466
|
+
*
|
|
467
|
+
* describe('my test suite', () => {
|
|
468
|
+
* test('should work with basic-studio', async () => {
|
|
469
|
+
* const cwd = await testFixture('basic-studio')
|
|
470
|
+
* // ... run your tests in this directory
|
|
471
|
+
* })
|
|
472
|
+
* })
|
|
473
|
+
* ```
|
|
474
|
+
*/
|
|
475
|
+
export declare function testFixture(
|
|
476
|
+
fixtureName: FixtureName | (string & {}),
|
|
477
|
+
options?: TestFixtureOptions,
|
|
478
|
+
): Promise<string>
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* @public
|
|
482
|
+
*/
|
|
483
|
+
export declare interface TestFixtureOptions {
|
|
484
|
+
/**
|
|
485
|
+
* Custom temp directory. Defaults to process.cwd()/tmp
|
|
486
|
+
*/
|
|
487
|
+
tempDir?: string
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Test an oclif hook
|
|
492
|
+
*
|
|
493
|
+
* @public
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* ```ts
|
|
497
|
+
* const result = await testHook(hook, {
|
|
498
|
+
* Command: Command.loadable,
|
|
499
|
+
* context: {
|
|
500
|
+
* config: Config.load({
|
|
501
|
+
* // CLI root is the directory of the package that contains the hook.
|
|
502
|
+
* root: path.resolve(fileURLToPath(import.meta.url), '../../../root'),
|
|
503
|
+
* }),
|
|
504
|
+
* },
|
|
505
|
+
* })
|
|
506
|
+
* ```
|
|
507
|
+
*
|
|
508
|
+
* @param hook - The hook to test
|
|
509
|
+
* @param options - The options for the hook
|
|
510
|
+
* @returns The result of the hook
|
|
511
|
+
*/
|
|
512
|
+
export declare function testHook<T extends keyof Hooks>(
|
|
513
|
+
hook: Hook<T>,
|
|
514
|
+
options: Options,
|
|
515
|
+
): Promise<CaptureResult<Hooks[T]['return']>>
|
|
516
|
+
|
|
517
|
+
export {}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
export * from './test/constants.js';
|
|
1
2
|
export * from './test/createTestClient.js';
|
|
2
3
|
export * from './test/createTestToken.js';
|
|
3
4
|
export * from './test/mockApi.js';
|
|
4
5
|
export * from './test/mockSanityCommand.js';
|
|
5
|
-
export * from './test/
|
|
6
|
+
export * from './test/mockTelemetry.js';
|
|
7
|
+
export * from './test/setupFixtures.js';
|
|
6
8
|
export * from './test/testCommand.js';
|
|
7
|
-
export * from './test/
|
|
9
|
+
export * from './test/testFixture.js';
|
|
8
10
|
export * from './test/testHook.js';
|
|
9
11
|
export * from './utils/paths.js';
|
|
10
12
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './test/createTestClient.js'\nexport * from './test/createTestToken.js'\nexport * from './test/mockApi.js'\nexport * from './test/mockSanityCommand.js'\nexport * from './test/
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './test/constants.js'\nexport * from './test/createTestClient.js'\nexport * from './test/createTestToken.js'\nexport * from './test/mockApi.js'\nexport * from './test/mockSanityCommand.js'\nexport * from './test/mockTelemetry.js'\nexport * from './test/setupFixtures.js'\nexport * from './test/testCommand.js'\nexport * from './test/testFixture.js'\nexport * from './test/testHook.js'\nexport * from './utils/paths.js'\n"],"names":[],"mappings":"AAAA,cAAc,sBAAqB;AACnC,cAAc,6BAA4B;AAC1C,cAAc,4BAA2B;AACzC,cAAc,oBAAmB;AACjC,cAAc,8BAA6B;AAC3C,cAAc,0BAAyB;AACvC,cAAc,0BAAyB;AACvC,cAAc,wBAAuB;AACrC,cAAc,wBAAuB;AACrC,cAAc,qBAAoB;AAClC,cAAc,mBAAkB"}
|