{"library":"mountebank","title":"Mountebank: Over-the-Wire Test Doubles","description":"Mountebank is an open-source, cross-platform, multi-protocol test double tool designed to mock services over the wire. It enables developers to create configurable mock API endpoints for various protocols like HTTP, HTTPS, TCP, and SMTP. Mountebank primarily operates by creating \"imposters\" that listen on specified ports, responding to requests based on defined \"stubs\" which include predicates (conditions) and responses. This approach allows for isolated and repeatable testing of applications, reducing dependencies on external services and improving development efficiency.\nThe project recently transitioned to a community-driven effort under the `mountebank-testing` GitHub organization, with the npm package name changing from `mountebank` to `@mbtest/mountebank`. It maintains a consistent release cadence, with several updates in the past year (v2.9.1 to v2.9.4). Key differentiators include its multi-protocol support, a powerful REST API for dynamic configuration, and the ability to extend functionality through JavaScript injection.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install mountebank"],"cli":{"name":"mb","version":null}},"imports":["npm install -g @mbtest/mountebank && mb","const mb = require('@mbtest/mountebank'); const server = await mb.create({ port: 2525 });","fetch('http://localhost:2525/imposters', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(imposterConfig) })"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { strict as assert } from 'assert';\nimport { exec } from 'child_process';\nimport util from 'util';\n\nconst execPromise = util.promisify(exec);\n\nconst imposterConfig = {\n  port: 4545,\n  protocol: 'http',\n  stubs: [\n    {\n      predicates: [{\n        equals: { path: '/test', method: 'GET' }\n      }],\n      responses: [\n        { is: { statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: 'Hello from Mountebank!' }) } }\n      ]\n    }\n  ]\n};\n\nasync function runMountebankQuickstart() {\n  let mbProcess;\n  try {\n    console.log('1. Installing @mbtest/mountebank globally...');\n    await execPromise('npm install -g @mbtest/mountebank');\n    console.log('   Installed successfully.');\n\n    console.log('2. Starting Mountebank server...');\n    // Using `exec` to start mb in the background, ensure --allowInjection for future use cases\n    // and --loglevel debug for better visibility.\n    mbProcess = exec('mb --allowInjection --loglevel debug');\n    mbProcess.stdout.pipe(process.stdout);\n    mbProcess.stderr.pipe(process.stderr);\n\n    // Give Mountebank a moment to start up\n    await new Promise(resolve => setTimeout(resolve, 3000)); \n    console.log('   Mountebank server started (admin UI at http://localhost:2525).');\n\n    console.log('3. Creating an HTTP imposter...');\n    await fetch('http://localhost:2525/imposters', {\n      method: 'POST',\n      headers: { 'Content-Type': 'application/json' },\n      body: JSON.stringify(imposterConfig)\n    });\n    console.log('   Imposter created on port 4545.');\n\n    console.log('4. Testing the mocked service...');\n    const response = await fetch('http://localhost:4545/test');\n    const data = await response.json();\n\n    assert.equal(response.status, 200, 'Expected status code 200');\n    assert.deepEqual(data, { message: 'Hello from Mountebank!' }, 'Expected specific mock response');\n    console.log('   Mocked service responded correctly:', data);\n\n  } catch (error) {\n    console.error('An error occurred:', error.message);\n    process.exit(1);\n  } finally {\n    if (mbProcess) {\n      console.log('5. Stopping Mountebank server...');\n      // Terminate the mountebank process gracefully\n      await execPromise('mb stop');\n      console.log('   Mountebank server stopped.');\n    }\n  }\n}\n\nrunMountebankQuickstart();","lang":"javascript","description":"This quickstart demonstrates how to install Mountebank globally, start the `mb` server, programmatically create a simple HTTP GET imposter via its REST API, and then verify the mock response.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}