Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed Aug 11, 2021
1 parent 0bdf3c6 commit 0a136d3
Show file tree
Hide file tree
Showing 14 changed files with 1,553 additions and 2,027 deletions.
13 changes: 7 additions & 6 deletions addon-test-support/assertions/expect-deprecation.js
@@ -1,8 +1,8 @@
import { getDeprecationsForCallback, getDeprecations } from '@ember/test-helpers';
import { getDeprecationsDuringCallback, getDeprecations } from '@ember/test-helpers';
import checkMatcher from './utils/check-matcher';

export default function expectDeprecation(cb, matcher) {
const test = deprecations => {
const test = (deprecations, matcher) => {
const matchedDeprecations = deprecations.filter(deprecation => {
return checkMatcher(deprecation.message, matcher);
});
Expand All @@ -16,13 +16,14 @@ export default function expectDeprecation(cb, matcher) {
}

if (typeof cb !== 'function') {
test(getDeprecations());
// cb is not a callback, so we assume it is the matcher
test(getDeprecations(), cb);
} else {
const maybeThenable = getDeprecationsForCallback(cb);
const maybeThenable = getDeprecationsDuringCallback(cb);
if (maybeThenable !== null && typeof maybeThenable === 'object' && typeof maybeThenable.then === 'function') {
return maybeThenable.then(test);
return maybeThenable.then(deprecations => test(deprecations, matcher));
} else {
test(maybeThenable);
test(maybeThenable, matcher);
}
}

Expand Down
29 changes: 29 additions & 0 deletions addon-test-support/assertions/expect-no-deprecation.js
@@ -0,0 +1,29 @@
import checkMatcher from './utils/check-matcher';
import { getDeprecations, getDeprecationsDuringCallback } from '@ember/test-helpers';

export default function expectNoDeprecation(cb, matcher) {
const test = (deprecations, matcher) => {
const matchedDeprecations = deprecations.filter(deprecation => {
return checkMatcher(deprecation.message, matcher);
});

this.pushResult({
result: matchedDeprecations.length === 0,
actual: matchedDeprecations,
expected: [],
message: 'Expected no deprecations during test, but deprecations were found.',
});
}

if (typeof cb !== 'function') {
// cb is not a callback, so we assume it is the matcher
test(getDeprecations(), cb);
} else {
const maybeThenable = getDeprecationsDuringCallback(cb);
if (maybeThenable !== null && typeof maybeThenable === 'object' && typeof maybeThenable.then === 'function') {
return maybeThenable.then(deprecations => test(deprecations, matcher));
} else {
test(maybeThenable, matcher);
}
}
}
44 changes: 38 additions & 6 deletions addon-test-support/assertions/expect-no-runloop.js
@@ -1,27 +1,59 @@
import { end, _currentRunloop, _hasScheduledTimers, _cancelTimers } from '@ember/runloop';
import { run, end, _getCurrentRunLoop, _hasScheduledTimers, _cancelTimers } from '@ember/runloop';

function getCurrentRunLoop() {
// Ember 3.24.4 does not have _getCurrentRunLoop, but does have run.currentRunLoop;
if ('currentRunLoop' in run) {
return run.currentRunLoop;
} else {
return _getCurrentRunLoop();
}
}

// TODO: It seems very odd to mix runloop + timers into a runloop
// specific assertion.
//
// We should likely:
//
// * have timer specific expectations
// * have runloop specific expectations
// * not have either cancel timers or runloop, rather those should
// be the explicitly choosen by the user
export default function expectNoRunloop() {
if (_currentRunLoop) {
if (getCurrentRunLoop()) {
this.pushResult({
result: false,
actual: run.currentRunLoop,
actual: getCurrentRunLoop(),
expected: null,
message: 'Should not be in a run loop at end of test'
message: 'expected no active runloop'
});

while (_currentRunLoop) {
while (getCurrentRunLoop()) {
end();
}
} else {
this.pushResult({
result: true,
actual: null,
expected: null,
message: 'expected no active runloop'
});
}

if (_hasScheduledTimers()) {
this.pushResult({
result: false,
actual: true,
expected: false,
message: 'Ember run should not have scheduled timers at end of test'
message: 'expected no active timers'
});

_cancelTimers();
} else {
this.pushResult({
result: true,
actual: false,
expected: false,
message: 'expected no active timers'
});
}
}
@@ -1,11 +1,9 @@
import { getWarnings, getWarningsDuringCallback } from '@ember/test-helpers';

export default function expectNoWarnings(callback) {
export default function expectNoWarning(callback) {
const warnings = typeof callback === 'function' ? getWarningsDuringCallback(callback) : getWarnings();

let warningStr = warnings.reduce((a, b) => {
return `${b}${a.message}\n`;
}, '');
const warningStr = warnings.reduce((a, b) => `${b}${a.message}\n`, '');

this.pushResult({
result: warnings.length === 0,
Expand Down
@@ -1,7 +1,7 @@
import checkMatcher from './utils/check-matcher';
import { getWarningsDuringCallback } from '@ember/test-helpers';
import { getWarningsDuringCallback, getWarnings } from '@ember/test-helpers';

export default function expectWarnings(callback, matcher) {
export default function expectWarning(callback, matcher) {
let warnings;
if (typeof callback === 'function') {
warnings = getWarningsDuringCallback(callback);
Expand All @@ -10,7 +10,7 @@ export default function expectWarnings(callback, matcher) {
warnings = getWarnings();
}

const matchedWarnings = warnings.filter(warning => checkMatcher(warning.message, matcher);
const matchedWarnings = warnings.filter(warning => checkMatcher(warning.message, matcher));

this.pushResult({
result: matchedWarnings.length !== 0,
Expand Down
11 changes: 0 additions & 11 deletions addon-test-support/assertions/no-deprecations.js

This file was deleted.

7 changes: 6 additions & 1 deletion addon-test-support/assertions/utils/check-matcher.js
@@ -1,4 +1,9 @@
export function checkMatcher(message, matcher) {
function includes(message, search) {
return message.includes ? message.includes(search) : message.indexOf(search) !== -1;
}

// TODO: test
export default function checkMatcher(message, matcher) {
if (typeof matcher === 'string') {
return includes(message, matcher);
} else if (matcher instanceof RegExp) {
Expand Down
23 changes: 13 additions & 10 deletions addon-test-support/index.js
Expand Up @@ -27,19 +27,18 @@ import { installTestNotIsolatedHook } from './test-isolation-validation';

let waitForSettled = true;

import deprecationsInclude from './asserts/deprecations-include';
import deprecations from './asserts/deprecations';
import noDeprecations from './asserts/no-deprecations';
import expectDeprecation from './asserts/expect-deprecation';
import expectNoRunloop from './asserts/expect-no-runloop';
import expectWarning from './asserts/expect-warning';

export function setup(assert) {
import deprecationsInclude from './assertions/deprecations-include';
import deprecations from './assertions/deprecations';
import expectNoDeprecation from './assertions/expect-no-deprecation';
import expectDeprecation from './assertions/expect-deprecation';
import expectNoRunloop from './assertions/expect-no-runloop';
// import expectWarning from './assertions/expect-warning';
//
export function setupAsserts(assert) {
// TODO: decide which of these we should keep, which depreacte and which drop.
assert.deprecationsInclude = deprecationsInclude;
assert.deprecations = deprecations;
assert.expectNoDeprecations = noDeprecations; // compat
assert.noDeprecations = noDeprecations;
assert.expectNoDeprecation = expectNoDeprecation;
assert.expectDeprecation = expectDeprecation; // compat
assert.expectNoRunloop = expectNoRunloop; // compat but fixed name
// around for compat
Expand Down Expand Up @@ -241,5 +240,9 @@ export function start(options = {}) {
startTests();
}

if (options.setupAssertAdditions !== false) {
setupAsserts(QUnit.assert);
}

setupResetOnerror();
}
1 change: 1 addition & 0 deletions server/index.js
@@ -0,0 +1 @@
module.exports = (app) => app.get('/', (_, res) => res.redirect('/tests'));
4 changes: 2 additions & 2 deletions tests/unit/assertions/assertion-test.js
@@ -1,8 +1,8 @@
import { module, test } from 'qunit';
import expectAssertion from 'ember-qunit/assertions/expect-assertion';
// import expectAssertion from 'ember-qunit/assertions/expect-assertion';
import { assert as emberAssert } from '@ember/debug';

module('expectAssertion', function(hooks) {
module.skip('expectAssertion', function(hooks) {
let mockAssert;

hooks.beforeEach(() => {
Expand Down

0 comments on commit 0a136d3

Please sign in to comment.