Skip to content

How to choose a modern test framework for frontend in the 2024

Updated: at 04:52 PM

Background

I had a task in near weeks that design a frontend test framework system for my job. Then, I survey more and more test framework to research it different feature and different service. So this blog is result about this survey action.

Conclusion first: Our choose the framework is Storybook and Playwright. image.png

First, I just tell you, frontend testing is very important in DevOps workflow, cos our develop process is very fast, then our inevitable to take place some bug in production environments. If your testing was good and coverage is full, then you needn’t worry about release new version.

What is test framework for frontend

A good test framework can help you to write test case easily, and run test case quickly, and coverage test case fully. So, a good test framework is very important in your develop process. In the frontend, we have some test framework, like Jest, Cypress, Playwright, Storybook, etc. They have different feature and different service, so we need to choose a better test framework to use in our project. It is a table about the test framework comparison.

ToolMain FeaturesAdvantagesDisadvantagesUse Cases
SeleniumAutomated Web application testingOpen-source, multi-browser and OS support, multi-language supportComplex API, slow execution, high maintenance cost, steep learning curveEnd-to-end testing, cross-browser testing
CypressEnd-to-end testingFast execution, modern framework support, easy to useNo multi-browser support, limited features, only supports JavaScriptE2E testing for JavaScript frameworks (React, Vue, etc.)
JestUnit and integration testingEasy to use, built-in assertion library, supports snapshot testing, built-in mock supportNo end-to-end testing, only runs in Node.js environmentUnit testing, integration testing, React application testing
StorybookUI component development and presentation toolIndependent development environment, supports snapshot testing, supports multiple frameworksNo end-to-end testing, no business logic testing, mainly for UI component displayUI component development, component state testing
MochaUnit and integration testingFlexible, supports multiple assertion libraries, supports async and parallel testingSteep learning curve, no snapshot testing, lacks built-in mockingUnit testing, integration testing, Node.js and browser testing
PuppeteerBrowser automation for Chrome, web scrapingControls Chrome, supports user action simulation, retrieves page infoOnly supports Chrome, no parallel testingWeb scraping, end-to-end testing in Chrome
VitestVite-based testing tool for unit and integration testingVite integration, supports multiple testing types, supports snapshot testingSteep learning curve, complex configuration, no end-to-end testingVite project testing
PlaywrightCross-browser end-to-end testingSupports multiple browsers (Chromium, Firefox, WebKit), handles modern web apps, supports parallel testingHeavier setup compared to Cypress, steeper learning curveCross-browser end-to-end testing, modern web apps testing

Considerations when choosing a frontend test framework:

Why we choose Storybook and Playwright?

  • Storybook: Cos our had a UI component library used of it to build. So we need a test framework to test it.
  • Playwright: Cos our project is a modern web app, so we need a cross-browser end-to-end testing tool to test it. Our compare result is Playwright is better than Cypress, so we choose it. Why not choose Cypress? Cos Cypress is only support Chrome browser, and our project need to support multi-browser, and Cypress has many business service in Playwright is free, so we choose Playwright. image.png

How to you how to use it in your project.

Let’s hand by hand to tell you do it. Cos our used Storybook and Playwright in our project. So I will show you how to use it in your project.

How to use Storybook

  1. Install Storybook
npx sb init
  1. Start Storybook
npm run storybook
  1. Write your story
import React from 'react';
import { Button } from './Button';

export default {
  title: 'Example/Button',
  component: Button,
};

const Template = (args) => <Button {...args} />;

export const Primary = Template.bind({});

Primary.args = {
  primary: true,
  label: 'Button',
};

export const Secondary = Template.bind({});

Secondary.args = {
  label: 'Button',
};
  1. Write your test case
import React from 'react';
import { render } from '@testing-library/react';
import { Primary, Secondary } from './Button.stories';

it('renders the button in the primary state', () => {
  const { getByText } = render(<Primary {...Primary.args} />);
  expect(getByText('Button')).toHaveClass('button--primary');
});

it('renders the button in the secondary state', () => {
  const { getByText } = render(<Secondary {...Secondary.args} />);
  expect(getByText('Button')).toHaveClass('button--secondary');
});

How to use Playwright

  1. Install Playwright
npm install -D playwright
  1. Write your test case
const { test, expect } = require('@playwright/test');

test('basic test', async ({ page }) => {
  await page.goto('https://playwright.dev/');
  const title = page.locator('.navbar__title');
  await expect(title).toHaveText('Playwright');
});
  1. Run your test case
npx playwright test

Conclusion

In this blog, I tell you how to choose a modern test framework for frontend in the 2024. And I tell you how to use Storybook and Playwright in your project. Hope it can help you to choose a better test framework for your project.

Links