Skip to content
This repository was archived by the owner on Nov 2, 2025. It is now read-only.

Conversation

@isaacs
Copy link
Member

@isaacs isaacs commented Feb 16, 2021

This brings into tap the standard mocking system we have been using
across the ecosystem of packages from the npm cli which consists into
hijacking the require calls from a given module and defining whatever
mock we want via something as simple as a key/value object.

It's a very conscious decision to make it a very opinionated API, as
stated in https://github.com/tapjs/node-tap#tutti-i-gusti-sono-gusti -
focusing only on the pattern that have been the standard way we handle
mocks.

It builds on the initial draft work from @nlf (ref:
https://gist.github.com/nlf/52ca6adab49e5b3939ba37c7f0fc51c6) and
initial brainstorming of such an API with @mikemimik - thanks ❤️

Example:

t.test('testing something, t => {
  const myModule = t.mock('../my-module.js', {
    fs: { readFileSync: () => 'foo' }
  })

  t.equal(myModule.bar(), 'foo', 'should receive expected content')
})

Credit: @ruyadorno, @nlf
Reviewed-by: @isaacs
PR-URL: tapjs/tapjs#698
Closes: tapjs/tapjs#698

This brings into **tap** the standard mocking system we have been using
across the ecosystem of packages from the npm cli which consists into
hijacking the `require` calls from a given module and defining whatever
mock we want via something as simple as a key/value object.

It's a very conscious decision to make it a very opinionated API, as
stated in https://github.com/tapjs/node-tap#tutti-i-gusti-sono-gusti -
focusing only on the pattern that have been the standard way we handle
mocks.

It builds on the initial draft work from @nlf (ref:
https://gist.github.com/nlf/52ca6adab49e5b3939ba37c7f0fc51c6) and
initial brainstorming of such an API with @mikemimik - thanks ❤️

Example:

```js
t.test('testing something, t => {
  const myModule = t.mock('../my-module.js', {
    fs: { readFileSync: () => 'foo' }
  })

  t.equal(myModule.bar(), 'foo', 'should receive expected content')
})
```

Credit: @ruyadorno, @nlf
Reviewed-by: @isaacs
PR-URL: tapjs/tapjs#698
Closes: tapjs/tapjs#698
@isaacs isaacs force-pushed the isaacs/add-mock-api branch from df2708f to 1e9ff26 Compare February 16, 2021 05:39
@coreyfarrell coreyfarrell merged commit e21cc1d into master Feb 16, 2021
@coreyfarrell coreyfarrell deleted the isaacs/add-mock-api branch February 16, 2021 13:35
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants