From 4c9dd7ffccf9f9fb1bad85d543469b7aa04360b1 Mon Sep 17 00:00:00 2001 From: Test User Date: Fri, 22 Dec 2023 15:20:43 +0100 Subject: [PATCH] refactor(expo-cli): make spawn failures in `eject-test.ts` visible --- packages/expo-cli/e2e/TestUtils.ts | 17 +++++++++++++---- packages/expo-cli/e2e/__tests__/eject-test.ts | 19 ++++++++++--------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/packages/expo-cli/e2e/TestUtils.ts b/packages/expo-cli/e2e/TestUtils.ts index 0205f990c6..d242d48a98 100644 --- a/packages/expo-cli/e2e/TestUtils.ts +++ b/packages/expo-cli/e2e/TestUtils.ts @@ -1,6 +1,6 @@ import { ExpoConfig } from '@expo/config'; import JsonFile from '@expo/json-file'; -import spawnAsync, { SpawnOptions, SpawnResult } from '@expo/spawn-async'; +import spawnAsync, { SpawnOptions, SpawnPromise, SpawnResult } from '@expo/spawn-async'; import fs from 'fs'; import path from 'path'; @@ -14,8 +14,14 @@ function isSpawnResult(errorOrResult: Error): errorOrResult is Error & SpawnResu export async function runAsync(args: string[], options?: SpawnOptions): Promise { const promise = spawnAsync(EXPO_CLI, args, options); - promise.child.stdout.pipe(process.stdout); - promise.child.stderr.pipe(process.stderr); + + promise.child.stdout?.pipe(process.stdout); + promise.child.stderr?.pipe(process.stderr); + + return await handleSpawnResult(promise); +} + +export async function handleSpawnResult(promise: SpawnPromise): Promise { try { return await promise; } catch (error: any) { @@ -90,7 +96,10 @@ export async function createMinimalProjectAsync( // TODO(Bacon): We shouldn't need this // Install the packages so eject can infer the versions - await spawnAsync('yarn', [], { cwd: projectRoot, stdio: ['ignore', 'inherit', 'inherit'] }); + spawnAsync('yarn', [], { + cwd: projectRoot, + stdio: process.env.CI === 'true' ? 'inherit' : ['ignore', 'inherit', 'inherit'], + }); return projectRoot; } diff --git a/packages/expo-cli/e2e/__tests__/eject-test.ts b/packages/expo-cli/e2e/__tests__/eject-test.ts index 269f7a9a50..387cce039d 100644 --- a/packages/expo-cli/e2e/__tests__/eject-test.ts +++ b/packages/expo-cli/e2e/__tests__/eject-test.ts @@ -10,6 +10,7 @@ import { EXPO_CLI, getBasicExpoConfig, getBasicPackageJson, + handleSpawnResult, } from '../TestUtils'; const tempDir = temporary.directory(); @@ -32,16 +33,16 @@ beforeAll(async () => { function executeDefaultAsync(cwd: string, args: string[]) { const promise = spawnAsync(EXPO_CLI, args, { cwd }); - promise.child.stdout.pipe(process.stdout); - promise.child.stderr.pipe(process.stderr); + promise.child.stdout?.pipe(process.stdout); + promise.child.stderr?.pipe(process.stderr); // When the test is prompted to use git, skip message // TODO(Bacon): this shouldn't be blocking in non-interactive - promise.child.stdout.on('data', data => { + promise.child.stdout?.on('data', data => { const stdout = data.toString(); // Skip dirty git if (/Would you like to proceed/.test(stdout)) { - promise.child.stdin.write('\n'); + promise.child.stdin?.write('\n'); } }); @@ -59,7 +60,7 @@ it(`can eject a minimal project`, async () => { const res = executeDefaultAsync(projectRoot, ['eject']); // This shouldn't fail - await res; + await handleSpawnResult(res); // Test that native folders were generated expect(fileExists(projectName, 'ios/hworld.xcodeproj')).toBe(true); @@ -74,11 +75,11 @@ it(`can eject a minimal project`, async () => { // Remove main expect(outputPkgJson.main).toBe(undefined); // Scripts should be rewritten to use react-native-community/cli - expect(outputPkgJson.scripts['ios']).toBe('expo run:ios'); - expect(outputPkgJson.scripts['android']).toBe('expo run:android'); - expect(outputPkgJson.scripts['web']).toBe('expo start --web'); + expect(outputPkgJson.scripts?.['ios']).toBe('expo run:ios'); + expect(outputPkgJson.scripts?.['android']).toBe('expo run:android'); + expect(outputPkgJson.scripts?.['web']).toBe('expo start --web'); // Ensure the react-native version doesn't change - expect(outputPkgJson.dependencies['react-native']).toBe( + expect(outputPkgJson.dependencies?.['react-native']).toBe( getBasicPackageJson().dependencies['react-native'] ); });