Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
remcohaszing committed Mar 25, 2024
0 parents commit e9989f2
Show file tree
Hide file tree
Showing 60 changed files with 9,178 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"100": true,
"reporter": ["html", "lcov", "text"]
}
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 100
trim_trailing_whitespace = true

[COMMIT_EDITMSG]
max_line_length = 72
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.jsx
4 changes: 4 additions & 0 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
root: true
extends:
- remcohaszing
- remcohaszing/typechecking
95 changes: 95 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: ci

on:
pull_request:
push:
branches: [main]
tags: ['*']

jobs:
eslint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx eslint .

pack:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npm pack
- uses: actions/upload-artifact@v4
with:
name: package
path: '*.tgz'

test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version:
- 16
- 18
- 20
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: node --test
if: ${{ matrix.node-version == 16 }}
- run: npm test
if: ${{ matrix.node-version >= 18 }}
- uses: codecov/codecov-action@v4
if: ${{ matrix.node-version == 20 }}

prettier:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx prettier --check .

remark:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- run: npx remark --frail .

release:
runs-on: ubuntu-latest
needs:
- eslint
- test
- pack
- prettier
- remark
if: startsWith(github.ref, 'refs/tags/')
permissions:
id-token: write
steps:
- uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org
- uses: actions/download-artifact@v4
with: { name: package }
- run: npm publish *.tgz --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage/
dist/
node_modules/
*.log
*.tsbuildinfo
*.tgz
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.jsx
4 changes: 4 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
proseWrap: always
semi: false
singleQuote: true
trailingComma: none
2 changes: 2 additions & 0 deletions .remarkrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plugins:
- remark-preset-remcohaszing
18 changes: 18 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# MIT License

Copyright © 2024 Remco Haszing

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.
134 changes: 134 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# snapshot-fixtures

[![github actions](https://github.com/remcohaszing/snapshot-fixtures/actions/workflows/ci.yaml/badge.svg)](https://github.com/remcohaszing/snapshot-fixtures/actions/workflows/ci.yaml)
[![codecov](https://codecov.io/gh/remcohaszing/snapshot-fixtures/branch/main/graph/badge.svg)](https://codecov.io/gh/remcohaszing/snapshot-fixtures)
[![npm version](https://img.shields.io/npm/v/snapshot-fixtures)](https://www.npmjs.com/package/snapshot-fixtures)
[![npm downloads](https://img.shields.io/npm/dm/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` 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](LICENSE.md) © [Remco Haszing](https://github.com/remcohaszing)
1 change: 1 addition & 0 deletions fixtures/extensionless/custom-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
1 change: 1 addition & 0 deletions fixtures/extensionless/custom-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
1 change: 1 addition & 0 deletions fixtures/extensionless/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No extension
1 change: 1 addition & 0 deletions fixtures/extensionless/test-function.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No extension
1 change: 1 addition & 0 deletions fixtures/extensionless/test-object.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No extension
1 change: 1 addition & 0 deletions fixtures/extensionless/test-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions fixtures/extensionless/trailing-slash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Trailing slash
1 change: 1 addition & 0 deletions fixtures/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file exists to test that plain files are skipped.
1 change: 1 addition & 0 deletions fixtures/options-cjs/custom-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
1 change: 1 addition & 0 deletions fixtures/options-cjs/custom-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
Empty file added fixtures/options-cjs/input.txt
Empty file.
1 change: 1 addition & 0 deletions fixtures/options-cjs/options.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.options = 'cjs'
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions fixtures/options-cjs/test-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": "cjs"
}
1 change: 1 addition & 0 deletions fixtures/options-cjs/trailing-slash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Trailing slash
1 change: 1 addition & 0 deletions fixtures/options-js/custom-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
1 change: 1 addition & 0 deletions fixtures/options-js/custom-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
Empty file added fixtures/options-js/input.txt
Empty file.
3 changes: 3 additions & 0 deletions fixtures/options-js/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
options: 'js'
}
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions fixtures/options-js/test-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": "js"
}
1 change: 1 addition & 0 deletions fixtures/options-js/trailing-slash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Trailing slash
1 change: 1 addition & 0 deletions fixtures/options-json/custom-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
1 change: 1 addition & 0 deletions fixtures/options-json/custom-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
Empty file added fixtures/options-json/input.txt
Empty file.
3 changes: 3 additions & 0 deletions fixtures/options-json/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": "json"
}
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions fixtures/options-json/test-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": "json"
}
1 change: 1 addition & 0 deletions fixtures/options-json/trailing-slash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Trailing slash
1 change: 1 addition & 0 deletions fixtures/options-mjs/custom-input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
1 change: 1 addition & 0 deletions fixtures/options-mjs/custom-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Custom input and output
Empty file added fixtures/options-mjs/input.txt
Empty file.
3 changes: 3 additions & 0 deletions fixtures/options-mjs/options.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
options: 'mjs'
}
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions fixtures/options-mjs/test-options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"options": "mjs"
}
1 change: 1 addition & 0 deletions fixtures/options-mjs/trailing-slash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Trailing slash
Loading

0 comments on commit e9989f2

Please sign in to comment.