@@ -8,7 +8,7 @@ import os from "os";
88import { basename , dirname , join , resolve } from "path" ;
99import { rimraf } from "rimraf" ;
1010import { fileURLToPath } from "url" ;
11- import { afterAll , afterEach , beforeEach , TestContext } from "vitest" ;
11+ import { afterAll , afterEach , beforeEach , onTestFailed , TestContext } from "vitest" ;
1212import { CopilotClient } from "../../../src" ;
1313import { CapiProxy } from "./CapiProxy" ;
1414import { retry } from "./sdkTestHelper" ;
@@ -17,7 +17,7 @@ const __filename = fileURLToPath(import.meta.url);
1717const __dirname = dirname ( __filename ) ;
1818const SNAPSHOTS_DIR = resolve ( __dirname , "../../../../test/snapshots" ) ;
1919
20- export const CLI_PATH = resolve ( __dirname , "../../../node_modules/@github/copilot/index.js" ) ;
20+ export const CLI_PATH = process . env . COPILOT_CLI_PATH || resolve ( __dirname , "../../../node_modules/@github/copilot/index.js" ) ;
2121
2222export async function createSdkTestContext ( ) {
2323 const homeDir = realpathSync ( fs . mkdtempSync ( join ( os . tmpdir ( ) , "copilot-test-config-" ) ) ) ;
@@ -44,8 +44,15 @@ export async function createSdkTestContext() {
4444
4545 const harness = { homeDir, workDir, openAiEndpoint, copilotClient, env } ;
4646
47+ // Track if any test fails to avoid writing corrupted snapshots
48+ let anyTestFailed = false ;
49+
4750 // Wire up to Vitest lifecycle
4851 beforeEach ( async ( testContext ) => {
52+ onTestFailed ( ( ) => {
53+ anyTestFailed = true ;
54+ } ) ;
55+
4956 await openAiEndpoint . updateConfig ( {
5057 filePath : getTrafficCapturePath ( testContext ) ,
5158 workDir,
@@ -63,7 +70,7 @@ export async function createSdkTestContext() {
6370
6471 afterAll ( async ( ) => {
6572 await copilotClient . stop ( ) ;
66- await openAiEndpoint . stop ( ) ;
73+ await openAiEndpoint . stop ( anyTestFailed ) ;
6774 await rmDir ( "remove e2e test homeDir" , homeDir ) ;
6875 await rmDir ( "remove e2e test workDir" , workDir ) ;
6976 } ) ;
0 commit comments