UNPKG

2.21 kBJavaScriptView Raw
1import Ember from 'ember';
2import * as QUnit from 'qunit';
3import hasEmberVersion from '@ember/test-helpers/has-ember-version';
4
5function unhandledRejectionAssertion(current, error) {
6 let message, source;
7
8 if (typeof error === 'object' && error !== null) {
9 message = error.message;
10 source = error.stack;
11 } else if (typeof error === 'string') {
12 message = error;
13 source = 'unknown source';
14 } else {
15 message = 'unhandledRejection occured, but it had no message';
16 source = 'unknown source';
17 }
18
19 current.assert.pushResult({
20 result: false,
21 actual: false,
22 expected: true,
23 message: message,
24 source: source,
25 });
26}
27
28export function nonTestDoneCallback() {}
29
30let Adapter = Ember.Test.Adapter.extend({
31 init() {
32 this.doneCallbacks = [];
33 this.qunit = this.qunit || QUnit;
34 },
35
36 asyncStart() {
37 let currentTest = this.qunit.config.current;
38 let done =
39 currentTest && currentTest.assert
40 ? currentTest.assert.async()
41 : nonTestDoneCallback;
42 this.doneCallbacks.push({ test: currentTest, done });
43 },
44
45 asyncEnd() {
46 let currentTest = this.qunit.config.current;
47
48 if (this.doneCallbacks.length === 0) {
49 throw new Error(
50 'Adapter asyncEnd called when no async was expected. Please create an issue in ember-qunit.'
51 );
52 }
53
54 let { test, done } = this.doneCallbacks.pop();
55
56 // In future, we should explore fixing this at a different level, specifically
57 // addressing the pairing of asyncStart/asyncEnd behavior in a more consistent way.
58 if (test === currentTest) {
59 done();
60 }
61 },
62
63 // clobber default implementation of `exception` will be added back for Ember
64 // < 2.17 just below...
65 exception: null,
66});
67
68// Ember 2.17 and higher do not require the test adapter to have an `exception`
69// method When `exception` is not present, the unhandled rejection is
70// automatically re-thrown and will therefore hit QUnit's own global error
71// handler (therefore appropriately causing test failure)
72if (!hasEmberVersion(2, 17)) {
73 Adapter = Adapter.extend({
74 exception(error) {
75 unhandledRejectionAssertion(QUnit.config.current, error);
76 },
77 });
78}
79
80export default Adapter;