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

What's the best way to use testdouble.js with tape? #93

Closed
d6u opened this issue Mar 31, 2016 · 10 comments
Closed

What's the best way to use testdouble.js with tape? #93

d6u opened this issue Mar 31, 2016 · 10 comments

Comments

@d6u
Copy link

d6u commented Mar 31, 2016

I saw #33 but it's quite verbose to use testdouble with tape:

const test = require('tape');
const td = require('testdouble');

test('verify invoked', function(t) {
  t.plan(1)

  var stub = td.create()

  stub('any arguments', 0, 1, 2)

  t.doesNotThrow(function() {
    td.verify(stub0())
  }, 'called with more arguments when none are specified')
})

Anyway to not pass a callback to t.doesNotThrow?

Thanks!

@searls
Copy link
Member

searls commented Apr 1, 2016

Forgive my naivety, but what does tape do when a function passed to test throws an error? I presume it fails the test, right? If so, why not:

const test = require('tape');
const td = require('testdouble');

test('verify invoked', function(t) {
  t.plan(1)

  var stub = td.create()

  stub('any arguments', 0, 1, 2)

  td.verify(stub())
})

@d6u
Copy link
Author

d6u commented Apr 1, 2016

@searls Because we called t.plan(1), tape expect have one assertion method called, which was t.doesNotThrow. Since we are not calling assertion in your example, tape will complaint about it. The rest should work though.

@Schoonology
Copy link
Contributor

@d6u, can you use t.end() at the end of your test in lieu of using t.plan() at the beginning?

@d6u
Copy link
Author

d6u commented Apr 1, 2016

Just tried. t.end() doesn't count towards plan.

@Schoonology
Copy link
Contributor

That's right. I meant more like this (dropping t.plan completely, as explicitly calling t.end works the same way):

const test = require('tape');
const td = require('testdouble');

test('verify invoked', function(t) {
  var stub = td.create()

  stub('any arguments', 0, 1, 2)

  td.verify(stub())

  t.end()
})

Does that work for your situation?

@d6u
Copy link
Author

d6u commented Apr 1, 2016

This should work. Thanks. To push this further, could testdouble.js hook up with tape and let tape count each verify() towards plan? If this is not possible now, what does it take to develop a plug in myself?

@searls
Copy link
Member

searls commented Apr 1, 2016

We have a couple other plugins that hook verify into assertion libs and I suspect this should also be possible. See testdouble-chai and testdouble-jasmine for reference

@d6u
Copy link
Author

d6u commented Apr 1, 2016

Thanks

@d6u d6u closed this as completed Apr 1, 2016
@searls
Copy link
Member

searls commented Apr 1, 2016

Because td.verify throws an error, one option might be to use tape-catch: https://github.com/michaelrhodes/tape-catch

@customcommander
Copy link

customcommander commented Nov 10, 2021

If you really want to use t.plan(…) then I suggest you also use t.pass(…):

const test = require('tape');
const td = require('testdouble');
const SUT = require(/* … */); // Subject Under Test

test('Hello World!', t => {
  t.plan(1);
  const fn = td.func();
  SUT(fn, 42);
  td.verify(fn(42));
  t.pass('fn was applied to 42'); // <-- Counts towards t.plan
  t.end();
});

TAP Output:

# Hello World!
ok 1 fn was applied to 42

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

No branches or pull requests

4 participants