All Projects → Xiphe → test-real-styles

Xiphe / test-real-styles

Licence: MIT license
(test-)framework agnostic utilities to test real styling of (virtual) dom elements

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to test-real-styles

playwright-pytest
Pytest plugin to write end-to-end browser tests with Playwright.
Stars: ✭ 214 (+478.38%)
Mutual labels:  testing-tools, playwright
browser-automation-api
Browser automation API for repetitive web-based tasks, with a friendly user interface. You can use it to scrape content or do many other things like capture a screenshot, generate pdf, extract content or execute custom Puppeteer, Playwright functions.
Stars: ✭ 24 (-35.14%)
Mutual labels:  browser-automation, playwright
SpecTools
Write less test code with this set of spec tools. Swift, iOS, testing framework independent (but works well with Quick/Nimble or directly).
Stars: ✭ 38 (+2.7%)
Mutual labels:  testing-tools, ui-testing
Compose
Nice and simple DSL for Espresso Compose UI testing in Kotlin
Stars: ✭ 56 (+51.35%)
Mutual labels:  ui-testing, testing-library
react-router-testing-utils
A collection of utilities to test React Router with React Testing Library (Work in progress 🚧)
Stars: ✭ 34 (-8.11%)
Mutual labels:  testing-tools, testing-library
api-test
🌿 A simple bash script to test JSON API from terminal in a structured and organized way.
Stars: ✭ 53 (+43.24%)
Mutual labels:  testing-tools, testing-library
Jest Dom
🦉 Custom jest matchers to test the state of the DOM
Stars: ✭ 2,908 (+7759.46%)
Mutual labels:  jsdom, testing-library
FlaUIRecorder
UIAutomation test recorder for using with FlaUI library.
Stars: ✭ 23 (-37.84%)
Mutual labels:  testing-tools, ui-testing
scenarioo
Scenarioo Docu Viewer for Automated Documentation using UI/E2E-Tests
Stars: ✭ 62 (+67.57%)
Mutual labels:  testing-tools, ui-testing
toster
DSL framework for testing Android apps
Stars: ✭ 31 (-16.22%)
Mutual labels:  testing-tools, testing-library
browser-pool
A Node.js library to easily manage and rotate a pool of web browsers, using any of the popular browser automation libraries like Puppeteer, Playwright, or SecretAgent.
Stars: ✭ 71 (+91.89%)
Mutual labels:  browser-automation, playwright
playwright-test
Run unit tests with several runners or benchmark inside real browsers with playwright.
Stars: ✭ 81 (+118.92%)
Mutual labels:  testing-tools, playwright
playwright-fluent
Fluent API around playwright
Stars: ✭ 71 (+91.89%)
Mutual labels:  testing-library, playwright
Recorder
A browser extension that generates Cypress, Playwright and Puppeteer test scripts from your interactions 🖱 ⌨
Stars: ✭ 277 (+648.65%)
Mutual labels:  testing-tools, playwright
playwright-testing-library
🔍 Find elements in Playwright with queries from Testing Library
Stars: ✭ 160 (+332.43%)
Mutual labels:  testing-library, playwright
react-jsdom
Render React components to actual DOM nodes in Node.js
Stars: ✭ 31 (-16.22%)
Mutual labels:  jsdom, testing-tools
mocat
🐈 Mocat is a mocking toolbar that allows you to interactively develop and test network requests.
Stars: ✭ 27 (-27.03%)
Mutual labels:  testing-tools, testing-library
scalatest-junit-runner
JUnit 5 runner for Scalatest
Stars: ✭ 30 (-18.92%)
Mutual labels:  testing-tools, testing-library
merlin
Testing and Benchmarking framework for deno 🧙‍♂️
Stars: ✭ 46 (+24.32%)
Mutual labels:  testing-tools
react-github-profile
Learning about (unit & integration) testing on React
Stars: ✭ 19 (-48.65%)
Mutual labels:  testing-library

test-real-styles

test & release codecov npm semantic-release Love and Peace

(test-)framework agnostic utilities to test real styling of (virtual) dom elements

Motivation

I created this with jest and testing-library in mind to programmatically test appearance effects of component APIs in real browsers.

While @testing-library/jest-dom has a toHaveStyle assertion and there are ways to test css-in-js all solutions I've tried ignore the css cascade or use a buggy/incomplete simulation of it.

But real-world components do use the cascade. This library aims to give you confidence that a style is actually active on an element.

Installation

npm install test-real-styles

Usage

Assuming a NodeJS test environment with jsdom like jest.

import { getRealStyles, launch, toCss } from 'test-real-styles';

const MY_CSS = `
  button { background-color: fuchsia; }
  button::after {
    border: 2px solid olive;
  }
  button:hover { color: #123456; }
  button:focus { color: rgba(255, 0, 123, 0.5); }
`;

describe(`button`, () => {
  it('is pink', async () => {
    /**
     * This will
     * - launch a headless chromium
     * - insert the css
     * - update the page with `doc`
     * - return getComputedStyle(doc)['backgroundColor']
     */
    const styles = await getRealStyles({
      css: MY_CSS,
      doc: document.createElement('button'),
      getStyles: ['backgroundColor'],
    });

    expect(styles).toEqual({ backgroundColor: 'fuchsia' });
  });

  describe('launch API', () => {
    /* In case you want to re-use the browser, interact with the page or do stuff
       before styles are returned */
    const { updatePage, getStyles, hover, focus } = launch('webkit', MY_CSS);

    it('gets hover and focus styles', async () => {
      const button = document.createElement('button');
      await updatePage(button);

      await hover(button);
      const hoverStyles = await getStyles(button, ['color', 'backgroundColor']);
      await focus(button);
      const focusStyles = await getStyles(button, ['color']);

      expect(toCss(hoverStyles)).toMatchInlineSnapshot(`
        "color: #123456;
        background-color: fuchsia;"
      `);
      expect(focusStyles.color).toBe('rgba(255, 0, 123, 0.5)');
    });

    it('gets pseudo elements', async () => {
      const button = document.createElement('button');
      await updatePage(button);

      expect(
        await getStyles(button, ['border'], { pseudoElt: '::after' }),
      ).toEqual({ border: '2px solid olive' });
    });
  });
});

Examples

License

The MIT License

Copyright (C) 2020 Hannes Diercks

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].