Mocha Test Helper Suite
raw JSON →node-test-helper is a legacy testing utility suite designed to simplify Mocha test setup by providing globally available assertions, mocking utilities, and test structure variables. Currently at version 1.0.0, the package's development appears to be abandoned, with no significant code updates in the past few years, despite a recent npm publish. Its approach relies heavily on CommonJS `require()` to inject global variables and functions (like `describe`, `it`, `expect`, `stub`, `TEST_NAME`) into the test environment, diverging significantly from modern modular JavaScript testing practices. It also presumes a `make`-based workflow for test execution rather than `npm scripts`, limiting its direct applicability in contemporary Node.js projects. Key differentiators (at the time of its creation) included simplifying boilerplate by abstracting away direct `require` calls for Mocha, Chai, and Sinon in individual test files, and offering an `init` command to scaffold a basic test directory structure and Makefile.
Common errors
error ReferenceError: describe is not defined ↓
require("node-test-helper"); is at the very top of your test file, or in a bootstrap file loaded before your tests. error make: *** No rule to make target 'test'. Stop. ↓
node-test-helper init to generate the default Makefile and test directory structure, or ensure you are executing make test from the directory containing your Makefile. error Error: Cannot find module 'node-test-helper' ↓
npm install node-test-helper --save-dev. error ReferenceError: expect is not defined ↓
npm install chai --save-dev. Warnings
breaking The package exclusively uses CommonJS `require()` for initialization and pollutes the global scope. It is not compatible with ES Modules (`import`/`export`) without significant configuration or a transpilation step, and attempting to use `import` will lead to `ReferenceError` or incorrect setup. ↓
gotcha This library relies on injecting test utilities (like `describe`, `it`, `expect`, `stub`) as global variables. This practice is generally considered an anti-pattern in modern JavaScript development due to potential conflicts, reduced code clarity, and difficulty in static analysis. ↓
deprecated Test execution is heavily coupled to `make` commands and a specific `Makefile` structure. Modern Node.js projects typically use `npm scripts` for test orchestration, offering greater portability and platform independence. ↓
gotcha The package depends on `mocha`, `chai`, `sinon`, and `sinon-chai` being installed in your project. If these dependencies are missing, global functions like `describe` or `expect` will not be available, leading to `ReferenceError`s. ↓
Install
npm install node-test-helper yarn add node-test-helper pnpm add node-test-helper Imports
- Side Effect Global Setup wrong
import 'node-test-helper';correctrequire("node-test-helper"); - expect wrong
import { expect } from 'node-test-helper';correct// Available globally after require("node-test-helper"); - TEST_NAME wrong
import { TEST_NAME } from 'node-test-helper';correct// Available globally after require("node-test-helper");
Quickstart
const fs = require('fs');
const path = require('path');
// Create a mock package.json to avoid 'make' install issues in a real quickstart
fs.writeFileSync('package.json', JSON.stringify({ name: 'my-test-app', version: '1.0.0', devDependencies: { 'node-test-helper': '^1.0.0', 'mocha': '^10.0.0', 'chai': '^4.0.0', 'sinon': '^17.0.0', 'sinon-chai': '^3.0.0' } }), 'utf8');
// Simulate npm install for quickstart context
console.log('Simulating npm install...');
// In a real environment, you'd run `npm install` here.
// For a quickstart, we're assuming dependencies are met if we just show the test file.
// Create a Makefile for 'make test'
const makefileContent = `
TEST_RUNNER = ./node_modules/.bin/mocha
TEST_OPTS = --recursive -t 30000 -R spec
test:
@NODE_ENV=test $(TEST_RUNNER) $(TEST_OPTS) test/unit/
.PHONY: test
`;
fs.writeFileSync('Makefile', makefileContent, 'utf8');
// Create test directory structure
fs.mkdirSync('test/unit', { recursive: true });
// Create a sample test file
const testContent = `
//-- test/unit/sample.test.js
require("node-test-helper");
describe(TEST_NAME, function() {
describe("without callback", function() {
it("should be successful", function() {
expect(true).to.be.true;
});
});
describe("with callback", function() {
it("should be successful asynchronously", function(done) {
setTimeout(() => {
expect(false).to.be.false;
done();
}, 10);
});
});
});
`;
fs.writeFileSync('test/unit/sample.test.js', testContent, 'utf8');
console.log("To run the tests, first ensure mocha, chai, sinon, and sinon-chai are installed locally (e.g., 'npm install'). Then run: `make test`");
console.log("Expected output will show '2 passing'.");