Frontend Test Framework
yhtml5-test is a front-end test framework designed to simplify unit testing in JavaScript projects, primarily by wrapping and pre-configuring Jest. It aims to reduce the boilerplate and configuration overhead typically associated with setting up a modern testing environment, abstracting away direct interactions with tools like Babel, Node, and Jest itself. The framework includes built-in polyfills, sophisticated file transformers for JavaScript, CSS, and other assets, and a pre-configured environment to simulate browser APIs. It supports testing React components through its integration with Enzyme and `react-test-renderer` (specifically v15.6.x and v2.9.x respectively, as shown in the quickstart). Key features include robust coverage reporting, snapshot testing capabilities, and an opinionated structure for test case organization. Currently at version 1.2.2, its release cadence is not explicitly stated but relies on updates to the underlying `@2dfire/2dfire-scripts` package. It differentiates itself by providing a batteries-included setup focused on immediate productivity for detecting bugs, identifying unused code, and ensuring code quality.
Common errors
-
ReferenceError: regeneratorRuntime is not defined
cause Modern JavaScript features like `async/await` require polyfills, which might be missing or incorrectly configured in the test environment.fixEnsure `regenerator-runtime` is installed (`npm i regenerator-runtime -D`) and imported in your Jest `setupFiles` (e.g., `import 'regenerator-runtime/runtime';`) or configure Babel to include `@babel/plugin-transform-runtime`. -
SyntaxError: Unexpected token 'export' (when running tests for a node_modules package)
cause A dependency within `node_modules` is using ES module syntax which Jest's default transformer, configured by `yhtml5-test`, is ignoring.fixModify the `transformIgnorePatterns` array in your `.config.js` to explicitly include the problematic `node_modules` package for Babel transformation. -
Cannot find module 'src/path/to/module'
cause Webpack aliases defined for the application are not being resolved correctly by Jest's module loader.fixVerify that your `moduleNameMapper` in `.config.js` accurately reflects your Webpack aliases, mapping them to the correct absolute paths. -
console.error: Warning: React.render is no longer supported in React 16+
cause Incompatible major versions of React, `react-test-renderer`, or `enzyme` are being used. The quickstart specifies older versions (React 15.x, Enzyme 2.x).fixUpdate `react-test-renderer` and `enzyme` to versions compatible with your project's React major version (e.g., `enzyme@3.x` with `enzyme-adapter-react-16` for React 16+), and ensure the adapter is configured in `setupTestFrameworkScriptFile`.
Warnings
- breaking The quickstart dependencies explicitly list older versions of `react-test-renderer@15.6.x` and `enzyme@2.9.x`. These versions are incompatible with React 16+ and will require significant updates.
- gotcha The framework's configuration (`.config.js`) must use CommonJS `module.exports` syntax. Attempting to use ES module `export default` will result in a configuration loading error.
- gotcha The `transformIgnorePatterns` in `.config.js` might prevent necessary transpilation for some `node_modules` packages, especially modern libraries published as ES modules, leading to `SyntaxError`s.
- gotcha Test files may not be discovered if their naming conventions or locations do not align with the default or custom `testMatch` patterns configured in `.config.js`.
- gotcha This package is a wrapper for `@2dfire/2dfire-scripts`. Direct `npm install yhtml5-test` does not install the primary CLI executable. Users must install `@2dfire/2dfire-scripts` as a dev dependency to run tests.
Install
-
npm install yhtml5-test -
yarn add yhtml5-test -
pnpm add yhtml5-test
Imports
- CLI Test Commands
import { test } from 'yhtml5-test'npm test npm run test:c npm run test:u
- Configuration Object
// .config.js export default config;
// .config.js const path = require('path'); module.exports = config; - Test File Imports
const MyComponent = require('./MyComponent');import { shallow } from 'enzyme'; import MyComponent from './MyComponent';
Quickstart
{
"name": "my-react-app",
"version": "1.0.0",
"scripts": {
"test": "2dfire-scripts test --env=jsdom",
"test:c": "npm test -- --coverage",
"test:u": "npm i @2dfire/2dfire-scripts@latest -D"
},
"devDependencies": {
"@2dfire/2dfire-scripts": "latest",
"react-test-renderer": "15.6.x",
"enzyme": "2.9.x",
"jest-enzyme": "3.8.x"
}
}
// Create .config.js in your project root
// This configures webpack aliases and Jest transformers
// .config.js
const path = require('path');
const webpackConfigAlias = {
'^src(.*)$': path.resolve(__dirname, 'src$1'),
};
const config = {
test: {
testMatch: ['<rootDir>/src/**/__tests__/**/*.js?(x)', '<rootDir>/src/**/?(*.)(spec|test).js?(x)'],
transformIgnorePatterns: ["node_modules/(?!(yhtml5-test|react-redux|react-native-button)/)"],
moduleNameMapper: webpackConfigAlias,
collectCoverageFrom: ['src/**/*.{js,jsx}'],
}
};
module.exports = config;
// Example test file: src/components/__tests__/MyComponent.test.js
import React from 'react';
import { shallow } from 'enzyme';
import MyComponent from '../MyComponent';
describe('MyComponent', () => {
it('renders correctly', () => {
const wrapper = shallow(<MyComponent />);
expect(wrapper).toMatchSnapshot();
});
it('displays the correct text', () => {
const wrapper = shallow(<MyComponent text="Hello Test" />);
expect(wrapper.find('h1').text()).toEqual('Hello Test');
});
});