0.1.21 • Published 2 years ago

geste-test v0.1.21

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Features

  • Drop-in replacement to Jest
  • TypeScript and JSX support out of the box
  • Supa-fast compilation of test files
  • Support of async tests
  • Support of both ESM and CJS syntaxes
  • Built-in support of benchmark tests

Installation

npm install --save-dev geste-test

Usage

To run your whole test suite, at the root of your project:

geste

To run only specific tests:

geste tests/utils/*.ts

Available options

  • --update-snapshots: when set, failing expect.toMatchSnapshot() assertions will update corresponding snapshot files.
  • --bench: run benchmark tests right after unit tests
  • --only-bench: run only benchmark tests and ignore unit tests

Usage with tests needing a DOM

Install linkedom and (optionally) @testing-library/jest-dom:

npm install --save-dev linkedom
# optional: for usage with testing-library
npm install --save-dev @testing-library/jest-dom

Create a geste.config.ts file at the root of your project (next to package.json):

// geste.config.ts

export default {
  setupFiles: ["./setupTests.ts"],
};

Then create the setupTests.ts file, which will be ran before each one of your tests (see https://jestjs.io/docs/configuration#setupfiles-array):

// setupTests.ts

import { parseHTML } from "linkedom";
// optional: for usage with testing-library (run `npm install @testing-library/jest-dom`)
import "@testing-library/jest-dom/extend-expect";

const defaultHtml =
  '<!doctype html><html><head><meta charset="utf-8"></head><body></body></html>';

const dom = parseHTML(defaultHtml);
const { window } = dom;
const { document } = window;

// add missing window.location property
if (!window.location) {
  // @ts-ignore
  window.location = { protocol: "http" };
}

// add missing window.getComputedStyled property
if (!window.getComputedStyle) {
  // https://github.com/mikemadest/jest-environment-linkedom/blob/main/src/get-computed-style-polyfill.js
  function getComputedStyle(element) {
    this.el = element;

    this.getPropertyValue = function (prop) {
      const regExp = /(\-([a-z]){1})/g;
      let updatedProp = prop === "float" ? "styleFloat" : prop;

      if (regExp.test(updatedProp)) {
        updatedProp = updatedProp.replace(regExp, function (match, ...parts) {
          return parts[1].toUpperCase();
        });
      }

      return element?.currentStyle?.[updatedProp]
        ? element.currentStyle[updatedProp]
        : null;
    };

    return this;
  }

  window.getComputedStyle = getComputedStyle;
}

// add missing default width/height of window (1024x768 is JSDOM's default)
if (!window.innerWidth) {
  window.innerWidth = 1024;
}
if (!window.outerWidth) {
  window.outerWidth = 1024;
}
if (!window.innerHeight) {
  window.innerHeight = 768;
}
if (!window.outerHeight) {
  window.outerHeight = 768;
}

// add in global all window's properties that are not already defined
for (const key of Object.getOwnPropertyNames(window).filter(
  (k) => !k.startsWith("_") && !global[k]
)) {
  global[key] = window[key];
}

global.document = document;
global.window = window;
global.navigator = window.navigator;
window.console = global.console;

API

geste's API aims to be the exact same as Jest's:

However this is a WIP project so more APIs will be implemented in the future. Feel free to fill in an issue to ask for the APIs you'd like to be implemented in priority.

Benchmark API

geste also implements a benchmarking API inspired by Golang's. Benchmark tests are written the same way as unit tests and are launched only when running geste with the --bench option (or --only-bench to run only benchmark tests).

  • benchmark: (desc: string, cb: (b: BenchmarkTools) => any) => void: register a benchmark test
import { BenchmarkTools } from "geste-test";

benchmark("benchmark factorialize", (b: BenchmarkTools) => {
  for (let i = 0; i < b.N; i++) {
    factorialize(10);
  }
});

// `geste --bench` will output something like:
// tests/benchmarks.test.ts
// b  benchmark factorialize(10)     20971520     82ns/op     1714ms
  • benchmark.skip: (desc: string, cb: (b: BenchmarkTools) => any) => void: skip a benchmark test

  • benchmark.each: (cases: any[]) => (desc: string, cb: (b: BenchmarkTools, ...args: any[]) => any) => void: register a benchmark test for each case provided. Useful to benchmark a function with various inputs.

benchmark.each([[10], [100], [1000]])("benchmark factorialize(%i)", (b, n) => {
  for (let i = 0; i < b.N; i++) {
    factorialize(n);
  }
});

Configuration

This project aims to be a drop-in replacement to Jest, so the configuration options are very much inspired by it.

You can configure geste's options by creating a geste.config.ts file at the root of your project.

These are the default configuration values:

// geste.config.ts

export default {
  testPatterns: [
    "**/__tests__/**/*.[jt]s?(x)",
    "**/?(*.)+(spec|test).[jt]s?(x)",
  ],
  ignorePatterns: ["**/node_modules/**/*"],
  setupFiles: [],
  throwOnCompilationErrors: true,
};

Here are the list of the currently supported configuration options, and their Jest's "equivalent":

  • testPatterns: array of glob patterns used to walk your project's sources and find test files. Equivalent to Jest's testMatch
  • ignorePatterns: array of glob patterns that will be ignored when looking for test files. Close to Jest's testPathIgnorePatterns but here it's a array of glob patterns instead of regexp patterns.
  • setupFiles: array of paths to modules that will be compiled and evaluated before every one of your test files. Equivalent to Jest's setupFiles
  • throwOnCompilationErrors: should geste throw if it encounters an error while compiling a test file. Kinda same as setting Jest's bail to true
0.1.21

2 years ago

0.1.20

2 years ago

0.1.19

2 years ago

0.1.18

2 years ago

0.1.17

2 years ago

0.1.16

2 years ago

0.1.15

2 years ago

0.1.14

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago