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.
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...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With