Skip to content

Commit 3ec518d

Browse files
committed
WIP: expectAssertion updates
1 parent 884baf3 commit 3ec518d

5 files changed

Lines changed: 74 additions & 79 deletions

File tree

addon-test-support/asserts/assertion.js

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,38 @@ let TestAdapter = QUnitAdapter.extend({
1212

1313
let noop = () => {};
1414

15-
function reset(origTestAdapter, origLoggerError) {
15+
let cleanup = (origTestAdapter, origLoggerError) => {
1616
// Cleanup the test adapter and restore the original.
17-
Ember.run(() => {
17+
return Ember.run(() => {
1818
Ember.Test.adapter.destroy();
1919
Ember.Test.adapter = origTestAdapter;
2020
Ember.Logger.error = origLoggerError;
2121
});
22-
}
22+
};
2323

24-
function handleError(syncErrorInCallback) {
25-
let error = syncErrorInCallback || Ember.Test.adapter.lastError;
26-
let isEmberError = error instanceof Ember.Error;
27-
let matches = Boolean(isEmberError && checkMatcher(error.message, matcher));
24+
let handleError = (context, error, matcher, isProductionBuild) => {
25+
let isEmberError = error instanceof Ember.Error;
26+
let matches = Boolean(isEmberError && checkMatcher(error.message, matcher));
27+
let errObj = {};
2828

29-
if (isProductionBuild) {
30-
this.pushResult({
31-
result: true,
32-
actual: null,
33-
expected: null,
34-
message: 'Assertions are disabled in production builds.'
35-
});
36-
} else {
37-
this.pushResult({
38-
result: isEmberError && matches,
39-
actual: error && error.message,
40-
expected: matcher,
41-
message: matcher ? 'Ember.assert matched specific message' : 'Ember.assert called with any message'
42-
});
43-
}
44-
}
29+
if (isProductionBuild) {
30+
errObj = {
31+
result: true,
32+
actual: null,
33+
expected: null,
34+
message: 'Assertions are disabled in production builds.'
35+
};
36+
} else {
37+
errObj = {
38+
result: isEmberError && matches,
39+
actual: error && error.message,
40+
expected: matcher,
41+
message: matcher ? 'Ember.assert matched specific message' : 'Ember.assert called with any message'
42+
};
43+
}
44+
45+
context.pushResult(errObj);
46+
};
4547

4648
export default function() {
4749
let isProductionBuild = (function() {
@@ -71,13 +73,25 @@ export default function() {
7173
error = e;
7274
}
7375

74-
if (result && typeof result.then === 'function') {
76+
if (error) {
77+
handleError(this, error, matcher, isProductionBuild);
78+
} else if (Ember.Test.adapter.lastError) {
79+
handleError(this, Ember.Test.adapter.lastError, matcher, isProductionBuild);
80+
} else if(result && typeof result === 'object' && result !== null && typeof result.then === 'function') {
7581
return result
76-
.then(null, () => handleError(error))
82+
.then(() => {
83+
if (Ember.Test.adapter.lastError) {
84+
handleError(this, Ember.Test.adapter.lastError, matcher, isProductionBuild);
85+
} else {
86+
handleError(this, null, matcher, null);
87+
}
88+
})
89+
.catch(() => handleError(this, error, matcher, isProductionBuild))
7790
.finally(() => cleanup(origTestAdapter, origLoggerError));
91+
} else {
92+
handleError(this, null, matcher, null);
7893
}
79-
8094

81-
cleanup(origTestAdapter, origLoggerError);
95+
return cleanup(origTestAdapter, origLoggerError);
8296
};
8397
}

config/ember-try.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-env node */
22
module.exports = {
3+
useYarn: true,
34
scenarios: [
45
{
56
name: 'ember-lts-2.8',

tests/helpers/module-for-assert.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

tests/helpers/setup-assert-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import QUnit from 'qunit';
2+
3+
export default function setupAssertTest(hooks) {
4+
hooks.beforeEach(function() {
5+
let originalPushResult = QUnit.assert.pushResult;
6+
this.pushedResults = [];
7+
8+
QUnit.assert.pushResult = (result) => {
9+
this.pushedResults.push(result);
10+
};
11+
12+
this.restoreAsserts = () => {
13+
if (originalPushResult) {
14+
QUnit.assert.pushResult = originalPushResult;
15+
originalPushResult = null;
16+
}
17+
};
18+
});
19+
20+
hooks.afterEach(function() {
21+
this.restoreAsserts();
22+
});
23+
}

tests/integration/assertion-test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ module('Assertion', function(hooks) {
1818
}));
1919
});
2020

21-
test('Check for assert', function(assert) {
22-
assert.expectAssertion(() => {
23-
render(hbs`{{x-assert-test}}`);
21+
test('Check for assert', async function(assert) {
22+
await assert.expectAssertion(() => {
23+
return render(hbs`{{x-assert-test}}`);
2424
}, /x-assert-test will always assert/);
2525

2626
// Restore the asserts (removes the mocking)
2727
this.restoreAsserts();
2828

29-
assert.ok(this.pushedResults[0].result, 'properly catured assertion');
29+
assert.ok(this.pushedResults[0].result, 'properly captured assertion');
3030
});
3131

32-
test('Check for async assert', function(assert) {
32+
test('Check for async assert', async function(assert) {
3333
this.owner.register('component:x-assert-async-test', Ember.Component.extend({
3434
init() {
3535
this._super();
@@ -43,14 +43,14 @@ module('Assertion', function(hooks) {
4343
}
4444
}));
4545

46-
assert.expectAssertion(async function() {
47-
await render(hbs`{{x-assert-test}}`);
48-
}, /x-assert-test will always assert/);
46+
await assert.expectAssertion(() => {
47+
return render(hbs`{{x-assert-async-test}}`);
48+
}, /x-assert-async-test will asynchronously assert/);
4949

5050
// Restore the asserts (removes the mocking)
5151
this.restoreAsserts();
5252

53-
assert.ok(this.pushedResults[0].result, '`expectWarning` captured warning call');
53+
assert.ok(this.pushedResults[0].result, 'properly captured an async assertion');
5454
});
5555

5656
test('Does not log caught assertions', function(assert) {

0 commit comments

Comments
 (0)