Testing utilities for Umi applications providing temporary test environments, HTML generation, React component rendering, and HTML content retrieval
npx @tessl/cli install tessl/npm-umijs--test-utils@3.5.0@umijs/test-utils provides testing utilities for Umi applications, enabling developers to create controlled testing environments with temporary file generation, HTML output, React component rendering, and content retrieval. It integrates seamlessly with Umi's service architecture and React Testing Library.
npm install @umijs/test-utils@testing-library/react ^9.4.0, umi ^3.0.0import { generateTmp, generateHTML, render, getHTML } from "@umijs/test-utils";For CommonJS:
const { generateTmp, generateHTML, render, getHTML } = require("@umijs/test-utils");import { generateTmp, render, getHTML } from "@umijs/test-utils";
import { Service } from "@umijs/core";
// Generate temporary test environment
await generateTmp({
cwd: "/path/to/project",
Service,
});
// Render the generated app with React Testing Library
const { getByText } = render({ cwd: "/path/to/project" });
// Get HTML output for assertions
const htmlContent = getHTML({ cwd: "/path/to/project" });@umijs/test-utils is built around the following key components:
.umi-test directories with all necessary Umi configuration and filesCreates temporary Umi test environments with all necessary configuration files and dependencies.
/**
* Generates temporary Umi test environment in .umi-test directory
* @param opts - Configuration options for test environment generation
* @returns Promise that resolves when generation is complete
*/
function generateTmp(opts: {
cwd: string;
Service?: typeof UmiService;
}): Promise<void>;Generates HTML output files for testing and integration scenarios.
/**
* Generates HTML output for testing in dist directory
* @param opts - Configuration options for HTML generation
* @returns Promise that resolves when HTML generation is complete
*/
function generateHTML(opts: {
cwd: string;
Service?: typeof UmiService;
}): Promise<void>;Renders Umi applications using React Testing Library for component testing.
/**
* Renders the generated Umi app using React Testing Library
* @param opts - Configuration options for rendering
* @returns RenderResult from React Testing Library for querying and interaction
*/
function render(opts: { cwd: string }): RenderResult;Retrieves generated HTML content as a string for assertions and validation.
/**
* Retrieves generated HTML content from dist/index.html
* @param opts - Configuration options for HTML retrieval
* @returns HTML content as a string
*/
function getHTML(opts: { cwd: string }): string;import { Service as UmiService } from 'umi';
import { RenderResult } from '@testing-library/react';
interface GenerationOptions {
/** Current working directory path for the test project */
cwd: string;
/** Optional Umi Service class override for custom service instances */
Service?: typeof UmiService;
}
interface RenderOptions {
/** Current working directory path containing the generated .umi-test files */
cwd: string;
}
interface HTMLOptions {
/** Current working directory path containing the generated dist files */
cwd: string;
}import { generateTmp, generateHTML, render, getHTML } from "@umijs/test-utils";
import { Service } from "@umijs/core";
// Setup test environment
const testCwd = "/tmp/test-project";
// Generate temporary test files
await generateTmp({
cwd: testCwd,
Service,
});
// Render app for component testing
const { getByText, container } = render({ cwd: testCwd });
// Assert component rendering
expect(getByText("Welcome")).toBeInTheDocument();
// Generate HTML for integration testing
await generateHTML({
cwd: testCwd,
Service,
});
// Get HTML content for validation
const htmlContent = getHTML({ cwd: testCwd });
expect(htmlContent).toContain("<title>My App</title>");import { generateTmp } from "@umijs/test-utils";
import { Service } from "umi";
// Create custom service configuration
class CustomTestService extends Service {
constructor(opts) {
super({
...opts,
// Custom test-specific configuration
});
}
}
// Use custom service for test generation
await generateTmp({
cwd: "/path/to/project",
Service: CustomTestService,
});Functions may throw errors in the following scenarios:
cwd path doesn't exist or lacks proper permissionsdist/index.html file doesn't exist or can't be readThe package generates the following directory structure during testing:
project-root/
├── .umi-test/ # Temporary test environment
│ ├── umi.ts # Main Umi app entry point
│ ├── core/ # Core Umi files
│ └── ... # Other generated test files
└── dist/ # HTML output directory
└── index.html # Generated HTML file