Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I setup a QUnit hook to run before all tests in a suite?

I'm using ember-qunit and have a service in my app that makes some nontrivial api calls. To handle this, I'm using a test helper:

// tests/helpers/mock-my-service.js
import { mock } from 'ember-data-factory-guy';

export function stubMyService(hooks) {
  hooks.beforeEach(function() {
    mock({
      type: 'GET',
      url: 'https://example.com',
      responseText: [
        { some: 'complex data' }
      ],
      status: 200
    });
  });
}

// tests/some-test.js
import { stubMyService } from 'helpers/mock-my-service';

module('Integration | Component | Whatever', function(hooks) {
  stubMyService(hooks);
  ...
}

Recently a feature required this service to be used in a fairly high level location in the app, meaning that I'm now saying stubMyService(hooks); in almost every test. This means that I'd have to include this helper for all tests from now on. Is there a way to include hooks globally? For example RSpec has:

config.before(:suite) do
  # runs before entire suite
end

I'd love to be able to do something like

// tests/test-helper.js
import { stubMyService } from 'helpers/mock-my-service';

QUnit.module.beforeSuite(function(hooks) {
  stubMyService(hooks);
});

Is there a good way to do this? Or is there a more qunity way of approacing this? Does ember-qunit have it's own way of doing this? I don't see anything in the documentation allowing for this.

like image 654
Glyoko Avatar asked Nov 07 '22 10:11

Glyoko


1 Answers

Not sure if this works for your needs, but QUnit does have a global event/callback system for tests and modules. So you could try to do this in that:

QUnit.testStart( ( { module, name } ) => {
  mock({
    type: 'GET',
    url: 'https://example.com',
    responseText: [
      { some: 'complex data' }
    ],
    status: 200
  });
});

(There's also a testDone callback for tearing down that mock...)

like image 194
Jordan Kasper Avatar answered Nov 12 '22 11:11

Jordan Kasper