Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion knip.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type KnipConfig} from 'knip'

const project = ['src/**/*.{js,jsx,ts,tsx}', '!**/docs/**']
const project = ['src/**/*.{js,jsx,ts,tsx}', 'test/**/*.{js,jsx,ts,tsx}', '!**/docs/**']

const baseConfig = {
// For now only care about cli package
Expand Down Expand Up @@ -62,6 +62,12 @@ const baseConfig = {
'src/**/*.worker.ts',
'package.config.ts',
],
ignoreFiles: [
'test/__fixtures__/cli-configs/error-both-ts-and-js/*', // used in an integration test
'test/__fixtures__/exec-*.ts',
'test/helpers/buildFixture.ts', // referenced in cli-test readme?
'test/snapshotSerializer.ts', // used in vitest.config
],
oclif: {
config: ['oclif.config.js'],
},
Expand Down
1 change: 1 addition & 0 deletions packages/@sanity/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
},
"devDependencies": {
"@eslint/compat": "catalog:",
"@heroku-cli/test-utils": "catalog:",
"@repo/package.config": "workspace:*",
"@repo/tsconfig": "workspace:*",
"@sanity/cli-test": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'

import {createMockSanityCommand} from '../../../test/mockSanityCommand.js'

// First: create the mocks and mocked SanityCommand class
const {MockedSanityCommand, mocks} = createMockSanityCommand()
// Second: install the mock on cli-core
vi.mock('@sanity/cli-core', async (importOriginal) => {
const actual = await importOriginal<typeof import('@sanity/cli-core')>()
return {...actual, SanityCommand: MockedSanityCommand}
})

// Third: mock doctor command imports
const mockDoctorChecks = {cli: vi.fn()} // coupled to actions/doctor/checks/index.js
const mockKnownChecks = Object.keys(mockDoctorChecks)
Expand All @@ -24,6 +15,8 @@ vi.mock('../../actions/doctor/runDoctorChecks.js', () => ({

// Finally, import the module under test: doctor command
const {DoctorCommand} = await import('../doctor.js')
// First: create the mocks and mocked SanityCommand class
const {createCmdInstance, mocks} = await createMockSanityCommand(DoctorCommand)

describe('doctor command', () => {
beforeEach(() => {
Expand All @@ -41,7 +34,7 @@ describe('doctor command', () => {
const checkResults = {checks: [passingCheck], summary: {passed: 420}}
mockRunDoctorChecks.mockResolvedValue(checkResults)

await DoctorCommand.run()
await createCmdInstance().run()

expect(mocks.SanityCmdOutputLog).toHaveBeenCalledWith(expect.stringContaining('Running diag'))
expect(mocks.SanityCmdOutputLog).toHaveBeenCalledWith(
Expand All @@ -58,7 +51,7 @@ describe('doctor command', () => {
const checkResults = {checks: [passingCheck], summary: {passed: 420}}
mockRunDoctorChecks.mockResolvedValue(checkResults)

await DoctorCommand.run(['--json'])
await createCmdInstance(['--json']).run()

expect(mocks.SanityCmdOutputLog).toHaveBeenCalledWith(
expect.stringContaining(JSON.stringify(checkResults, null, 2)),
Expand Down
47 changes: 18 additions & 29 deletions packages/@sanity/cli/src/commands/datasets/__tests__/copy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@ import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest'

import {createMockSanityCommand} from '../../../../test/mockSanityCommand.js'

// First: create the mocks and mocked SanityCommand class
const {MockedSanityCommand, mocks} = createMockSanityCommand()
// Second: install the mock on cli-core
vi.mock('@sanity/cli-core', async (importOriginal) => {
const actual = await importOriginal<typeof import('@sanity/cli-core')>()
return {
...actual,
SanityCommand: MockedSanityCommand,
}
})

// Third: mock dataset copy command imports
const mockPromptForDataset = vi.hoisted(() => vi.fn())
const mockPromptForDatasetName = vi.hoisted(() => vi.fn())
Expand Down Expand Up @@ -76,8 +65,8 @@ vi.mock('@sanity/cli-core/ux', async () => {
}
})

// Finally, import the module under test: dataset copy command
const {CopyDatasetCommand} = await import('../copy.js')
const {createCmdInstance, mocks} = await createMockSanityCommand(CopyDatasetCommand)

const TEST_PROJECT_ID = '1337newb'
function createMockDataset(name: string) {
Expand Down Expand Up @@ -109,7 +98,7 @@ describe('#dataset:copy', () => {
{desc: '--list with --detach', flags: ['--list', '--detach']},
{desc: '--attach with --detach', flags: ['--attach', 'job-123', '--detach']},
])('errors when using mutually exclusive flags: $desc', async ({flags}) => {
await expect(CopyDatasetCommand.run(flags)).rejects.toThrow(
await expect(createCmdInstance(flags).run()).rejects.toThrow(
expect.objectContaining({
message: expect.stringContaining('cannot also be provided when using'),
}),
Expand All @@ -120,7 +109,7 @@ describe('#dataset:copy', () => {
{flag: '--offset', value: '2'},
{flag: '--limit', value: '10'},
])('errors when $flag is used without --list', async ({flag, value}) => {
await expect(CopyDatasetCommand.run([flag, value])).rejects.toThrow(
await expect(createCmdInstance([flag, value]).run()).rejects.toThrow(
expect.objectContaining({
message: expect.stringMatching(/all of the following must be provided.*--list/i),
}),
Expand Down Expand Up @@ -155,7 +144,7 @@ describe('#dataset:copy', () => {
},
])

await CopyDatasetCommand.run(['--list', '--offset', '2', '--limit', '10'])
await createCmdInstance(['--list', '--offset', '2', '--limit', '10']).run()

expect(mockListDatasetCopyJobs).toHaveBeenCalledWith({
limit: 10,
Expand All @@ -172,15 +161,15 @@ describe('#dataset:copy', () => {

test('shows message when no copy jobs exist', async () => {
mockListDatasetCopyJobs.mockResolvedValue([])
await CopyDatasetCommand.run(['--list'])
await createCmdInstance(['--list']).run()
expect(mocks.SanityCmdOutputLog).toHaveBeenCalledWith(
expect.stringMatching(/doesn't have any dataset copy jobs/i),
)
})

test('errors and exits if list copy job API throws', async () => {
mockListDatasetCopyJobs.mockRejectedValue('boom')
await CopyDatasetCommand.run(['--list'])
await createCmdInstance(['--list']).run()
expect(mocks.SanityCmdOutputError).toHaveBeenCalledWith(
expect.stringMatching(/failed to list dataset copy jobs.*boom/i),
{exit: 1},
Expand All @@ -190,7 +179,7 @@ describe('#dataset:copy', () => {

describe('attach mode', () => {
test('rejects whitespace-only jobId', async () => {
await CopyDatasetCommand.run(['--attach', ' '])
await createCmdInstance(['--attach', ' ']).run()

expect(mocks.SanityCmdOutputError).toHaveBeenCalledWith(
expect.stringMatching(/supply a valid jobId/i),
Expand All @@ -207,7 +196,7 @@ describe('#dataset:copy', () => {
),
)

await CopyDatasetCommand.run(['--attach', 'job-123'])
await createCmdInstance(['--attach', 'job-123']).run()

expect(mockFollowCopyJob).toHaveBeenCalledWith({
jobId: 'job-123',
Expand All @@ -220,7 +209,7 @@ describe('#dataset:copy', () => {
test('errors out if progress tracking throws', async () => {
mockFollowCopyJob.mockThrow(new Error('boom'))

await CopyDatasetCommand.run(['--attach', 'job-123'])
await createCmdInstance(['--attach', 'job-123']).run()

expect(mocks.SanityCmdOutputError).toHaveBeenCalledWith(
expect.stringMatching(/failed to attach to copy.*boom/i),
Expand All @@ -238,7 +227,7 @@ describe('#dataset:copy', () => {
),
)

await CopyDatasetCommand.run(['--attach', 'job-123'])
await createCmdInstance(['--attach', 'job-123']).run()

expect(mockFollowCopyJob).toHaveBeenCalledWith({
jobId: 'job-123',
Expand All @@ -261,7 +250,7 @@ describe('#dataset:copy', () => {
mockCopyDataset.mockResolvedValue({jobId: 'job-456'})
mockFollowCopyJob.mockReturnValue(of({progress: 100, type: 'progress'}))

await CopyDatasetCommand.run(['production', 'backup'])
await createCmdInstance(['production', 'backup']).run()

expect(mockCopyDataset).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down Expand Up @@ -293,7 +282,7 @@ describe('#dataset:copy', () => {
mockPromptForDataset.mockResolvedValue('production')
mockPromptForDatasetName.mockResolvedValue('backup')

await CopyDatasetCommand.run([])
await createCmdInstance([]).run()

expect(mockPromptForDataset).toHaveBeenCalledOnce()
expect(mockPromptForDatasetName).toHaveBeenCalledWith({
Expand Down Expand Up @@ -348,7 +337,7 @@ describe('#dataset:copy', () => {
])('errors when $description', async ({args, expectedError, setupMocks}) => {
setupMocks()

await CopyDatasetCommand.run(args)
await createCmdInstance(args).run()

expect(mocks.SanityCmdOutputError).toHaveBeenCalledWith(
expect.stringMatching(new RegExp(expectedError)),
Expand All @@ -364,12 +353,12 @@ describe('#dataset:copy', () => {
mockCopyDataset.mockResolvedValue({jobId: 'job-skip'})
mockFollowCopyJob.mockReturnValue(of({progress: 100, type: 'progress'}))

await CopyDatasetCommand.run([
await createCmdInstance([
'production',
'backup',
'--skip-history',
'--skip-content-releases',
])
]).run()

expect(mockCopyDataset).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -392,7 +381,7 @@ describe('#dataset:copy', () => {
])
mockCopyDataset.mockResolvedValue({jobId: 'job-detach'})

await CopyDatasetCommand.run(['production', 'backup', '--detach'])
await createCmdInstance(['production', 'backup', '--detach']).run()

expect(mocks.SanityCmdOutputLog).toHaveBeenCalledWith(
expect.stringMatching(/job job-detach started/i),
Expand All @@ -410,7 +399,7 @@ describe('#dataset:copy', () => {
])
mockCopyDataset.mockRejectedValue(new Error('boom'))

await CopyDatasetCommand.run(['production', 'backup'])
await createCmdInstance(['production', 'backup']).run()

expect(mocks.SanityCmdOutputError).toHaveBeenCalledWith(
expect.stringMatching(/dataset copying failed: boom/i),
Expand All @@ -421,7 +410,7 @@ describe('#dataset:copy', () => {
test('handles list datasets error', async () => {
mockListDatasets.mockRejectedValue(new Error('boom'))

await CopyDatasetCommand.run(['production', 'backup'])
await createCmdInstance(['production', 'backup']).run()

expect(mocks.SanityCmdOutputError).toHaveBeenCalledWith(
expect.stringMatching(/failed to fetch datasets: boom/i),
Expand Down
Loading
Loading