Giter Site home page Giter Site logo

snapshot-fixtures's Introduction

snapshot-fixtures

github actions codecov npm version npm downloads

Snapshot fixtures for node:test.

Table of Contents

Installation

npm install snapshot-fixtures

When to use this?

This package exposes a function for creating tests based on a directory containing test fixtures. This is useful if you want to run tests based on a string input and a string output, especially for multiline content.

Usage

Let’s say you have the following project structure:

my-project/
├── fixtures/
│   ├── fixture-1/
│   │   ├── input.txt
│   │   ├── expected-1.txt
│   │   ├── expected-2.txt
│   │   └── options.json
│   └── fixture-2/
│       ├── input.txt
│       ├── expected-1.txt
│       ├── expected-2.txt
│       └── options.js
└── test/
    └── fixtures.test.js

Then src/fixtures.test.js could look like this:

import { fn } from 'my-project'
import { testFixturesDirectory } from 'snapshot-fixtures'

testFixturesDirectory({
  directory: new URL('../fixtures', import.meta.url),
  tests: {
    'expected-1.txt'(file, options) {
      return fn(file, options)
    },

    'expected-2.txt'(file, options) {
      return fn(file, options, 'additional options')
    }
  }
})

This test asserts that the contents of the expected-1.txt and expected-2.txt tests of each fixture match the respective return value of matching test function.

API

assertEqual(actual, expected[, options])

Assert two strings are equal.

If the strings are not equal, an assertion error will be thrown. This assertion error contains a pretty diff of the two strings.

Arguments

  • actual (string) — The actual value that was produced.
  • expected (string) — The value that was expected.
  • options (object, optional) — An object with additional options. The following options are supported:
    • url (string | URL) — The file URL to include in the assertion error if expected is not equal to actual.

testFixturesDirectory(options)

Create a test suite for a fixtures directory based on node:test. A fixtures directory is a directory that contains other directories. For each of these directories a nested test suite is created. For each of these nested test suites, a test is created based on the tests passed. Each test reads the input file, uses the given test function to generate output, and compares it to the content of the expected output file.

Options

  • directory (string | URL) — The directory containing fixtures as a URL or URL string.
  • prettier (boolean, optional) — If true, format the generated value with Prettier. (Default: false)
  • tests (object) — A mapping of test name to a fixture test.
  • write (boolean) — If true, overwrite the expected content with the actual content. In CI, the output is never written. (Default: false)

Test

A test to run, either as a fixture test object, or as a generate function. The generate function takes a VFile as input, which is read from the input file and return a string that should match the expected output. A second argument options can be accepted. This may contain the options of the fixture. These options are read from the options.cjs, options.js, options.json or options.mjs file in the fixture directory.

A test may be an object with the generate and optional input, expected properties. In this case input determines which input file to read, generate serves as the generate function, and expected refers to the expected output file.

Compatibility

This project is compatible with Node.js 16 or greater.

License

MIT © Remco Haszing

snapshot-fixtures's People

Contributors

remcohaszing avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.