CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-loopback--build

A comprehensive set of build tools and configurations for LoopBack 4 and TypeScript projects providing CLI commands for compilation, linting, testing, and coverage

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

test-execution.mddocs/

Test Execution

Mocha test runner with console log detection, configuration merging, and enhanced error reporting for LoopBack projects.

Capabilities

lb-mocha Command

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.json

Programmatic 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" });

Configuration Discovery

Automatically discovers Mocha configuration files in the project.

Configuration Search Order:

  1. .mocharc.js in project root
  2. .mocharc.json in project root
  3. .mocharc.yaml in project root
  4. .mocharc.yml in project root
  5. Falls back to @loopback/build/config/.mocharc.json

Default Configuration:

// @loopback/build/config/.mocharc.json contents:
interface MochaConfig {
  require: ["source-map-support/register"];
  recursive: true;
  exit: true;
  reporter: "dot";
}

Console Log Detection

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:

  • Intercepts console.log, console.error, and console.warn
  • Allows Mocha reporter console output
  • Ignores console logs from failed tests (for debugging)
  • Provides detailed stack traces for console log sources
  • Fails test suite if unexpected console logs are detected

Disabling Console Log Detection:

# Allow console logs in tests
lb-mocha --allow-console-logs "dist/__tests__"

Configuration Merging

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 configs
  • require: Combines all require arrays into single array
  • Other properties: Later configs override earlier ones

Usage 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
// }

Language and Environment Support

Supports custom language settings and environment variables.

// --lang option sets LANG environment variable
// Useful for locale-specific test behavior

Usage:

# Set language for tests
lb-mocha --lang en_US.UTF-8 "dist/__tests__"

# This sets process.env.LANG = "en_US.UTF-8" before running tests

Node.js Warning Suppression

Automatically 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 enabled

Command Line Option Passthrough

All 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
}

Usage Examples

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
};

Error Handling and Reporting

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 codes

Console 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)

docs

code-coverage.md

code-formatting.md

code-linting.md

file-cleanup.md

index.md

process-management.md

test-execution.md

typescript-compilation.md

tile.json