Happner Test Modules
happner-test-modules is a supplementary library designed to provide utilities and mock components specifically for testing applications built with the `happner` microservices framework. `happner` is a cloud application framework that enables the creation of an interconnected mesh of local and remote components, utilizing `happn-3` for its pub/sub and data layer. This package (currently at version `1.0.2`) offers helper functions to simplify the setup and teardown of `happner` meshes, the loading of test components, and the simulation of component interactions and event flows. These functionalities are crucial for ensuring the reliability of `happner`-based systems. Given that `happner`'s last significant updates (v1.34.1 and `happner-2`) were several years ago, `happner-test-modules` is likely in maintenance mode, receiving updates primarily for compatibility or critical bug fixes rather than active feature development. Its key differentiator is its tailored integration with the `happner` ecosystem, providing specific test paradigms for mesh components and event APIs.
Common errors
-
Error: Mesh not started
cause Attempting to access `mesh.exchange` or `mesh.event` before the `happner` mesh has fully initialized or if `Happner.create` failed.fixEnsure that `await Happner.create(config)` has successfully completed before attempting any interactions with the mesh. Handle potential errors during mesh creation. -
TypeError: Cannot read property 'exchange' of undefined
cause This usually indicates that the `mesh` object itself is undefined or null, meaning the `Happner.create` call likely failed or returned an unexpected value, or the `mesh` variable was not correctly assigned.fixVerify the `Happner.create(config)` call and its promise resolution. Debug the `mesh` variable to confirm it holds a valid `happner` instance after creation. -
SyntaxError: Unexpected token 'export' (or 'import')
cause This error occurs when a CommonJS (CJS) environment tries to parse an ECMAScript Module (ESM) syntax (`import`/`export`). Given the age of `happner`, many projects are still CJS-based while modern libraries or test setups might use ESM.fixCheck your `tsconfig.json` (for TypeScript) or Node.js `package.json`'s `type` field and `module` resolution settings. Ensure your test runner (e.g., Jest, Mocha) is configured to handle ESM, or transpile your code to CJS if the environment strictly requires it. For Node.js, ensure `.mjs` or `type: "module"` is used for ESM files, and `.cjs` or `type: "commonjs"` for CJS.
Warnings
- breaking Major version updates of `happner` itself may introduce breaking changes that require corresponding updates or adjustments in `happner-test-modules` usage or internal implementation. Always check `happner`'s migration guides for relevant changes.
- gotcha Tests involving `happner` meshes, especially those with multiple components or instances, can suffer from shared state issues if not properly isolated. This can lead to flaky tests that pass individually but fail when run in a suite.
- gotcha Improper shutdown of `happner` mesh instances in tests can lead to resource leaks (e.g., open ports, database connections) or interference with subsequent tests. This is particularly relevant in CI/CD environments.
Install
-
npm install happner-test-modules -
yarn add happner-test-modules -
pnpm add happner-test-modules
Imports
- createTestMesh
const createTestMesh = require('happner-test-modules').createTestMesh;import { createTestMesh } from 'happner-test-modules'; - TestComponent
import TestComponent from 'happner-test-modules/lib/test-component';
import { TestComponent } from 'happner-test-modules'; - Happner
const Happner = require('happner');import * as Happner from 'happner';
Quickstart
import { createTestMesh } from 'happner-test-modules';
import Happner from 'happner'; // Assuming Happner is a default export for simplicity, or named import can be used
async function runHappnerTest() {
const config = {
name: 'testMesh',
modules: {
TestComponent: {
instance: {
testMethod: async (message) => {
console.log(`TestComponent received: ${message}`);
return `Processed: ${message}`;
}
}
}
},
components: {
TestComponent: {}
}
};
let mesh;
try {
// Use happner-test-modules to create a test mesh
mesh = await Happner.create(config);
console.log('Happner test mesh started.');
const result = await mesh.exchange.TestComponent.testMethod('Hello from test!');
console.log(`Result from TestComponent: ${result}`);
// Example of another test action
const events = [];
mesh.event.TestComponent.on('test-event', (data) => {
events.push(data);
console.log('Event received:', data);
});
await mesh.emit('TestComponent/test-event', { value: 'eventData' });
// Wait a moment for events to propagate (in real tests, use promises/await for specific events)
await new Promise(resolve => setTimeout(resolve, 100));
console.log('Events collected:', events);
} catch (e) {
console.error('Error during test:', e);
process.exit(1);
} finally {
if (mesh) {
await mesh.stop();
console.log('Happner test mesh stopped.');
}
}
}
runHappnerTest();