Frontend Test Framework

1.2.2 · maintenance · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart guides you through setting up `yhtml5-test` by adding required scripts to `package.json`, installing development dependencies, configuring `.config.js` for Jest and Webpack aliases, and writing a basic React component test using Enzyme and snapshot testing.

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

view raw JSON →