feat: moves project to using vitest

This commit is contained in:
Marais Rossouw
2025-07-24 18:08:26 +10:00
parent 64cbe74d35
commit 4ba8e6bc1e
15 changed files with 643 additions and 7110 deletions

View File

@@ -1,4 +1,4 @@
import { describe, it, expect } from '@jest/globals'
import { describe, it, expect } from 'vitest'
import {
buildMessages,
buildResponseFormat,

View File

@@ -1,26 +1,21 @@
/**
* Unit tests for the helpers module, src/helpers.ts
*/
import { jest } from '@jest/globals'
import { vi, it, expect, beforeEach, describe } from 'vitest'
import * as core from '../__fixtures__/core.js'
// Mock fs module
const mockExistsSync = jest.fn()
const mockReadFileSync = jest.fn()
const mockExistsSync = vi.fn()
const mockReadFileSync = vi.fn()
jest.unstable_mockModule('fs', () => ({
vi.mock('fs', () => ({
existsSync: mockExistsSync,
readFileSync: mockReadFileSync
}))
jest.unstable_mockModule('@actions/core', () => core)
vi.mock('@actions/core', () => core)
// Import the module being tested
const { loadContentFromFileOrInput } = await import('../src/helpers.js')
describe('helpers.ts', () => {
beforeEach(() => {
jest.clearAllMocks()
vi.clearAllMocks()
})
describe('loadContentFromFileOrInput', () => {

View File

@@ -1,32 +1,34 @@
/**
* Unit tests for the inference module, src/inference.ts
*/
import { jest } from '@jest/globals'
import {
vi,
type MockedFunction,
beforeEach,
expect,
describe,
it
} from 'vitest'
import * as core from '../__fixtures__/core.js'
// Mock Azure AI Inference
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockPost = jest.fn() as jest.MockedFunction<any>
const mockPath = jest.fn(() => ({ post: mockPost }))
const mockClient = jest.fn(() => ({ path: mockPath }))
const mockPost = vi.fn() as MockedFunction<any>
const mockPath = vi.fn(() => ({ post: mockPost }))
const mockClient = vi.fn(() => ({ path: mockPath }))
jest.unstable_mockModule('@azure-rest/ai-inference', () => ({
vi.mock('@azure-rest/ai-inference', () => ({
default: mockClient,
isUnexpected: jest.fn(() => false)
isUnexpected: vi.fn(() => false)
}))
jest.unstable_mockModule('@azure/core-auth', () => ({
AzureKeyCredential: jest.fn()
vi.mock('@azure/core-auth', () => ({
AzureKeyCredential: vi.fn()
}))
// Mock MCP functions
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockExecuteToolCalls = jest.fn() as jest.MockedFunction<any>
jest.unstable_mockModule('../src/mcp.js', () => ({
const mockExecuteToolCalls = vi.fn() as MockedFunction<any>
vi.mock('../src/mcp.js', () => ({
executeToolCalls: mockExecuteToolCalls
}))
jest.unstable_mockModule('@actions/core', () => core)
vi.mock('@actions/core', () => core)
// Import the module being tested
const { simpleInference, mcpInference } = await import('../src/inference.js')
@@ -44,7 +46,7 @@ describe('inference.ts', () => {
}
beforeEach(() => {
jest.clearAllMocks()
vi.clearAllMocks()
})
describe('simpleInference', () => {

View File

@@ -1,38 +1,46 @@
import { describe, it, expect, beforeEach, jest } from '@jest/globals'
import {
describe,
it,
expect,
beforeEach,
vi,
type MockedFunction,
type Mock
} from 'vitest'
import * as core from '../__fixtures__/core.js'
// Create fs mocks
const mockExistsSync = jest.fn()
const mockReadFileSync = jest.fn()
const mockWriteFileSync = jest.fn()
const mockExistsSync = vi.fn()
const mockReadFileSync = vi.fn()
const mockWriteFileSync = vi.fn()
// Create inference mocks
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockSimpleInference = jest.fn() as jest.MockedFunction<any>
const mockMcpInference = jest.fn()
const mockSimpleInference = vi.fn() as MockedFunction<any>
const mockMcpInference = vi.fn()
// Create MCP mocks
const mockConnectToGitHubMCP = jest.fn()
const mockConnectToGitHubMCP = vi.fn()
// Mock fs module
jest.unstable_mockModule('fs', () => ({
vi.mock('fs', () => ({
existsSync: mockExistsSync,
readFileSync: mockReadFileSync,
writeFileSync: mockWriteFileSync
}))
// Mock the inference functions
jest.unstable_mockModule('../src/inference.js', () => ({
vi.mock('../src/inference.js', () => ({
simpleInference: mockSimpleInference,
mcpInference: mockMcpInference
}))
// Mock the MCP connection
jest.unstable_mockModule('../src/mcp.js', () => ({
vi.mock('../src/mcp.js', () => ({
connectToGitHubMCP: mockConnectToGitHubMCP
}))
jest.unstable_mockModule('@actions/core', () => core)
vi.mock('@actions/core', () => core)
// The module being tested should be imported dynamically. This ensures that the
// mocks are used in place of any actual dependencies.
@@ -40,7 +48,7 @@ const { run } = await import('../src/main.js')
describe('main.ts - prompt.yml integration', () => {
beforeEach(() => {
jest.clearAllMocks()
vi.clearAllMocks()
// Mock environment variables
process.env['GITHUB_TOKEN'] = 'test-token'
@@ -62,7 +70,7 @@ describe('main.ts - prompt.yml integration', () => {
})
// Mock core.getBooleanInput
const mockGetBooleanInput = core.getBooleanInput as jest.Mock
const mockGetBooleanInput = core.getBooleanInput as Mock
mockGetBooleanInput.mockReturnValue(false)
// Mock fs.readFileSync for prompt file

View File

@@ -1,21 +1,25 @@
/**
* Unit tests for the action's main functionality, src/main.ts
*/
import { jest } from '@jest/globals'
import {
vi,
describe,
expect,
it,
beforeEach,
type MockedFunction
} from 'vitest'
import * as core from '../__fixtures__/core.js'
// Default to throwing errors to catch unexpected calls
const mockExistsSync = jest.fn().mockImplementation(() => {
const mockExistsSync = vi.fn().mockImplementation(() => {
throw new Error(
'Unexpected call to existsSync - test should override this implementation'
)
})
const mockReadFileSync = jest.fn().mockImplementation(() => {
const mockReadFileSync = vi.fn().mockImplementation(() => {
throw new Error(
'Unexpected call to readFileSync - test should override this implementation'
)
})
const mockWriteFileSync = jest.fn()
const mockWriteFileSync = vi.fn()
/**
* Helper function to mock file system operations for one or more files
@@ -83,7 +87,7 @@ function verifyStandardResponse(): void {
)
}
jest.unstable_mockModule('fs', () => ({
vi.mock('fs', () => ({
existsSync: mockExistsSync,
readFileSync: mockReadFileSync,
writeFileSync: mockWriteFileSync
@@ -91,22 +95,22 @@ jest.unstable_mockModule('fs', () => ({
// Mock MCP and inference modules
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockConnectToGitHubMCP = jest.fn() as jest.MockedFunction<any>
const mockConnectToGitHubMCP = vi.fn() as MockedFunction<any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockSimpleInference = jest.fn() as jest.MockedFunction<any>
const mockSimpleInference = vi.fn() as MockedFunction<any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockMcpInference = jest.fn() as jest.MockedFunction<any>
const mockMcpInference = vi.fn() as MockedFunction<any>
jest.unstable_mockModule('../src/mcp.js', () => ({
vi.mock('../src/mcp.js', () => ({
connectToGitHubMCP: mockConnectToGitHubMCP
}))
jest.unstable_mockModule('../src/inference.js', () => ({
vi.mock('../src/inference.js', () => ({
simpleInference: mockSimpleInference,
mcpInference: mockMcpInference
}))
jest.unstable_mockModule('@actions/core', () => core)
vi.mock('@actions/core', () => core)
// The module being tested should be imported dynamically. This ensures that the
// mocks are used in place of any actual dependencies.
@@ -115,7 +119,7 @@ const { run } = await import('../src/main.js')
describe('main.ts', () => {
// Reset all mocks before each test
beforeEach(() => {
jest.clearAllMocks()
vi.clearAllMocks()
// Remove any existing GITHUB_TOKEN
delete process.env.GITHUB_TOKEN

View File

@@ -1,16 +1,20 @@
/**
* Unit tests for the MCP module, src/mcp.ts
*/
import { jest } from '@jest/globals'
import {
vi,
type MockedFunction,
describe,
it,
expect,
beforeEach
} from 'vitest'
import * as core from '../__fixtures__/core.js'
// Mock MCP SDK
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockConnect = jest.fn() as jest.MockedFunction<any>
const mockConnect = vi.fn() as MockedFunction<any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockListTools = jest.fn() as jest.MockedFunction<any>
const mockListTools = vi.fn() as MockedFunction<any>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockCallTool = jest.fn() as jest.MockedFunction<any>
const mockCallTool = vi.fn() as MockedFunction<any>
const mockClient = {
connect: mockConnect,
@@ -19,18 +23,15 @@ const mockClient = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any
jest.unstable_mockModule('@modelcontextprotocol/sdk/client/index.js', () => ({
Client: jest.fn(() => mockClient)
vi.mock('@modelcontextprotocol/sdk/client/index.js', () => ({
Client: vi.fn(() => mockClient)
}))
jest.unstable_mockModule(
'@modelcontextprotocol/sdk/client/streamableHttp.js',
() => ({
StreamableHTTPClientTransport: jest.fn()
})
)
vi.mock('@modelcontextprotocol/sdk/client/streamableHttp.js', () => ({
StreamableHTTPClientTransport: vi.fn()
}))
jest.unstable_mockModule('@actions/core', () => core)
vi.mock('@actions/core', () => core)
// Import the module being tested
const { connectToGitHubMCP, executeToolCall, executeToolCalls } = await import(
@@ -39,7 +40,7 @@ const { connectToGitHubMCP, executeToolCall, executeToolCalls } = await import(
describe('mcp.ts', () => {
beforeEach(() => {
jest.clearAllMocks()
vi.clearAllMocks()
})
describe('connectToGitHubMCP', () => {

View File

@@ -1,4 +1,4 @@
import { describe, it, expect } from '@jest/globals'
import { describe, it, expect } from 'vitest'
import * as path from 'path'
import { fileURLToPath } from 'url'
import {