diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 57f662ef21b..0e8ec9dcf49 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -80,6 +80,6 @@ jobs: run: | node -v npm -v - yarn test + yarn test --silent env: SANITY_CI_CLI_AUTH_TOKEN: ${{ secrets.SANITY_CI_CLI_AUTH_TOKEN }} diff --git a/packages/@sanity/cli/test/preview.test.ts b/packages/@sanity/cli/test/preview.test.ts index b1235400a4c..e937233ebb1 100644 --- a/packages/@sanity/cli/test/preview.test.ts +++ b/packages/@sanity/cli/test/preview.test.ts @@ -54,12 +54,11 @@ describeCliTest('CLI: `sanity preview`', () => { expect(previewHtml).toContain('

This is static.

') }) - testConcurrent('start (hint for new `dev` command)', async () => { - const result = await runSanityCmdCommand('v3', ['start'], {expectFailure: true}) - const error = result.stderr.trim() - expect(error).toContain('command is used to preview static builds') - expect(error).toContain('sanity dev') - expect(result.code).toBe(1) + testConcurrent('start (hint for new `dev` command)', () => { + return expect(runSanityCmdCommand('v3', ['start'])).rejects.toMatchObject({ + stderr: /.*(command is used to preview static builds).*(sanity dev).*/, + code: 1, + }) }) }) }) diff --git a/packages/@sanity/cli/test/shared/environment.ts b/packages/@sanity/cli/test/shared/environment.ts index 59ec4d53b49..eda5681eabb 100644 --- a/packages/@sanity/cli/test/shared/environment.ts +++ b/packages/@sanity/cli/test/shared/environment.ts @@ -101,25 +101,19 @@ export const getTestRunArgs = (version: string) => { } } -export async function runSanityCmdCommand( +export function runSanityCmdCommand( version: string, args: string[], - options: {env?: Record; expectFailure?: boolean} = {}, + options: {env?: Record} = {}, ): Promise<{ code: number | null stdout: string stderr: string }> { - const result = await exec(process.argv[0], [cliBinPath, ...args], { + return exec(process.argv[0], [cliBinPath, ...args], { cwd: path.join(studiosPath, version), env: {...sanityEnv, ...options.env}, }) - - if (result.code !== 0 && !options.expectFailure) { - throw new Error(`Command failed with code ${result.code}. stderr: ${result.stderr}`) - } - - return result } export function exec( @@ -131,7 +125,7 @@ export function exec( stdout: string stderr: string }> { - return new Promise((resolve) => { + return new Promise((resolve, reject) => { const stdout: Buffer[] = [] const stderr: Buffer[] = [] @@ -143,7 +137,42 @@ export function exec( const stdoutStr = Buffer.concat(stdout).toString('utf8') const stderrStr = Buffer.concat(stderr).toString('utf8') - resolve({code, stdout: stdoutStr, stderr: stderrStr}) + if (code === 0) { + resolve({code, stdout: stdoutStr, stderr: stderrStr}) + } else { + reject( + new ExecError( + `Command failed: +Exit code: ${code} +Command: ${command} ${args.join(' ')} +CWD: ${options.cwd}\n +--- stderr --- +${stderrStr}\n +--------------\n +${ + stdoutStr && + `--- stdout ---\n +${stdoutStr}\n +--------------\n` +}`, + code || 1, + stdoutStr, + stderrStr, + ), + ) + } }) }) } + +class ExecError extends Error { + code: number + stdout: string + stderr: string + constructor(message: string, code: number, stdout: string, stderr: string) { + super(message) + this.code = code + this.stdout = stdout + this.stderr = stderr + } +}