CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-umi-test

A Jest-based testing utility package for Umi.js applications that provides pre-configured test framework setup and transformers.

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

umi-test

umi-test is a Jest-based testing utility library specifically designed for Umi.js applications. It provides pre-configured Jest settings with custom transformers for JavaScript and TypeScript, automatic test file discovery, CSS/LESS module mocking, and integrated setup files for React component testing with Enzyme.

Package Information

  • Package Name: umi-test
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install umi-test

Core Imports

const test = require("umi-test").default;

For ES modules:

import test from "umi-test";

Basic Usage

const test = require("umi-test").default;

// Run tests with default configuration
test().then(() => {
  console.log('Tests passed');
}).catch(e => {
  console.error('Tests failed:', e);
});

// Run tests with custom options
test({
  watch: true,
  coverage: true,
  cwd: '/path/to/project',
  moduleNameMapper: {
    '^@/(.*)$': '<rootDir>/src/$1'
  }
});

CLI usage:

# Run tests once
umi-test

# Run in watch mode
umi-test --watch

# Run with coverage
umi-test --coverage

Architecture

umi-test is built around several key components:

  • Main Test Function: Core test runner that configures and executes Jest with Umi-specific settings
  • CLI Binary: Command-line interface for easy test execution
  • Custom Transformers: Babel and TypeScript transformers optimized for Umi.js projects
  • Setup Files: Pre-configured environment setup for JSDOM, Enzyme, and polyfills
  • Module Mocking: Automatic CSS/LESS module mocking to prevent import errors

Capabilities

Main Test Function

Core test runner function that configures and runs Jest with Umi-specific settings including transformers, setup files, and module resolution.

/**
 * Main test runner function for Umi.js applications
 * @param {Object} [opts={}] - Configuration options for Jest and test execution
 * @param {string} [opts.cwd] - Current working directory (defaults to process.cwd())
 * @param {Object} [opts.moduleNameMapper] - Additional module name mappings for Jest
 * @param {boolean} [opts.watch] - Enable watch mode
 * @param {boolean} [opts.coverage] - Enable coverage reporting
 * @returns {Promise<void>} Promise that resolves on successful test completion or rejects on failure
 */
function test(opts = {});

Usage Examples:

const test = require("umi-test").default;

// Basic usage
await test();

// With custom configuration
await test({
  cwd: '/path/to/project',
  watch: true,
  coverage: true,
  moduleNameMapper: {
    '^@components/(.*)$': '<rootDir>/src/components/$1',
    '^@utils/(.*)$': '<rootDir>/src/utils/$1'
  }
});

CLI Binary

Command-line interface for running tests with common options.

# Usage: umi-test [options]
# Options:
#   -w, --watch     Enable watch mode
#   --coverage      Enable coverage reporting
#   Additional Jest CLI options are passed through to Jest

Usage Examples:

# Run tests once
umi-test

# Run in watch mode
umi-test --watch

# Run with coverage
umi-test --coverage

# Combine options
umi-test --watch --coverage

Transformers

Custom transformers for processing JavaScript, TypeScript, and JSX files.

// JavaScript/JSX Transformer (babel-jest with babel-preset-umi)
const jsTransformer;

// TypeScript/TSX Transformer (ts-jest)  
const tsTransformer;

// JS Transformer Configuration:
// - Uses babel-preset-umi with transformRuntime: false
// - Includes babel-plugin-module-resolver with aliases:
//   - ts-jest, react, react-dom, enzyme

Built-in Configuration

The test function provides comprehensive Jest configuration:

Transform Configuration

  • JavaScript/JSX files: Uses babel-jest with babel-preset-umi (transformRuntime: false) and babel-plugin-module-resolver for aliases
  • TypeScript/TSX files: Uses ts-jest with useBabelrc: true
  • CSS/LESS/SASS/SCSS files: Mocked with identity-obj-proxy

Test Discovery

  • Pattern: **/?(*.)(spec|test|e2e).(j|t)s?(x)
  • Extensions: js, jsx, ts, tsx, json

Module Resolution

  • Provides aliases for common testing libraries (react, react-dom, enzyme, ts-jest)
  • CSS modules are automatically mocked to prevent import errors
  • Supports custom moduleNameMapper via options

Environment Setup

  • Sets NODE_ENV to 'test'
  • Configures JSDOM with basic HTML structure including root div
  • Sets up Enzyme with React 16 adapter
  • Provides babel polyfill and requestAnimationFrame shim
  • Sets jasmine timeout to 20 seconds (20000ms)

Setup Files

// Setup files loaded before tests:
// 1. require.resolve('./shim.js') - Babel polyfill and requestAnimationFrame shim
// 2. require.resolve('./setupTests.js') - JSDOM and Enzyme configuration

// Test framework setup:
// setupTestFrameworkScriptFile: require.resolve('./jasmine.js') - Sets jasmine timeout to 20000ms

// Module name mapping:
// CSS/LESS/SASS/SCSS files are mocked with identity-obj-proxy

Custom Configuration

Users can provide additional Jest configuration by creating a jest.config.js file in their project root:

// jest.config.js
module.exports = {
  // Custom configuration that will be merged with default umi-test config
  testTimeout: 30000,
  collectCoverageFrom: [
    'src/**/*.{js,jsx,ts,tsx}',
    '!src/**/*.d.ts',
  ],
  // Additional Jest options...
};

Error Handling

The test function returns a Promise that:

  • Resolves when all tests pass successfully
  • Rejects with an Error('Jest failed') when tests fail
  • Logs errors to console for debugging

Configuration Details

// Jest Configuration Generated:
{
  rootDir: process.cwd(),
  setupFiles: [
    require.resolve('./shim.js'),
    require.resolve('./setupTests.js')
  ],
  transform: {
    '\\.jsx?$': require.resolve('./transformers/jsTransformer'),
    '\\.tsx?$': require.resolve('./transformers/tsTransformer')
  },
  testMatch: ['**/?(*.)(spec|test|e2e).(j|t)s?(x)'],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json'],
  setupTestFrameworkScriptFile: require.resolve('./jasmine'),
  moduleNameMapper: {
    '\\.(css|less|sass|scss)$': require.resolve('identity-obj-proxy'),
    // Additional mappings from opts.moduleNameMapper
  },
  globals: {
    'ts-jest': {
      useBabelrc: true
    }
  }
  // User jest.config.js is merged if present
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/umi-test@1.0.x
Publish Source
CLI
Badge
tessl/npm-umi-test badge