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

How should we be using t.plan and t.end in our tests? #12

Open
samhstn opened this issue Feb 9, 2017 · 2 comments
Open

How should we be using t.plan and t.end in our tests? #12

samhstn opened this issue Feb 9, 2017 · 2 comments

Comments

@samhstn
Copy link
Member

samhstn commented Feb 9, 2017

I've been noticing a difference in the way we are using t.plan and t.end in our modules and in Dwyl's Science Museum Project

What are the merits of using t.plan along with t.end vs just using t.end (in most scenarios) and which pattern should we be following when writing tape tests and why?

@alanshaw I know you like using this pattern. What benefits have you found from the additional test planning.

@alanshaw
Copy link
Member

alanshaw commented Mar 15, 2017

It's a way of verifying the assertions you wanted to make were made.

Without t.plan it's possible (for example when juggling multiple async operations) to accidentally end a test before you've made any assertions - the test passes, but possibly shouldn't have. By using t.plan in combination with t.end you ensure this doesn't happen.

A contrived example:

Imagine if you were testing Async.each, without t.plan:

const Async = require('async')
const test = require('tape')

test('should call iterator function for each element', (t) => {
  const data = [1, 2, 3]
  const iteratee = (d, cb) => { t.ok(d); cb() }
  const callback = () => t.end()
  Async.each(data, iteratee, callback)
})

If Async.each is buggy and does not call the iterator function for each element, but does call the callback function then the test will pass - a false positive. Using t.plan(data.length) can prevent this.

@nelsonic
Copy link
Member

This is worth adding to the readme in a "FAQ" section.

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

No branches or pull requests

3 participants