How chihebnabil/openai-api-mock works
Project type and purpose
• This is an open-source Node.js library (module) whose sole purpose is to mock OpenAI API endpoints in a development or test environment. It doesn’t stand up servers or offer a UI—it hooks into HTTP calls made by an OpenAI client and returns synthetic responses for chat completions (normal and streaming) and image generation.
File structure
• Top-level files:
• package.json, package-lock.json, vite.config.js – build and dependency metadata
• jest.config.js, __tests__/… – test suite
• .github/workflows/test.yml – CI integration
• src/ directory:
• index.js – main export (mockOpenAIResponse)
• chat.js – mocks non-stream chat completions
• chat.stream.js – mocks streaming chat completions
• image.js – mocks image generation endpoints
• utils/
– fakeDataGenerators.js – builds fake chat/image content (leverages @faker-js/faker)
• Third-party intercept library: nock
README for additional insights
• The README confirms usage patterns (mockOpenAIResponse(force, options)), supported endpoints, and custom endpoint registration via mock.addCustomEndpoint().
• It documents simulated latency, error injection, and request logging features.