Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uvu/spy module #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

marvinhagemeister
Copy link
Contributor

This PR adds a new uvu/spy package that exports a function to spy on function calls. This works with both sync and async functions. All function arguments, return values and the proper this context is captured and can be asserted against.

Example:

import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { spy } from 'uvu/spy';

test('check if function is called twice', () => {
  const testFunction = spy((a, b) => a + b);

  add(1, 2);
  add(3, 4);

  // Check if called twice
  assert.is(testFunction.calls.length, 2);

  // Check return value of 1st call
  assert.is(testFunction.calls[0].return, 3);

  // Check arguments of 2nd call
  assert.equal(testFunction.calls[1].args, [3, 4]);
});

Fixes #10 .

@codecov-commenter
Copy link

codecov-commenter commented Jul 24, 2020

Codecov Report

Merging #32 into master will increase coverage by 1.11%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #32      +/-   ##
==========================================
+ Coverage   89.36%   90.47%   +1.11%     
==========================================
  Files           3        4       +1     
  Lines         282      315      +33     
==========================================
+ Hits          252      285      +33     
  Misses         30       30              
Impacted Files Coverage Δ
src/spy.js 100.00% <100.00%> (ø)
src/assert.js 91.35% <0.00%> (+1.08%) ⬆️
src/index.js 78.09% <0.00%> (+2.56%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a99e21a...6fbd0af. Read the comment docs.

docs/api.spy.md Outdated Show resolved Hide resolved
Co-authored-by: Marcis Bergmanis <marcisbergmanis@gmail.com>
Copy link

@tjoskar tjoskar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!
Just a side note: there is a similar lib, written in typescript, over here: https://github.com/tjoskar/simple-spy/blob/master/src/index.ts

src/spy.js Show resolved Hide resolved
src/spy.js Outdated Show resolved Hide resolved
Co-authored-by: Oskar Karlsson <oskar.karlsson@hey.com>
@marvinhagemeister
Copy link
Contributor Author

@tjoskar Love these improvements! Spoke with @lukeed in a few weeks ago and he wasn't sure about the approach chosen in this PR and was looking more for something proxy based. So I'm not sure if this PR will get mainlined at all.

@43081j
Copy link

43081j commented Apr 4, 2021

fwiw there's also: https://github.com/43081j/hanbi

apologies for the self-promotion but it is rather tiny and more complete than what exists here, if you don't want to maintain your own. there's a few similar libraries around, too.

i'd be happy to do some uvu integration for it if that'd help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add uvu/spy package
5 participants