test: Add mock for execa module

- Create CommonJS mock implementation of execa
- Add execa to moduleNameMapper in Jest config
- Resolves 'Cannot use import statement outside a module' error for execa
This commit is contained in:
Smartsheet-JB-Brown 2025-05-23 16:18:03 -07:00 committed by hannesrudolph
parent 87236c7ea0
commit 56cded1742
3 changed files with 22 additions and 5 deletions

13
src/__mocks__/execa.js Normal file
View file

@ -0,0 +1,13 @@
// Mock implementation of execa
module.exports = {
execa: jest.fn().mockResolvedValue({
stdout: "",
stderr: "",
exitCode: 0,
}),
execaSync: jest.fn().mockReturnValue({
stdout: "",
stderr: "",
exitCode: 0,
}),
}

View file

@ -38,9 +38,10 @@ export default {
"^os-name$": "<rootDir>/__mocks__/os-name.js",
"^strip-bom$": "<rootDir>/__mocks__/strip-bom.js",
"^kontroll$": "<rootDir>/__mocks__/kontroll.js",
"^execa$": "<rootDir>/__mocks__/execa.js",
},
transformIgnorePatterns: [
"node_modules/(?!(@modelcontextprotocol|delay|p-wait-for|serialize-error|strip-ansi|default-shell|os-name|strip-bom|kontroll)/)",
"node_modules/(?!(@modelcontextprotocol|delay|p-wait-for|serialize-error|strip-ansi|default-shell|os-name|strip-bom|kontroll|execa)/)",
],
roots: ["<rootDir>"],
modulePathIgnorePatterns: ["dist", "out"],

View file

@ -73,6 +73,8 @@ describe("MetadataScanner", () => {
const normalizedBasePath = normalizePath(mockBasePath)
const normalizedComponentPath = normalizePath(path.join(mockBasePath, "component1"))
process.stdout.write(`\nMock readdir called with path: ${normalizedP}\n`)
if (normalizedP === normalizedBasePath) {
// For the base path, return only the component directory
const baseDirents = [componentDirDirent]
@ -87,15 +89,16 @@ describe("MetadataScanner", () => {
}
})
mockedFs.readFile.mockResolvedValue(
Buffer.from(`
mockedFs.readFile.mockImplementation(async (p: string | Buffer | URL | FileHandle) => {
process.stdout.write(`\nMock readFile called with path: ${String(p)}\n`)
return Buffer.from(`
name: Test Component
description: A test component
type: mcp
version: 1.0.0
sourceUrl: https://example.com/component1
`),
)
`)
})
const items = await metadataScanner.scanDirectory(mockBasePath, mockRepoUrl)