Registry / testing / mocha-test-container-support

mocha-test-container-support

JSON →
library0.2.0jsnpmunverified

mocha-test-container-support is a Mocha plugin designed to provide visual debugging support for test cases by injecting a unique `div` container into the DOM for each test. This allows developers to visually inspect the DOM changes and rendering produced by their test code directly in the browser during test-driven development (TDD). The plugin is currently at version 0.2.0, with its last update several years ago, indicating it is no longer actively maintained. A key differentiator is its focus on visual feedback, although it explicitly states it offers no 'real' encapsulation, meaning CSS, JavaScript, and DOM changes can leak between tests, potentially causing unintended side effects. It differs from isolated testing environments (like `<iframe>` or Shadow DOM) by rendering content directly into the main DOM. Its release cadence was slow, and it appears to be abandoned.

npm install mocha-test-container-support
INSTALL
IMPORT
SIG · MOCHA-TEST-CONTAIN
M
mocha-test-container-support
testingjavascriptv0.2.0
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.

TestContainer
const TestContainer = require('mocha-test-container-support');
import TestContainer from 'mocha-test-container-support';
This package exclusively uses CommonJS `require()` syntax as it was last updated before broad Node.js ESM adoption. Attempting to use ES module `import` will result in a `ReferenceError`.

This quickstart demonstrates how to import and use `mocha-test-container-support` within Mocha's `beforeEach` hook to obtain a dedicated DOM container for each test, allowing for visual inspection and manipulation of test-specific DOM content.

const assert = require('assert'); const TestContainer = require('mocha-test-container-support'); describe('My Feature', function() { let testContentContainer; beforeEach(function() { // Get a new container for each test testContentContainer = TestContainer.get(this); // Clear previous content if any, though TestContainer.get typically returns a fresh one testContentContainer.innerHTML = ''; }); it('should append a div to the test container', function() { const myDiv = document.createElement('div'); myDiv.textContent = 'Hello from test!'; testContentContainer.appendChild(myDiv); assert.strictEqual(testContentContainer.querySelector('div').textContent, 'Hello from test!'); // You could visually verify this in a browser-based Mocha runner }); it('should apply a style to the test container', function() { testContentContainer.style.backgroundColor = 'blue'; assert.strictEqual(testContentContainer.style.backgroundColor, 'blue'); // Caution: styles can leak to other tests if not reset or isolated (which this lib doesn't do fully) }); // Example of a test where DOM changes could be visually inspected it('should render a list of items', function() { const ul = document.createElement('ul'); ['Item 1', 'Item 2'].forEach(text => { const li = document.createElement('li'); li.textContent = text; ul.appendChild(li); }); testContentContainer.appendChild(ul); assert.strictEqual(testContentContainer.querySelectorAll('li').length, 2); }); });
Debug
Known issues
breakingThe project is archived and no longer actively maintained by its owner (archived May 6, 2020). This means there will be no further bug fixes, security updates, or feature enhancements.
fix
Consider migrating to more actively maintained testing solutions or implementing custom DOM handling within Mocha hooks if visual debugging is critical.
affects: >=0.2.0
gotchaThis plugin provides no 'real' encapsulation. CSS, JavaScript, and DOM changes made within one test's container can still leak and affect other tests, leading to non-deterministic or flaky test results. The containers are simple `div` elements, not `<iframe>` or Shadow DOM.
fix
Manually ensure that all DOM modifications and style changes are fully reset or cleaned up in `afterEach` hooks, or ensure tests are completely isolated in their effects on the global DOM/CSS. For true isolation, explore alternatives like `jsdom` environments per test or browser-based testing tools with stronger sandboxing.
affects: >=0.0.1
gotchaThe package exclusively uses CommonJS `require()` syntax. It does not support ES modules (`import`) directly, which is common in modern JavaScript projects. Attempting to `import` will result in a runtime error.
fix
Ensure your test files are run in a CommonJS context (e.g., using `.js` extension without `"type": "module"` in `package.json`). If you must use ESM, you might need a CommonJS wrapper or a build step, though this adds complexity to an unmaintained library.
affects: >=0.0.1
gotchaThe current version 0.2.0 is still an early development release, indicating potential instability, unaddressed edge cases, or incomplete feature sets. The API might have breaking changes in future minor versions if development were to resume.
fix
Review the source code carefully for your specific use case. Be prepared for unexpected behavior and thorough testing.
affects: >=0.0.1
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to import `mocha-test-container-support` using ES module `import` syntax in an environment that treats the file as an ES module.
fix
Change your import statement to `const TestContainer = require('mocha-test-container-support');` and ensure the file is interpreted as CommonJS (e.g., by not using `.mjs` or `"type": "module"` in `package.json`).
TypeError: TestContainer.get is not a function
The `this` context passed to `TestContainer.get()` inside Mocha hooks might not be the correct Mocha context object, or `TestContainer` itself was not correctly imported.
fix
Ensure `TestContainer.get(this)` is called directly within a Mocha `beforeEach` or `it` hook callback that receives the correct `this` context. Verify `TestContainer` is correctly `require()`d at the top of your test file. If using arrow functions for hooks, `this` will be lexically scoped, often `window` or `undefined`, preventing access to Mocha's context. Use traditional `function () { ... }` syntax for Mocha hooks.
Unexpected test failures due to leaked styles or DOM changes
The plugin provides limited test isolation. CSS rules, global JavaScript variables, or uncleaned DOM manipulations from one test persist and affect subsequent tests, leading to non-deterministic results.
fix
Implement explicit cleanup in `afterEach` hooks to reset any global styles, remove added DOM elements (beyond what `mocha-test-container-support` handles for its own container), or reset global state. Example: `afterEach(function() { document.body.style = ''; /* Reset global styles */ });`.
Upgrade
Version history
0.2.0latest on npm
Audit
Dependencies
mocharequiredRuntime peer dependency for the test framework it extends.
Agent activity
2 hits · last 30 days
node
2
Resources
mocha-test-container-support — npm install mocha-test-container-support · libregistry