Migrate to vitest from jest (#1074)

Co-authored-by: Felix Weinberger <3823880+felixweinberger@users.noreply.github.com>
Co-authored-by: Felix Weinberger <fweinberger@anthropic.com>
This commit is contained in:
Matt 2025-11-14 14:08:00 +00:00 • committed by GitHub
commit 98df118a4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,33 +2,34 @@ import { StdioClientTransport, getDefaultEnvironment } from './stdio.js';
import spawn from 'cross-spawn';
import { JSONRPCMessage } from '../types.js';
import { ChildProcess } from 'node:child_process';
import { Mock, MockedFunction } from 'vitest';
// mock cross-spawn
jest.mock('cross-spawn');
const mockSpawn = spawn as jest.MockedFunction<typeof spawn>;
vi.mock('cross-spawn');
const mockSpawn = spawn as unknown as MockedFunction<typeof spawn>;
describe('StdioClientTransport using cross-spawn', () => {
beforeEach(() => {
// mock cross-spawn's return value
mockSpawn.mockImplementation(() => {
const mockProcess: {
on: jest.Mock;
stdin?: { on: jest.Mock; write: jest.Mock };
stdout?: { on: jest.Mock };
on: Mock;
stdin?: { on: Mock; write: Mock };
stdout?: { on: Mock };
stderr?: null;
} = {
on: jest.fn((event: string, callback: () => void) => {
on: vi.fn((event: string, callback: () => void) => {
if (event === 'spawn') {
callback();
}
return mockProcess;
}),
stdin: {
on: jest.fn(),
write: jest.fn().mockReturnValue(true)
on: vi.fn(),
write: vi.fn().mockReturnValue(true)
},
stdout: {
on: jest.fn()
on: vi.fn()
},
stderr: null
};
@ -37,7 +38,7 @@ describe('StdioClientTransport using cross-spawn', () => {
});
afterEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});
test('should call cross-spawn correctly', async () => {
@ -105,30 +106,30 @@ describe('StdioClientTransport using cross-spawn', () => {
// get the mock process object
const mockProcess: {
on: jest.Mock;
on: Mock;
stdin: {
on: jest.Mock;
write: jest.Mock;
once: jest.Mock;
on: Mock;
write: Mock;
once: Mock;
};
stdout: {
on: jest.Mock;
on: Mock;
};
stderr: null;
} = {
on: jest.fn((event: string, callback: () => void) => {
on: vi.fn((event: string, callback: () => void) => {
if (event === 'spawn') {
callback();
}
return mockProcess;
}),
stdin: {
on: jest.fn(),
write: jest.fn().mockReturnValue(true),
once: jest.fn()
on: vi.fn(),
write: vi.fn().mockReturnValue(true),
once: vi.fn()
},
stdout: {
on: jest.fn()
on: vi.fn()
},
stderr: null
};