A comprehensive set of build tools and configurations for LoopBack 4 and TypeScript projects providing CLI commands for compilation, linting, testing, and coverage
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Mocha test runner with console log detection, configuration merging, and enhanced error reporting for LoopBack projects.
Runs Mocha tests with automatic configuration discovery and console log detection.
/**
* Mocha test runner function with enhanced features
* @param argv - Command line arguments including test files and options
* @param options - Execution options for dry run and process control
* @returns ChildProcess when executed, string when dry run
*/
function mocha(argv: string[], options?: RunOptions): ChildProcess | string;CLI Usage:
# Run all tests
lb-mocha
# Run specific test files
lb-mocha "dist/__tests__/**/*.js"
# Run tests with custom timeout
lb-mocha --timeout 5000 "dist/__tests__"
# Allow console logs in tests
lb-mocha --allow-console-logs "dist/__tests__"
# Set language for tests
lb-mocha --lang en_US.UTF-8 "dist/__tests__"
# Run with custom configuration
lb-mocha --config .mocharc.custom.jsonProgrammatic Usage:
import { mocha } from "@loopback/build";
// Run tests with default settings
const child = mocha(["dist/__tests__"]);
// Dry run to see command
const command = mocha(["--timeout", "10000", "dist/__tests__"], { dryRun: true });
console.log(command); // Shows the Mocha command that would be executed
// Run tests in custom directory
mocha(["test/"], { cwd: "/path/to/project" });Automatically discovers Mocha configuration files in the project.
Configuration Search Order:
.mocharc.js in project root.mocharc.json in project root.mocharc.yaml in project root.mocharc.yml in project root@loopback/build/config/.mocharc.jsonDefault Configuration:
// @loopback/build/config/.mocharc.json contents:
interface MochaConfig {
require: ["source-map-support/register"];
recursive: true;
exit: true;
reporter: "dot";
}Advanced feature that detects and fails tests when unexpected console output occurs.
/**
* Console log detection hooks for Mocha
* Automatically injected unless --allow-console-logs is specified
*/
interface MochaHooks {
beforeAll: () => void; // Start recording console logs
beforeEach: () => void; // Save current test context
afterEach: () => void; // Check for test failures
afterAll: () => void; // Report console log problems
}Features:
console.log, console.error, and console.warnDisabling Console Log Detection:
# Allow console logs in tests
lb-mocha --allow-console-logs "dist/__tests__"Utility for merging multiple Mocha configuration objects.
/**
* Merge multiple Mocha configuration objects with smart handling
* @param configs - Array of Mocha configuration objects
* @returns Merged configuration with combined require arrays and max timeout
*/
function mergeMochaConfigs(...configs: MochaConfig[]): MochaConfig;
interface MochaConfig {
timeout?: number; // Test timeout in milliseconds
require?: string | string[]; // Files to require before tests
reporter?: string; // Test reporter to use
recursive?: boolean; // Search subdirectories for tests
exit?: boolean; // Force exit after tests complete
[key: string]: any; // Additional Mocha options
}Merging Behavior:
timeout: Uses maximum value from all configsrequire: Combines all require arrays into single arrayUsage Example:
import { mergeMochaConfigs } from "@loopback/build";
const baseConfig = {
timeout: 2000,
require: ["source-map-support/register"],
reporter: "spec"
};
const projectConfig = {
timeout: 5000,
require: ["./test/setup.js"],
recursive: true
};
const merged = mergeMochaConfigs(baseConfig, projectConfig);
// Result:
// {
// timeout: 5000, // Max of 2000 and 5000
// require: ["source-map-support/register", "./test/setup.js"],
// reporter: "spec",
// recursive: true
// }Supports custom language settings and environment variables.
// --lang option sets LANG environment variable
// Useful for locale-specific test behaviorUsage:
# Set language for tests
lb-mocha --lang en_US.UTF-8 "dist/__tests__"
# This sets process.env.LANG = "en_US.UTF-8" before running testsAutomatically suppresses Node.js warnings during test execution.
// Automatically adds --no-warnings flag to Node.js
// Prevents deprecation warnings from cluttering test output
// Only applied when console log detection is enabledAll Mocha command line options are supported and passed through.
interface MochaOptions {
"--config": string; // Configuration file path
"--package": string; // package.json file path
"--no-config": boolean; // Ignore configuration files
"--timeout": number; // Test timeout in milliseconds
"--reporter": string; // Test reporter
"--grep": string; // Only run tests matching pattern
"--fgrep": string; // Only run tests containing string
"--invert": boolean; // Invert grep/fgrep matches
"--recursive": boolean; // Search subdirectories
"--exit": boolean; // Force exit after completion
"--bail": boolean; // Stop on first failure
"--slow": number; // Slow test threshold
"--require": string; // Require module before running
"--reporter-options": string; // Reporter-specific options
"--parallel": boolean; // Run tests in parallel
"--jobs": number; // Number of parallel jobs
}Basic Test Execution:
import { mocha, mergeMochaConfigs } from "@loopback/build";
// Run tests with default configuration
mocha(["dist/__tests__"], { dryRun: false });
// Merge custom configuration
const config = mergeMochaConfigs(
{ timeout: 2000, reporter: "spec" },
{ timeout: 5000, require: ["./test/setup.js"] }
);Package.json Integration:
{
"scripts": {
"test": "lb-mocha \"dist/__tests__\"",
"test:watch": "lb-mocha --watch \"dist/__tests__\"",
"test:debug": "lb-mocha --allow-console-logs --timeout 0 \"dist/__tests__\"",
"test:coverage": "lb-nyc lb-mocha \"dist/__tests__\""
}
}Custom Configuration Files:
// .mocharc.js
module.exports = {
timeout: 10000,
require: ["source-map-support/register", "./test/setup.js"],
reporter: "spec",
recursive: true,
exit: true
};Enhanced error handling with detailed console log reporting.
// Console log detection provides detailed error messages
// Stack traces show exact location of console.log calls
// Test failures are properly reported with context
// Process exits with appropriate error codesConsole Log Error Format:
=== ATTENTION - INVALID USAGE OF CONSOLE LOGS DETECTED ===
Learn more at https://github.com/loopbackio/loopback-next/blob/master/packages/build/README.md#a-note-on-console-logs-printed-by-tests
Unexpected console.log() call
at MyClass.method (/path/to/file.js:123:45)
at Test.myTest (/path/to/test.js:67:89)