{"id":14614,"library":"happner-test-modules","title":"Happner Test Modules","description":"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.","status":"maintenance","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/happner/happner-test-modules","tags":["javascript"],"install":[{"cmd":"npm install happner-test-modules","lang":"bash","label":"npm"},{"cmd":"yarn add happner-test-modules","lang":"bash","label":"yarn"},{"cmd":"pnpm add happner-test-modules","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This package provides test utilities specifically for the `happner` microservices framework, requiring `happner` to define and interact with mesh components.","package":"happner","optional":false}],"imports":[{"note":"While `happner` ecosystem often uses CommonJS, modern testing often prefers ESM imports. Both patterns might be seen depending on the project setup.","wrong":"const createTestMesh = require('happner-test-modules').createTestMesh;","symbol":"createTestMesh","correct":"import { createTestMesh } from 'happner-test-modules';"},{"note":"Assumes a named export for a common test component or helper. Direct path imports might bypass necessary module resolution if not careful.","wrong":"import TestComponent from 'happner-test-modules/lib/test-component';","symbol":"TestComponent","correct":"import { TestComponent } from 'happner-test-modules';"},{"note":"Although `happner-test-modules` is the subject, `happner` itself is often imported in test files. CommonJS `require` is typical for older `happner` projects, but ESM star import is used here for modern context.","wrong":"const Happner = require('happner');","symbol":"Happner","correct":"import * as Happner from 'happner';"}],"quickstart":{"code":"import { createTestMesh } from 'happner-test-modules';\nimport Happner from 'happner'; // Assuming Happner is a default export for simplicity, or named import can be used\n\nasync function runHappnerTest() {\n  const config = {\n    name: 'testMesh',\n    modules: {\n      TestComponent: {\n        instance: {\n          testMethod: async (message) => {\n            console.log(`TestComponent received: ${message}`);\n            return `Processed: ${message}`;\n          }\n        }\n      }\n    },\n    components: {\n      TestComponent: {}\n    }\n  };\n\n  let mesh;\n  try {\n    // Use happner-test-modules to create a test mesh\n    mesh = await Happner.create(config);\n    console.log('Happner test mesh started.');\n\n    const result = await mesh.exchange.TestComponent.testMethod('Hello from test!');\n    console.log(`Result from TestComponent: ${result}`);\n\n    // Example of another test action\n    const events = [];\n    mesh.event.TestComponent.on('test-event', (data) => {\n      events.push(data);\n      console.log('Event received:', data);\n    });\n    await mesh.emit('TestComponent/test-event', { value: 'eventData' });\n\n    // Wait a moment for events to propagate (in real tests, use promises/await for specific events)\n    await new Promise(resolve => setTimeout(resolve, 100));\n    console.log('Events collected:', events);\n\n  } catch (e) {\n    console.error('Error during test:', e);\n    process.exit(1);\n  } finally {\n    if (mesh) {\n      await mesh.stop();\n      console.log('Happner test mesh stopped.');\n    }\n  }\n}\n\nrunHappnerTest();","lang":"typescript","description":"This quickstart demonstrates how to create and interact with a basic `happner` mesh using `happner-test-modules` (implicitly through `happner.create`), define a simple component, call a method on it, and emit/receive an event."},"warnings":[{"fix":"Consult the `happner` project's release notes and migration guides (e.g., from `happner-1` to `happner-2`) for specific changes. Update `happner-test-modules` to the latest compatible version.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure each test creates a fresh `happner` mesh instance and cleans it up (e.g., `mesh.stop()`) after completion. Avoid global state or ensure any shared resources are reset before each test run. Consider using a test runner's `beforeEach` and `afterEach` hooks.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Always ensure `mesh.stop()` is called in an `afterEach` or `finally` block for every mesh created in a test, even if the test fails. This guarantees clean resource release.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure that `await Happner.create(config)` has successfully completed before attempting any interactions with the mesh. Handle potential errors during mesh creation.","cause":"Attempting to access `mesh.exchange` or `mesh.event` before the `happner` mesh has fully initialized or if `Happner.create` failed.","error":"Error: Mesh not started"},{"fix":"Verify the `Happner.create(config)` call and its promise resolution. Debug the `mesh` variable to confirm it holds a valid `happner` instance after creation.","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.","error":"TypeError: Cannot read property 'exchange' of undefined"},{"fix":"Check 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.","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.","error":"SyntaxError: Unexpected token 'export' (or 'import')"}],"ecosystem":"npm"}