1.2.1 • Published 8 months ago

bleed-guard v1.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Bleed Guard

npm version NPM

Maintainability Test Coverage

Bleed Guard is a set of test reporters designed to help ensure clean test environments across various testing frameworks. It tracks potential issues like leftover DOM content, global window pollution, and incomplete network requests, making sure your tests are isolated and reliable.

Note: some test runners, like Jest, already have options like resetMocks and resetModules which do a great job at isolating tests and cleaning up. If that's all you're after you probably don't need these reporters. If on the otherhand you want to find and tix those places where you have bleed, either in your tests or your source, then read on.

Features

  • Framework Support
    Bleed Guard supports popular testing frameworks like:

  • Issue Tracking
    Each reporter can track and report the following issues:

    • DOM Cleanup: Detects if any DOM elements were left behind after tests run.
    • Global State Changes: Ensures no changes persist in the global window object that might bleed into subsequent tests.
    • Incomplete Network Requests: Identifies if there are any ongoing or incomplete network requests when a test completes.

Installation

NPMYarn
npm install -D bleed-guardyarn add -D bleed-guard

Usage

Simply add the relevant Bleed Guard reporter to your test configuration and call the setup method from the reporter once you have a test environment. Below are some examples:

Jest

In your jest.config.js:

module.exports = {
  testEnvironment: 'jsdom', // A browser-like environment is required if you want to enable dom checking
  reporters: [
    'default',  
    ['bleed-guard/jest', {
      domCheck: true,
      globalWindowCheck: true,
      shouldThrow: false
    }]
  ],
  setupFilesAfterEnv: ["./examples/jest/setup"] // Your setup file which will call the setup() from the reporter
};

Then in a test setup file where you have access to the environment before tests start running, usually provided via setupFilesAfterEnv:

require("bleed-guard/reporters/jest/jest").setup(beforeAll, afterEach, afterAll);

An example of this setup can be seen in examples/jest;

Vitest

In your vitest.config.js:

import { defineConfig } from 'vitest/config';
import BleedReporter from './../../reporters/vitest/vitest';

export default defineConfig({
  test: {
    environment: "jsdom", // A browser-like environment is required if you want to enable dom checking
    reporters: ['default', new BleedReporter()],
    setupFiles: ["./examples/vitest/setup"] // Your setup file which will call the setup() from the reporter
  },
})

Then in a test setup file where you have access to the environment before tests start running:

require("bleed-guard/reporters/vitest/vitest").setup(beforeAll, afterEach, afterAll);

An example of this setup can be seen in examples/vitest;

Reporter Options

Each reporter is a wrapper around more generic detection options you can customize to fit your testing setup. For example:

interface DetectionOptions {
  domCheck?: boolean;
  globalWindowCheck?: boolean;
  networkCheck?: boolean;
  shouldThrow?: boolean;
  logLevel?: LogLevel;  // "none" | "info" | "verbose"
  library?: "Jest" | "Vitest";
}
OptionDefaultUsage
domChecktrueEnables or disables tracking of leftover DOM elements
globalWindowChecktrueMonitors changes in the global window object
networkChecktrueTracks incomplete network requests between tests
shouldThrowfalseWill throw an error if any bleed is detected.
logLevel"info"Determines how much output will be logged to the console from the reporter.
Library""Used to include the reporter name while logging.

Contributing

First off, thank you for considering contributing. We are always happy to accept contributions and will do our best to ensure they receive the appropriate feedback.

Things to know:

  • Commits are required to follow the Conventional Commits Specification.
  • There is no planned/automated cadence for releases. Because of this once changes are merged they will be manually published, at least for the time being.

License

Bleed Guard is licensed under the MIT License. See the LICENSE file for details.

1.2.0

8 months ago

1.2.1

8 months ago

1.1.0

10 months ago

1.0.0

10 months ago