# test-fixture

> Copy test-fixtures to temp dir and get resolved file paths.

Latest version **2.4.1** (published 2019-08-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install test-fixture
pnpm add test-fixture
yarn add test-fixture
bun add test-fixture
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.4.1 |
| Published | 2019-08-17 |
| First published | 2014-06-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 7 |
| Dependencies | 4 |
| Unpacked size | 7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | kaelzhang |
| Maintainers | kael |
| Keywords | test, fixtures, resolve, copy, temp, tmp |

## Links

- npm: https://www.npmjs.com/package/test-fixture
- Repository: https://github.com/kaelzhang/node-test-fixture
- Homepage: https://github.com/kaelzhang/node-test-fixture#readme
- Issues: https://github.com/kaelzhang/node-test-fixture/issues
- npm.io page: https://npm.io/package/test-fixture

## Dependencies (4)

- [fs-extra](https://npm.io/package/fs-extra.md) ^7.0.1
- [npminstall](https://npm.io/package/npminstall.md) ^4.0.0
- [tmp-promise](https://npm.io/package/tmp-promise.md) ^1.1.0
- [core-util-is](https://npm.io/package/core-util-is.md) ^1.0.2

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 2.4.1 (latest) — 2019-08-17
- 2.4.0 — 2019-05-11
- 2.3.0 — 2019-05-11
- 2.2.0 — 2019-05-11
- 2.1.1 — 2019-05-11
- 2.1.0 — 2019-05-11
- 2.0.0 — 2019-05-11
- 1.0.2 — 2014-08-05
- 1.0.1 — 2014-06-12
- 1.0.0 — 2014-06-08
- 0.1.1 — 2014-06-07
- 0.1.0 — 2014-06-07

## README

[![Build Status](https://travis-ci.org/kaelzhang/node-test-fixture.svg?branch=master)](https://travis-ci.org/kaelzhang/node-test-fixture)
[![Coverage](https://codecov.io/gh/kaelzhang/node-test-fixture/branch/master/graph/badge.svg)](https://codecov.io/gh/kaelzhang/node-test-fixture)

# test-fixture

Copy test fixtures to a temp dir and get resolved file paths.

### Why?

I am tired of writing these:

```js
path.resolve(__dirname, 'test', 'fixtures')
tmp.dir(callback)
fse.copy(fixtures, dir)
path.join(fixtures, 'some-file.js')
```

**EVERY DAY!**

So, I got this.

## Install

```bash
$ npm i test-fixture -D
```

## Usage

```js
// By default, it will use 'test/fixtures' dir.
const {copy, resolve} = require('test-fixture')()

// copy 'test/fixtures' to the temp dir
;(async () => {
  await copy()
  console.log(resolve('foo.js'))
  // '/<temp-dir>/foo.js'
})
```

### fixtures(...paths): {resolve, copy, install, root}

- **paths** `Array<path>` to define the root paths of the fixtures, which is similar as

Defines the root of the fixture

```js
const path = require('path')
const fixturesRoot = path.resolve(projectRoot, 'test', 'fixtures')

path.resolve(fixturesRoot, ...paths)
```

`...paths` | `base`(dir of test fixtures)
--------- | --------------------
`undefined` | `test/fixtures`
`'a'` | `test/fixtures/a`
`'a'`, `'b'` | `test/fixtures/a/b`
`'/path/to'` (absolute) | `/path/to`
`'/path/to'` (absolute), `'a'` | `/path/to/a`

Actually, the `base` is `path.resolve('text/fixtures', ...paths)`

#### Without copying

```js
const {resolve} = fixtures(p)
resolve('a.js')  // -> /path/to/<p>/a.js
```

#### Using `.copy()`

```js
const {copy, resolve} = fixtures(p)

await copy('/path/to')

resolve('a.js') // -> /path/to/a.js
```

### await copy(options?)
### await copy(to?)

- **options?** `Object`
  - **to?** `path=` the destination folder where the test fixtures will be copied to. If not specified, a temporary directory will be used.
  - **clean?** `boolean=false` whether should clean the directory `to` if `options.to` is specified.
  - **install?** `boolean=false` whether should run npm install after copying

Copy the test fixtures into another directory.

```js
copy('/path/to')

// is equivalent to
copy({
  to: '/path/to',
  install: false
})
```

### resolve(...paths)

Resolves the paths to get the path of the test fixtures

After `.copy()`ed, it will resolve paths based on the destination dir.

If not, it will use the base dir. But never use both of them simultaneously.

```
/path/to/<base>
             |-- a.js
/path/to/<to>
           |-- a.js
```

### await install(options?)

- **options?** `Object` options of [`npminstall`](https://npmjs.org/package/npminstall)

Install packages in the working directory. If run after `await copy()`, then it will install packages in the directory which fixtures copied to.

### getter: root `string`

The fixture root `/<project-root>/test/fixtures`

## License

[MIT](LICENSE)

---
_Source: https://npm.io/package/test-fixture · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
