# mock-path-with-spy-that-returns-x

> Mocks a path with a spy that returns x

Latest version **2.0.0** (published 2016-12-31) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install mock-path-with-spy-that-returns-x
pnpm add mock-path-with-spy-that-returns-x
yarn add mock-path-with-spy-that-returns-x
bun add mock-path-with-spy-that-returns-x
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2016-12-31 |
| First published | 2016-12-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Shahar Or |
| Maintainers | mightyiam |
| Keywords | mock, spy, simple-spy, mock-require |

## Links

- npm: https://www.npmjs.com/package/mock-path-with-spy-that-returns-x
- Repository: https://github.com/mightyiam/mock-path-with-spy-that-returns-x
- Homepage: https://github.com/mightyiam/mock-path-with-spy-that-returns-x#readme
- Issues: https://github.com/mightyiam/mock-path-with-spy-that-returns-x/issues
- npm.io page: https://npm.io/package/mock-path-with-spy-that-returns-x

## Dependencies (2)

- [simple-spy](https://npm.io/package/simple-spy.md) ^2.1.0
- [mock-require](https://npm.io/package/mock-require.md) ^2.0.0

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [@crvouga/mockingbird-service-prism](https://npm.io/package/@crvouga/mockingbird-service-prism.md) — 0 weekly downloads
- [@crvouga/mockingbird-service-otel](https://npm.io/package/@crvouga/mockingbird-service-otel.md) — 0 weekly downloads
- [@crvouga/mockingbird-service-plane](https://npm.io/package/@crvouga/mockingbird-service-plane.md) — 0 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2016-12-31
- 1.1.0 — 2016-12-28
- 1.0.0 — 2016-12-25

## README

[![Build Status](https://travis-ci.org/mightyiam/mock-path-with-spy-that-returns-x.svg?branch=master)](https://travis-ci.org/mightyiam/mock-path-with-spy-that-returns-x)
[![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

# mock-path-with-spy-that-returns-x

[Mocks](https://www.npmjs.com/package/mock-require)
a given path with a
[spy](https://www.npmjs.com/package/simple-spy)
that returns either a symbol or something provided by you.

## Why?

Because in unit tests, we mock some dependency functions.  
And, usually, we like them to return a constant value.  
And, usually, we like to assert their calls/args.  
And, usually, we `require` the test subject multiple times.

This utility fits that pattern.

## How?

### Example

#### `dep.js`
```js
// we will be mocking this file

module.exports = (x) => x.toUpperCase()
```

#### `index.js`
```js
// this module will be our test subject

const dep = require('./dep')
module.exports = (x) => dep(x) + '-foo'
```

#### `index.test.js`
```js
// unit tests here

const assert = require('assert')
const mockPathWithSpy = require('mock-path-with-spy-that-returns-x')
const requireUncached = require('require-uncached')

// set up a mock
const depMocks = mockPathWithSpy(
  './dep', // path to mock
  'MOCKED' // mocked function return value
)

// test A
const depMockA = depMocks.next().value // mock
const subjectA = requireUncached('.')
const actualA = subjectA('a')
assert.strictEqual(actualA, 'MOCKED-foo') // `./dep` is mocked
assert.deepStrictEqual(depMockA.spy.args, [['a']]) // spy available

// test B
const depMockB = depMocks.next().value
const subjectB = requireUncached('.')
const actualB = subject('b')
assert.strictEqual((actualB, 'MOCKED-foo')
assert.deepStrictEqual(depMockB.spy.args, [['b']])
```

### API

#### `mockPathWithSpy(path[, constantReturn])` (generator)

- `path`  
  The path to mock.
  Will be passed to
  [`mock`](https://www.npmjs.com/package/mock-require#mockpath-mockexport).
- `constantReturn` (optional)  
  If provided, the spy will return provided value.  
  If not provided, the spy will return a symbol
  that is unique to each mock.  
  The description of the symbols will be `path`.  

Returns an iterator:  
On `next`, the `path` is mocked with a new spy
and an object is returned:
- `spy`  
  The [simple-spy](https://www.npmjs.com/package/simple-spy)
  spy
- `spyReturn`  
  The spy’s return value
- `stop()`  
  Calls [`mock.stop`](https://github.com/boblauer/mock-require#mockstoppath)
  on the `path`

### Caveats

#### Uses `module.parent`

So it must always be `require`d
directly in the module where it is used.
This may be fixed by using
[caller-path](https://www.npmjs.com/package/caller-path)
instead of `module.parent`,
so please report an issue
if you find that it bothers you.

#### `require`s the mocked module

The `path` is `require`d
and the exported function’s length is examined,
for the purpose of the mock function
being of the same arity as the mocked function.

---
_Source: https://npm.io/package/mock-path-with-spy-that-returns-x · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
