https://github.com/remcohaszing/snapshot-fixtures
Snapshot fixtures for node:test
https://github.com/remcohaszing/snapshot-fixtures
assert diff equal fixture snapshot string test
Last synced: about 1 year ago
JSON representation
Snapshot fixtures for node:test
- Host: GitHub
- URL: https://github.com/remcohaszing/snapshot-fixtures
- Owner: remcohaszing
- License: mit
- Created: 2024-03-25T19:22:40.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-26T09:55:26.000Z (about 2 years ago)
- Last Synced: 2024-05-02T05:14:23.839Z (about 2 years ago)
- Topics: assert, diff, equal, fixture, snapshot, string, test
- Language: TypeScript
- Homepage:
- Size: 172 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# snapshot-fixtures
[](https://github.com/remcohaszing/snapshot-fixtures/actions/workflows/ci.yaml)
[](https://codecov.io/gh/remcohaszing/snapshot-fixtures)
[](https://www.npmjs.com/package/snapshot-fixtures)
[](https://www.npmjs.com/package/snapshot-fixtures)
Snapshot fixtures for [`node:test`](https://nodejs.org/api/test.html).
## Table of Contents
- [Installation](#installation)
- [When to use this?](#when-to-use-this)
- [Usage](#usage)
- [API](#api)
- [`assertEqual(actual, expected[, options])`](#assertequalactual-expected-options)
- [Arguments](#arguments)
- [`testFixturesDirectory(options)`](#testfixturesdirectoryoptions)
- [Options](#options)
- [`Test`](#test)
- [Compatibility](#compatibility)
- [License](#license)
## Installation
```sh
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:
```typescript
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`](https://nodejs.org/api/test.html). 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](#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`](https://github.com/vfile/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`, and `ignore`
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. If `ignore` is true, the
result is written, but assertion failures are ignored.
## Compatibility
This project is compatible with Node.js 16 or greater.
## License
[MIT](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)