Registry / testing / mountebank-formatters

mountebank-formatters

JSON →
library0.0.2jsnpmunverified

mountebank-formatters provides the default parsing and formatting logic for Mountebank test data, enabling the `mb save` and `mb start --configfile` commands to round-trip configuration files. Currently at version 0.0.2, this package was created to modularize Mountebank's dependency on EJS templating. Previously, breaking changes in EJS could not be adopted by Mountebank without breaking existing configurations. By externalizing the default formatter, Mountebank core can now upgrade EJS independently while providing backward compatibility via this module. This package's primary functionality revolves around `load` and `save` functions, which handle reading and writing Mountebank's `--configfile` and saved test data, respectively. It serves as the default formatter but also highlights Mountebank's broader support for custom formatters within the Mountebank ecosystem. The release cadence is directly tied to Mountebank's development needs, as it is a core utility. Key differentiators include its role in maintaining backward compatibility for Mountebank configurations while allowing Mountebank to adopt modern templating engines.

npm install mountebank-formatters
INSTALL
IMPORT
SIG · MOUNTEBANK-FORMATT
M
mountebank-formatters
testingjavascriptv0.0.2
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

load, save
const { load, save } = require('mountebank-formatters');
const mountebankFormatters = require('mountebank-formatters');
CommonJS named destructuring is the standard way to import the formatter functions in older Node.js environments. The package exports an object.
load, save
import { load, save } from 'mountebank-formatters';
import mountebankFormatters from 'mountebank-formatters';
ES Modules named import for environments supporting it. The package exports an object with named properties.
Formatter object
const formatter = require('mountebank-formatters'); const { load, save } = formatter;
import * as formatter from 'mountebank-formatters';
While not directly used in the README examples, some might prefer to import the entire formatter object and then destructure or access its properties. The `import * as` syntax for ESM is less common for this specific package's structure.

This quickstart demonstrates how to use the `load` and `save` functions from `mountebank-formatters` to persist and retrieve a sample Mountebank configuration to/from a file, mimicking the `mb save` and `mb start --configfile` operations.

import { load, save } from 'mountebank-formatters'; import fs from 'fs/promises'; import path from 'path'; async function runFormatterExample() { const configFilePath = path.join(process.cwd(), 'mb-config.json'); const tempConfig = { port: 2525, stubs: [{ responses: [{ is: { statusCode: 200, body: 'Hello, mountebank!' } }] }], predicates: [] }; console.log('1. Saving temporary Mountebank config...'); await save(configFilePath, tempConfig); // The actual save function expects a callback or returns a promise implicitly console.log(`Config saved to ${configFilePath}`); // To make it runnable for real, mock the fs.writeFileSync/readFileSync or use a real file. // The actual 'save' in mountebank-formatters uses fs.writeFileSync internally // and 'load' uses fs.readFileSync, which return the content directly, not a Promise. // This example assumes they are made async-compatible or wrapped. // Re-read for demonstration purposes const fileContent = await fs.readFile(configFilePath, 'utf8'); const loadedConfig = load(configFilePath); // In reality, 'load' reads the file synchronously console.log('2. Loading config from file...'); console.log('Loaded config (simplified view):', loadedConfig.port, loadedConfig.stubs[0].responses[0].is.body); // Clean up await fs.unlink(configFilePath); console.log(`Cleaned up ${configFilePath}`); } // Acknowledging the actual synchronous nature of the formatter's load/save // In a real scenario, you'd wrap them or use their synchronous versions directly. // For quickstart, we'll simulate the asynchronous file operations. const actualFormatter = require('mountebank-formatters'); const originalSave = actualFormatter.save; const originalLoad = actualFormatter.load; // Mocking 'save' to be async for the quickstart example actualFormatter.save = async (filename, config) => { await fs.writeFile(filename, JSON.stringify(config, null, 2)); return config; }; // Mocking 'load' to be async for the quickstart example actualFormatter.load = async (filename) => { const content = await fs.readFile(filename, 'utf8'); return JSON.parse(content); }; runFormatterExample().catch(console.error);
Debug
Known issues
gotchaThis package primarily serves as the *default* formatter for Mountebank. While it exposes `load` and `save` functions, its direct consumption is usually internal to Mountebank CLI operations. When defining custom formatters for Mountebank, you would typically implement your own `load` and `save` logic rather than directly extending or calling these functions.
fix
Understand that for custom Mountebank formatters, you define your own `load` and `save` functions, which mountebank-formatters can serve as a reference for, but not necessarily a direct dependency.
affects: >=0.0.1
breakingThe existence of this package stems from a breaking change in EJS (an internal dependency) that Mountebank core could not adopt directly without breaking existing user configurations. While this package provides backward compatibility, users relying on specific EJS templating features that changed significantly in EJS v3+ might encounter subtle differences if Mountebank were to upgrade EJS and if their configurations depend on deprecated EJS behaviors that this formatter still supports for compatibility.
fix
For Mountebank users: ensure your Mountebank configuration files are compatible with the version of EJS used by the `mountebank-formatters` package, especially if you rely on advanced templating features. Consider using custom formatters if you need strict control over templating engines.
affects: N/A (Historical context for Mountebank prior to this package)
Errors
Common errors & fixes
TypeError: require(...) is not a function
Attempting to use `require('mountebank-formatters')` directly as a function or assuming it exports a default function, rather than an object with `load` and `save` properties.
fix
Use destructuring for CommonJS: `const { load, save } = require('mountebank-formatters');`
Error: Cannot find module 'mountebank-formatters'
The package is not installed or the Node.js module resolution path is incorrect.
fix
Ensure the package is installed: `npm install mountebank-formatters` or `yarn add mountebank-formatters`.
Upgrade
Version history
0.0.2latest on npm
Audit
Dependencies
lodashrequiredUsed internally for utility functions.
Agent activity
6 hits · last 30 days
node
6
Resources
mountebank-formatters — npm install mountebank-formatters · libregistry