|
| 1 | +import {existsSync, readFileSync} from 'node:fs' |
| 2 | +import {rm} from 'node:fs/promises' |
| 3 | +import {join} from 'node:path' |
| 4 | + |
| 5 | +import {testFixture} from '@sanity/cli-test' |
| 6 | +import {afterEach, beforeEach, describe, expect, test} from 'vitest' |
| 7 | + |
| 8 | +import {getE2EProjectId, runCli} from '../../helpers/runCli.js' |
| 9 | + |
| 10 | +const projectId = getE2EProjectId() |
| 11 | + |
| 12 | +describe('sanity init - Next.js integration', {timeout: 120_000}, () => { |
| 13 | + let nextjsDir: string |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + nextjsDir = await testFixture('nextjs-app', {useSystemTmp: true}) |
| 17 | + await rm(join(nextjsDir, 'node_modules'), {force: true, recursive: true}) |
| 18 | + }) |
| 19 | + |
| 20 | + afterEach(async () => { |
| 21 | + if (nextjsDir) await rm(nextjsDir, {force: true, recursive: true}) |
| 22 | + }) |
| 23 | + |
| 24 | + describe.each([ |
| 25 | + {label: 'with -y flag', yFlag: ['-y']}, |
| 26 | + {label: 'unattended (no -y)', yFlag: [] as string[]}, |
| 27 | + ])('non-interactive ($label)', ({yFlag}) => { |
| 28 | + test('creates sanity config, schema, and cli files with --nextjs-add-config-files', async () => { |
| 29 | + const {error, exitCode} = await runCli({ |
| 30 | + args: [ |
| 31 | + 'init', |
| 32 | + ...yFlag, |
| 33 | + '--project', |
| 34 | + projectId, |
| 35 | + '--dataset', |
| 36 | + 'production', |
| 37 | + '--nextjs-add-config-files', |
| 38 | + '--package-manager', |
| 39 | + 'pnpm', |
| 40 | + ], |
| 41 | + cwd: nextjsDir, |
| 42 | + }) |
| 43 | + |
| 44 | + if (error) throw error |
| 45 | + expect(exitCode).toBe(0) |
| 46 | + |
| 47 | + expect(existsSync(`${nextjsDir}/sanity.config.ts`)).toBe(true) |
| 48 | + expect(existsSync(`${nextjsDir}/sanity/schemaTypes/index.ts`)).toBe(true) |
| 49 | + |
| 50 | + const cliConfig = readFileSync(`${nextjsDir}/sanity.cli.ts`, 'utf8') |
| 51 | + expect(cliConfig).toContain('NEXT_PUBLIC_SANITY_PROJECT_ID') |
| 52 | + }) |
| 53 | + |
| 54 | + test('creates embedded studio route with --nextjs-embed-studio', async () => { |
| 55 | + const {error, exitCode} = await runCli({ |
| 56 | + args: [ |
| 57 | + 'init', |
| 58 | + ...yFlag, |
| 59 | + '--project', |
| 60 | + projectId, |
| 61 | + '--dataset', |
| 62 | + 'production', |
| 63 | + '--nextjs-add-config-files', |
| 64 | + '--nextjs-embed-studio', |
| 65 | + '--package-manager', |
| 66 | + 'pnpm', |
| 67 | + ], |
| 68 | + cwd: nextjsDir, |
| 69 | + }) |
| 70 | + |
| 71 | + if (error) throw error |
| 72 | + expect(exitCode).toBe(0) |
| 73 | + expect(existsSync(`${nextjsDir}/app/studio/[[...tool]]/page.tsx`)).toBe(true) |
| 74 | + }) |
| 75 | + |
| 76 | + test('writes env variables to .env.local with --nextjs-append-env', async () => { |
| 77 | + const {error, exitCode} = await runCli({ |
| 78 | + args: [ |
| 79 | + 'init', |
| 80 | + ...yFlag, |
| 81 | + '--project', |
| 82 | + projectId, |
| 83 | + '--dataset', |
| 84 | + 'production', |
| 85 | + '--nextjs-add-config-files', |
| 86 | + '--nextjs-append-env', |
| 87 | + '--package-manager', |
| 88 | + 'pnpm', |
| 89 | + ], |
| 90 | + cwd: nextjsDir, |
| 91 | + }) |
| 92 | + |
| 93 | + if (error) throw error |
| 94 | + expect(exitCode).toBe(0) |
| 95 | + |
| 96 | + const envContent = readFileSync(`${nextjsDir}/.env.local`, 'utf8') |
| 97 | + expect(envContent).toContain('NEXT_PUBLIC_SANITY_PROJECT_ID') |
| 98 | + expect(envContent).toContain('NEXT_PUBLIC_SANITY_DATASET') |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe('interactive', () => { |
| 103 | + test('detects Next.js and completes with config files', async () => { |
| 104 | + const session = await runCli({ |
| 105 | + args: [ |
| 106 | + 'init', |
| 107 | + '--project', |
| 108 | + projectId, |
| 109 | + '--dataset', |
| 110 | + 'production', |
| 111 | + '--package-manager', |
| 112 | + 'pnpm', |
| 113 | + '--no-mcp', |
| 114 | + '--no-git', |
| 115 | + ], |
| 116 | + cwd: nextjsDir, |
| 117 | + interactive: true, |
| 118 | + }) |
| 119 | + |
| 120 | + await session.waitForText(/Would you like to add configuration files/i) |
| 121 | + session.sendKey('Enter') |
| 122 | + |
| 123 | + await session.waitForText(/Do you want to use TypeScript/i) |
| 124 | + session.sendKey('Enter') |
| 125 | + |
| 126 | + await session.waitForText(/Would you like an embedded Sanity Studio/i) |
| 127 | + session.sendKey('Enter') |
| 128 | + |
| 129 | + await session.waitForText(/What route do you want to use for the Studio/i) |
| 130 | + session.sendKey('Enter') |
| 131 | + |
| 132 | + await session.waitForText(/Select project template to use/i) |
| 133 | + session.sendKey('Enter') |
| 134 | + |
| 135 | + await session.waitForText(/Would you like to add the project ID and dataset/i) |
| 136 | + session.sendKey('Enter') |
| 137 | + |
| 138 | + const exitCode = await session.waitForExit(90_000) |
| 139 | + expect(exitCode).toBe(0) |
| 140 | + |
| 141 | + expect(existsSync(`${nextjsDir}/sanity.config.ts`)).toBe(true) |
| 142 | + |
| 143 | + const output = session.getOutput() |
| 144 | + expect(output).toMatch(/\/studio/i) |
| 145 | + expect(output).not.toMatch(/Project output path/i) |
| 146 | + }) |
| 147 | + |
| 148 | + test('accepts custom studio route', async () => { |
| 149 | + const session = await runCli({ |
| 150 | + args: [ |
| 151 | + 'init', |
| 152 | + '--project', |
| 153 | + projectId, |
| 154 | + '--dataset', |
| 155 | + 'production', |
| 156 | + '--package-manager', |
| 157 | + 'pnpm', |
| 158 | + '--no-mcp', |
| 159 | + '--no-git', |
| 160 | + ], |
| 161 | + cwd: nextjsDir, |
| 162 | + interactive: true, |
| 163 | + }) |
| 164 | + |
| 165 | + await session.waitForText(/Would you like to add configuration files/i) |
| 166 | + session.sendKey('Enter') |
| 167 | + |
| 168 | + await session.waitForText(/Do you want to use TypeScript/i) |
| 169 | + session.sendKey('Enter') |
| 170 | + |
| 171 | + await session.waitForText(/Would you like an embedded Sanity Studio/i) |
| 172 | + session.sendKey('Enter') |
| 173 | + |
| 174 | + await session.waitForText(/What route do you want to use for the Studio/i) |
| 175 | + session.write('/admin\n') |
| 176 | + |
| 177 | + await session.waitForText(/Select project template to use/i) |
| 178 | + session.sendKey('Enter') |
| 179 | + |
| 180 | + await session.waitForText(/Would you like to add the project ID and dataset/i) |
| 181 | + session.sendKey('Enter') |
| 182 | + |
| 183 | + const exitCode = await session.waitForExit(90_000) |
| 184 | + expect(exitCode).toBe(0) |
| 185 | + |
| 186 | + expect(existsSync(`${nextjsDir}/app/admin/[[...tool]]/page.tsx`)).toBe(true) |
| 187 | + expect(existsSync(`${nextjsDir}/sanity.config.ts`)).toBe(true) |
| 188 | + }) |
| 189 | + }) |
| 190 | +}) |
0 commit comments