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

Using invocation verification with QUnit #304

Closed
iamdtang opened this issue Nov 20, 2017 · 12 comments
Closed

Using invocation verification with QUnit #304

iamdtang opened this issue Nov 20, 2017 · 12 comments

Comments

@iamdtang
Copy link

Hey Justin, thanks for the library! Love it!

I'm currently using testdouble with QUnit via Ember. QUnit expects each test to have at least one assertion, but it doesn't recognize td.verify() as an assertion. A few work around are:

  1. Put assert.expect(0) at the top of the test. I don't like this because td.verify() is an assertion and this is saying there are no assertions.
  2. Put assert.ok(true) after td.verify().

I considered doing something like assert.ok(td.verify(...)), but td.verify() returns undefined. This approach seems the most clear if td.verify() returned something truthy. Have you heard of anyone experiencing this issue and how they solved it? If not, what are your thoughts on having td.verify() return true when the assertion passes?

@searls
Copy link
Member

searls commented Nov 20, 2017

I would recommend creating a test helper that wraps td.verify to do what you want and does something like this in a suite-wide test helper file:

global.td = require('testdouble')
const originalVerify = td.verify
td.verify = function(...args) {
  originalVerify.apply(this, args)
  assert.ok(true)
}

If you can get that to work, you should consider publishing a qunit plugin similar to testdouble-jasmine and testdouble-chai to help others!

@searls searls closed this as completed Nov 20, 2017
@iamdtang
Copy link
Author

thanks Justin!

@alexlafroscia
Copy link
Contributor

alexlafroscia commented Mar 27, 2018

I added a QUnit assertion that internally calls td.verify for this purpose.

In a "test setup" file, I added the following:

import QUnit from 'qunit';
import td from 'testdouble';

QUnit.extend(QUnit.assert, {
  verify() {
    try {
      td.verify(...arguments);

      this.pushResult({
        result: true,
        message: 'Stub passed verification'
      });
    } catch ({ message }) {
      this.pushResult({
        result: false,
        message
      });
    }
  }
});

This allows me to use assert.verify in my tests with the same ergonomics as td.verify, but it automatically shows the rich TestDouble verification error message in the QUnit UI if the verification failed. I also don't have to do an assert.expect(0) because both the pass and fail cases clue QUnit into the fact that an assertion was made.

Another nice bit of this approach is that a failing verification doesn't blow up the rest of the test; it'll continue to make other assertions that might happen after the td.verify

screen shot 2018-03-27 at 4 28 52 pm

@searls
Copy link
Member

searls commented Mar 28, 2018

Might it make sense to extract this into a testdouble-qunit module? Similar to testdouble-chai or testdouble-jasmine?

@alexlafroscia
Copy link
Contributor

I was thinking the same. I can give that a shot in the next few days

@alexlafroscia
Copy link
Contributor

@searls
Copy link
Member

searls commented Mar 28, 2018

Hey @alexlafroscia, a few points of feedback you might be able to work into that package (github)

  1. Any chance you can eliminate the 3 hard deps on ember/broccoli stuff? That's going to blow up the install size/time for non-ember-cli users

  2. Currently you're importing testdouble, but AFAICT you aren't actually depending on it anywhere (though your devDep on the ember-cli-testdouble extension is probably bringing this along for you). Instead, I would recommend you expose a public interface that received the user's testdouble object as an argument to initialize itself, so that you can both have certain access to a td instance without marrying the plugin to any particular version range of testdouble. This is how testdouble-jest, testdouble-chai, and testdouble-jasmine

It'd be super fantastic if testdouble-qunit could accomplish both of these things

@alexlafroscia
Copy link
Contributor

Sure, I can split it out into a more generic package and an Ember addon. I modeled the approach after qunit-dom which does essentially the same thing, shipping as an Ember addon by default (since QUnit is primarily used with Ember apps) but contains a library that could be used independently as well

@alexlafroscia
Copy link
Contributor

Just to follow up, it's now two separate repos:

  • testdouble-qunit doesn't have a direct dependency on with QUnit or test-double and instead expects to have them passed in as arguments
  • ember-cli-testdouble-qunit wraps it and packages it for consumption by an Ember app

There's a whole documentation site here:

https://alexlafroscia.com/testdouble-qunit

@searls
Copy link
Member

searls commented Apr 20, 2018

Fantastic! Thanks for aping the style of the other plugins!

@searls
Copy link
Member

searls commented Apr 20, 2018

Threw it in the README 8ba9ba0

@alexlafroscia
Copy link
Contributor

Sure thing! I did notice that I unintentionally swapped the argument order that the others use in some cases. Do you have a preference on consistency with other tools? I'm happy to change it if you'd rather.

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

3 participants