@sanity/cli-test 10.0.0 → 11.0.0
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/dist/index.d.ts +8 -605
- package/dist/mockTelemetry-Bh1va02-.d.ts +16 -0
- package/dist/mocks/cli-core/SanityCommand.d.ts +12 -21
- package/dist/mocks/cli-core/apiClient.d.ts +4 -6
- package/dist/mocks/cli-core/browser.d.ts +3 -4
- package/dist/mocks/cli-core/config.d.ts +23 -44
- package/dist/mocks/cli-core/package-manager.d.ts +11 -20
- package/dist/mocks/cli-core/request.d.ts +8 -36
- package/dist/mocks/cli-core/request.js +10 -35
- package/dist/mocks/cli-core/request.js.map +1 -1
- package/dist/mocks/cli-core/tasks.d.ts +5 -8
- package/dist/mocks/cli-core/telemetry.d.ts +11 -22
- package/dist/mocks/cli-core/ux.d.ts +31 -51
- package/dist/snapshotSerializer-B3X8EXGX.d.ts +10 -0
- package/dist/test/createTestClient.d.ts +19 -22
- package/dist/test/mockApi.d.ts +10 -20
- package/dist/test/oclif.d.ts +2 -179
- package/dist/test/setupFixtures.d.ts +32 -41
- package/dist/test/util.d.ts +4 -203
- package/dist/testFixture-8PtoG-6z.d.ts +140 -0
- package/dist/testHook-CG6GhHGq.d.ts +145 -0
- package/dist/utils/paths.d.ts +30 -34
- package/dist/vitest.d.ts +6 -103
- package/fixtures/worst-case-studio/package.json +1 -1
- package/package.json +8 -7
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { t as MockTelemetryOptions } from "./mockTelemetry-Bh1va02-.js";
|
|
2
|
+
import { Command, Config, Errors } from "@oclif/core";
|
|
3
|
+
import { SanityCommand } from "@sanity/cli-core/SanityCommand";
|
|
4
|
+
import { CliConfig, ProjectRootResult } from "@sanity/cli-core/types";
|
|
5
|
+
import { Hook, Hooks } from "@oclif/core/hooks";
|
|
6
|
+
/**
|
|
7
|
+
* @public
|
|
8
|
+
*/
|
|
9
|
+
interface MockSanityCommandOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Mock CLI config (required if command uses getCliConfig or getProjectId)
|
|
12
|
+
*/
|
|
13
|
+
cliConfig?: CliConfig;
|
|
14
|
+
/**
|
|
15
|
+
* When provided, getCliConfig() will throw this error instead of returning a config.
|
|
16
|
+
* Useful for simulating running outside a project directory.
|
|
17
|
+
*/
|
|
18
|
+
cliConfigError?: Error;
|
|
19
|
+
/**
|
|
20
|
+
* Mock whether the terminal is interactive (used by isUnattended)
|
|
21
|
+
*/
|
|
22
|
+
isInteractive?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Mock project root result (required if command uses getProjectRoot)
|
|
25
|
+
*/
|
|
26
|
+
projectRoot?: ProjectRootResult;
|
|
27
|
+
/**
|
|
28
|
+
* Mock authentication token (passed to API clients, bypasses getCliToken)
|
|
29
|
+
*/
|
|
30
|
+
token?: string;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates a testable subclass of a command with mocked SanityCommand dependencies.
|
|
34
|
+
*
|
|
35
|
+
* @public
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* // Basic config mocking
|
|
40
|
+
* const TestAdd = mockSanityCommand(Add, {
|
|
41
|
+
* cliConfig: { api: { projectId: 'test-project' } }
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
44
|
+
* // With mock API client
|
|
45
|
+
* const mockClient = {
|
|
46
|
+
* getDocument: vi.fn().mockResolvedValue({ _id: 'doc1', title: 'Test' }),
|
|
47
|
+
* fetch: vi.fn().mockResolvedValue([]),
|
|
48
|
+
* }
|
|
49
|
+
* const TestGet = mockSanityCommand(GetDocumentCommand, {
|
|
50
|
+
* cliConfig: { api: { projectId: 'test-project', dataset: 'production' } },
|
|
51
|
+
* projectApiClient: mockClient,
|
|
52
|
+
* })
|
|
53
|
+
*
|
|
54
|
+
* const {stdout} = await testCommand(TestGet, ['doc1'])
|
|
55
|
+
* expect(mockClient.getDocument).toHaveBeenCalledWith('doc1')
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
declare function mockSanityCommand<T extends typeof SanityCommand<typeof Command>>(CommandClass: T, options?: MockSanityCommandOptions): T;
|
|
59
|
+
/** @public */
|
|
60
|
+
interface CaptureOptions {
|
|
61
|
+
/**
|
|
62
|
+
* Whether to print the output to the console
|
|
63
|
+
*/
|
|
64
|
+
print?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Whether to strip ANSI escape codes from the output
|
|
67
|
+
*/
|
|
68
|
+
stripAnsi?: boolean;
|
|
69
|
+
testNodeEnv?: string;
|
|
70
|
+
}
|
|
71
|
+
/** @public */
|
|
72
|
+
interface CaptureResult<T = unknown> {
|
|
73
|
+
stderr: string;
|
|
74
|
+
stdout: string;
|
|
75
|
+
error?: Error & Partial<Errors.CLIError>;
|
|
76
|
+
result?: T;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Capture the output of a command and return the result
|
|
80
|
+
*
|
|
81
|
+
* @param fn - The function to capture the output of
|
|
82
|
+
* @param opts - The options for the capture
|
|
83
|
+
* @returns The result of the command
|
|
84
|
+
* @public
|
|
85
|
+
*
|
|
86
|
+
* Credits to oclif for the original implementation:
|
|
87
|
+
* https://github.com/oclif/test/blob/2a5407e6fc80d388043d10f6b7b8eaa586483015/src/index.ts
|
|
88
|
+
*
|
|
89
|
+
* We are not using the library directly since it does not support mocking code inside of the command
|
|
90
|
+
* possibly because the commands run in a different thread
|
|
91
|
+
*/
|
|
92
|
+
declare function captureOutput<T>(fn: () => Promise<T>, opts?: CaptureOptions): Promise<CaptureResult<T>>;
|
|
93
|
+
type CommandClass = (new (argv: string[], config: Config) => Command) & typeof Command;
|
|
94
|
+
/**
|
|
95
|
+
* @public
|
|
96
|
+
*/
|
|
97
|
+
interface TestCommandOptions {
|
|
98
|
+
/**
|
|
99
|
+
* Options for capturing output
|
|
100
|
+
*/
|
|
101
|
+
capture?: CaptureOptions;
|
|
102
|
+
/**
|
|
103
|
+
* Partial oclif config overrides
|
|
104
|
+
*/
|
|
105
|
+
config?: Partial<Config>;
|
|
106
|
+
/**
|
|
107
|
+
* Mock options for SanityCommand dependencies (config, project root, API clients).
|
|
108
|
+
* When provided, the command is automatically wrapped with mockSanityCommand.
|
|
109
|
+
*/
|
|
110
|
+
mocks?: MockSanityCommandOptions & MockTelemetryOptions;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* @public
|
|
114
|
+
*/
|
|
115
|
+
declare function testCommand(command: CommandClass, args?: string[], options?: TestCommandOptions): Promise<CaptureResult<unknown>>;
|
|
116
|
+
interface Options {
|
|
117
|
+
config: Config;
|
|
118
|
+
Command?: Command.Class;
|
|
119
|
+
context?: Hook.Context;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Test an oclif hook
|
|
123
|
+
*
|
|
124
|
+
* @public
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* const result = await testHook(hook, {
|
|
129
|
+
* Command: Command.loadable,
|
|
130
|
+
* context: {
|
|
131
|
+
* config: Config.load({
|
|
132
|
+
* // CLI root is the directory of the package that contains the hook.
|
|
133
|
+
* root: path.resolve(fileURLToPath(import.meta.url), '../../../root'),
|
|
134
|
+
* }),
|
|
135
|
+
* },
|
|
136
|
+
* })
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @param hook - The hook to test
|
|
140
|
+
* @param options - The options for the hook
|
|
141
|
+
* @returns The result of the hook
|
|
142
|
+
*/
|
|
143
|
+
declare function testHook<T extends keyof Hooks>(hook: Hook<T>, options: Options): Promise<CaptureResult<Hooks[T]["return"]>>;
|
|
144
|
+
export { CaptureResult as a, mockSanityCommand as c, CaptureOptions as i, TestCommandOptions as n, captureOutput as o, testCommand as r, MockSanityCommandOptions as s, testHook as t };
|
|
145
|
+
//# sourceMappingURL=testHook-CG6GhHGq.d.ts.map
|
package/dist/utils/paths.d.ts
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* On Windows:
|
|
4
|
-
* - Absolute paths starting with '/': adds drive letter and converts to backslashes
|
|
5
|
-
* - Relative/partial paths: converts forward slashes to backslashes
|
|
6
|
-
* On Unix: keeps paths as-is.
|
|
2
|
+
* Gets the path to the fixtures directory bundled with this package.
|
|
7
3
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
4
|
+
* The fixtures are copied during build and bundled with the published package.
|
|
5
|
+
* This function works the same whether the package is used in a monorepo
|
|
6
|
+
* or installed from npm.
|
|
7
|
+
*
|
|
8
|
+
* @returns Absolute path to the fixtures directory
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
declare function getFixturesPath(): string;
|
|
12
|
+
/**
|
|
13
|
+
* Gets the path to the temporary directory for test fixtures.
|
|
14
|
+
*
|
|
15
|
+
* Uses the initial working directory captured when this module was first loaded,
|
|
16
|
+
* not process.cwd() which may change during test execution.
|
|
17
|
+
*
|
|
18
|
+
* @param customTempDir - Optional custom temp directory path
|
|
19
|
+
* @returns Absolute path to temp directory (default: initial cwd/tmp)
|
|
10
20
|
* @internal
|
|
11
21
|
*/
|
|
12
|
-
|
|
13
|
-
|
|
22
|
+
declare function getTempPath(customTempDir?: string): string;
|
|
14
23
|
/**
|
|
15
24
|
* Creates a unique temporary directory for test output.
|
|
16
25
|
* Uses {@link getTempPath} as the base, creating it if needed.
|
|
@@ -21,14 +30,13 @@ export declare function convertToSystemPath(pathStr: string): string;
|
|
|
21
30
|
* @returns The path and a cleanup function
|
|
22
31
|
* @public
|
|
23
32
|
*/
|
|
24
|
-
|
|
33
|
+
declare function createTmpDir(options?: {
|
|
25
34
|
prefix?: string;
|
|
26
35
|
useSystemTmp?: boolean;
|
|
27
36
|
}): Promise<{
|
|
28
37
|
cleanup: () => Promise<void>;
|
|
29
38
|
path: string;
|
|
30
39
|
}>;
|
|
31
|
-
|
|
32
40
|
/**
|
|
33
41
|
* Gets the current Windows drive letter from process.cwd().
|
|
34
42
|
* Falls back to 'C:\\' if detection fails.
|
|
@@ -36,30 +44,18 @@ export declare function createTmpDir(options?: {
|
|
|
36
44
|
* @returns Drive letter with backslash (e.g., 'C:\\', 'D:\\') or empty string on Unix
|
|
37
45
|
* @internal
|
|
38
46
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
47
|
+
declare function getCurrentDrive(): string;
|
|
41
48
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @returns Absolute path to the fixtures directory
|
|
49
|
-
* @internal
|
|
50
|
-
*/
|
|
51
|
-
export declare function getFixturesPath(): string;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Gets the path to the temporary directory for test fixtures.
|
|
55
|
-
*
|
|
56
|
-
* Uses the initial working directory captured when this module was first loaded,
|
|
57
|
-
* not process.cwd() which may change during test execution.
|
|
49
|
+
* Converts Unix-style paths to platform-appropriate paths.
|
|
50
|
+
* On Windows:
|
|
51
|
+
* - Absolute paths starting with '/': adds drive letter and converts to backslashes
|
|
52
|
+
* - Relative/partial paths: converts forward slashes to backslashes
|
|
53
|
+
* On Unix: keeps paths as-is.
|
|
58
54
|
*
|
|
59
|
-
* @param
|
|
60
|
-
* @returns
|
|
55
|
+
* @param pathStr - Unix-style path (e.g., '/test/path' or '.config/file.json')
|
|
56
|
+
* @returns Platform-appropriate path
|
|
61
57
|
* @internal
|
|
62
58
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
59
|
+
declare function convertToSystemPath(pathStr: string): string;
|
|
60
|
+
export { convertToSystemPath, createTmpDir, getCurrentDrive, getFixturesPath, getTempPath };
|
|
61
|
+
//# sourceMappingURL=paths.d.ts.map
|
package/dist/vitest.d.ts
CHANGED
|
@@ -1,65 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Global setup function for initializing test fixtures.
|
|
5
|
-
*
|
|
6
|
-
* Copies fixtures from the bundled location to a temp directory
|
|
7
|
-
* and installs dependencies.
|
|
8
|
-
*
|
|
9
|
-
* Note: Fixtures are NOT built during setup. Tests that need built
|
|
10
|
-
* fixtures should build them as part of the test.
|
|
11
|
-
*
|
|
12
|
-
* This function is designed to be used with vitest globalSetup.
|
|
13
|
-
*
|
|
14
|
-
* @public
|
|
15
|
-
*
|
|
16
|
-
* @param options - Configuration options
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* // In vitest.config.ts
|
|
20
|
-
* export default defineConfig({
|
|
21
|
-
* test: {
|
|
22
|
-
* globalSetup: ['@sanity/cli-test/vitest']
|
|
23
|
-
* }
|
|
24
|
-
* })
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export declare function setup(
|
|
28
|
-
_: TestProject,
|
|
29
|
-
options?: SetupTestFixturesOptions,
|
|
30
|
-
): Promise<void>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Options for setupTestFixtures
|
|
34
|
-
*
|
|
35
|
-
* @public
|
|
36
|
-
*/
|
|
37
|
-
declare interface SetupTestFixturesOptions {
|
|
38
|
-
/**
|
|
39
|
-
* Glob patterns for additional fixture directories to set up.
|
|
40
|
-
*
|
|
41
|
-
* Each pattern is matched against directories in the current working directory.
|
|
42
|
-
* Only directories containing a `package.json` file are included.
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```typescript
|
|
46
|
-
* ['fixtures/*', 'dev/*']
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
additionalFixtures?: string[];
|
|
50
|
-
/**
|
|
51
|
-
* When true, passes `--ignore-workspace` to pnpm install so fixtures get
|
|
52
|
-
* their own node_modules even when the temp directory is inside a pnpm
|
|
53
|
-
* workspace. Required for E2E tests that spawn the CLI binary against
|
|
54
|
-
* fixture directories.
|
|
55
|
-
*/
|
|
56
|
-
ignoreWorkspace?: boolean;
|
|
57
|
-
/**
|
|
58
|
-
* Custom temp directory path. Defaults to process.cwd()/tmp
|
|
59
|
-
*/
|
|
60
|
-
tempDir?: string;
|
|
61
|
-
}
|
|
62
|
-
|
|
1
|
+
import { setup, teardown } from "./test/setupFixtures.js";
|
|
2
|
+
import { t as snapshotSerializer } from "./snapshotSerializer-B3X8EXGX.js";
|
|
63
3
|
/**
|
|
64
4
|
* Setup function to build the worker files with esbuild.
|
|
65
5
|
*
|
|
@@ -74,44 +14,7 @@ declare interface SetupTestFixturesOptions {
|
|
|
74
14
|
* @throws If the worker files cannot be bundled
|
|
75
15
|
* @throws If the watcher cannot be set up
|
|
76
16
|
*/
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Serializer for snapshot tests to normalize line endings and Windows ^ to Unix `\`
|
|
81
|
-
* @public
|
|
82
|
-
*/
|
|
83
|
-
export declare const snapshotSerializer: {
|
|
84
|
-
serialize: (val: string) => string;
|
|
85
|
-
test: (val: unknown) => val is string;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Teardown function to clean up test fixtures.
|
|
90
|
-
*
|
|
91
|
-
* Removes the temp directory created by setupTestFixtures.
|
|
92
|
-
*
|
|
93
|
-
* This function is designed to be used with vitest globalSetup.
|
|
94
|
-
*
|
|
95
|
-
* @public
|
|
96
|
-
*
|
|
97
|
-
* @param options - Configuration options
|
|
98
|
-
*/
|
|
99
|
-
export declare function teardown(
|
|
100
|
-
options?: TeardownTestFixturesOptions,
|
|
101
|
-
): Promise<void>;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Options for teardownTestFixtures
|
|
105
|
-
*
|
|
106
|
-
* @public
|
|
107
|
-
*/
|
|
108
|
-
declare interface TeardownTestFixturesOptions {
|
|
109
|
-
/**
|
|
110
|
-
* Custom temp directory path. Defaults to process.cwd()/tmp
|
|
111
|
-
*/
|
|
112
|
-
tempDir?: string;
|
|
113
|
-
}
|
|
114
|
-
|
|
17
|
+
declare function setupWorkerBuild(filePaths: string[]): Promise<void>;
|
|
115
18
|
/**
|
|
116
19
|
* Teardown function to clean up the worker build.
|
|
117
20
|
*
|
|
@@ -123,6 +26,6 @@ declare interface TeardownTestFixturesOptions {
|
|
|
123
26
|
* @throws If the build contexts cannot be disposed
|
|
124
27
|
* @throws If the compiled JavaScript files cannot be deleted
|
|
125
28
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
29
|
+
declare function teardownWorkerBuild(): Promise<void>;
|
|
30
|
+
export { setup, setupWorkerBuild, snapshotSerializer, teardown, teardownWorkerBuild };
|
|
31
|
+
//# sourceMappingURL=vitest.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/cli-test",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "11.0.0",
|
|
4
4
|
"description": "Sanity CLI test helpers and utilities",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|
|
@@ -106,21 +106,22 @@
|
|
|
106
106
|
"@swc/core": "^1.15.43",
|
|
107
107
|
"ansis": "^4.2.0",
|
|
108
108
|
"esbuild": "^0.28.1",
|
|
109
|
+
"get-it": "^9.5.0",
|
|
109
110
|
"nock": "^14.0.15",
|
|
110
111
|
"tinyglobby": "^0.2.17",
|
|
111
|
-
"@sanity/cli-core": "3.
|
|
112
|
+
"@sanity/cli-core": "3.1.0"
|
|
112
113
|
},
|
|
113
114
|
"devDependencies": {
|
|
114
115
|
"@eslint/compat": "^2.1.0",
|
|
115
116
|
"@oclif/core": "^4.11.14",
|
|
116
|
-
"@sanity/client": "^
|
|
117
|
-
"@sanity/pkg-utils": "^
|
|
117
|
+
"@sanity/client": "^8.1.0",
|
|
118
|
+
"@sanity/pkg-utils": "^12.1.2",
|
|
118
119
|
"@swc/cli": "^0.8.1",
|
|
119
120
|
"@types/node": "^22.20.0",
|
|
120
121
|
"eslint": "^10.7.0",
|
|
121
122
|
"publint": "^0.3.21",
|
|
122
123
|
"rimraf": "^6.0.1",
|
|
123
|
-
"tsx": "^4.23.
|
|
124
|
+
"tsx": "^4.23.12",
|
|
124
125
|
"typescript": "^6.0.3",
|
|
125
126
|
"vitest": "^4.1.10",
|
|
126
127
|
"yaml": "^2.9.0",
|
|
@@ -130,9 +131,9 @@
|
|
|
130
131
|
},
|
|
131
132
|
"peerDependencies": {
|
|
132
133
|
"@oclif/core": "^4.0.0",
|
|
133
|
-
"@sanity/client": "^
|
|
134
|
+
"@sanity/client": "^8.1.0",
|
|
134
135
|
"vitest": ">=3.0.0 <5.0.0",
|
|
135
|
-
"@sanity/cli-core": "3.
|
|
136
|
+
"@sanity/cli-core": "3.1.0"
|
|
136
137
|
},
|
|
137
138
|
"engines": {
|
|
138
139
|
"node": ">=22.12"
|