Skip to content

Commit

Permalink
[CHORE] allow asserting all tests for deprecations (emberjs#6627)
Browse files Browse the repository at this point in the history
* [CHORE] configure the ability to filter deprecations from assertNoDeprecations

* turn off helpers until tests are fixed
  • Loading branch information
runspired authored and snewcomer committed Nov 9, 2019
1 parent c821e74 commit 3c4a2ce
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 27 deletions.
20 changes: 19 additions & 1 deletion packages/-ember-data/tests/helpers/deprecated-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { test } from 'qunit';
import VERSION from 'ember-data/version';
import { DEBUG } from '@glimmer/env';

// temporary so that we can split out test fixes
// from landing this. If working locally turn this
// on to have tests fail that require being fixed.
export const SHOULD_ASSERT_ALL = false;

// small comparison function for major and minor semver values
function gte(EDVersion, DeprecationVersion) {
Expand All @@ -22,8 +28,20 @@ export function deprecatedTest(testName, deprecation, testCallback) {
throw new Error(`deprecatedTest expects { id } to be a meaningful string`);
}

async function interceptor(assert) {
await testCallback.call(this, assert);
if (DEBUG) {
if (SHOULD_ASSERT_ALL) {
if (typeof assert.test.expected === 'number') {
assert.test.expected += 1;
}
assert.expectDeprecation(deprecation);
}
}
}

if (gte(VERSION, deprecation.until)) {
test(`DEPRECATION ${deprecation.id} until ${deprecation.until} | ${testName}`, testCallback);
test(`DEPRECATION ${deprecation.id} until ${deprecation.until} | ${testName}`, interceptor);
} else {
test(`DEPRECATION ${deprecation.id} until ${deprecation.until} | ${testName}`, function(assert) {
if (deprecation.refactor === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function verifyDeprecation(config: DeprecationConfig, label?: string): AssertSom
return isMatched;
});
DEPRECATIONS_FOR_TEST = DEPRECATIONS_FOR_TEST.filter(deprecation => {
matchedDeprecations.indexOf(deprecation) === -1;
return matchedDeprecations.indexOf(deprecation) === -1;
});
HANDLED_DEPRECATIONS_FOR_TEST.push(...matchedDeprecations);

Expand All @@ -75,9 +75,18 @@ function verifyDeprecation(config: DeprecationConfig, label?: string): AssertSom
};
}

function verifyNoDeprecation(label?: string): AssertNoneResult {
const UNHANDLED_DEPRECATIONS = DEPRECATIONS_FOR_TEST;
DEPRECATIONS_FOR_TEST = [];
function verifyNoDeprecation(filter?: (deprecation: FoundDeprecation) => boolean, label?: string): AssertNoneResult {
let UNHANDLED_DEPRECATIONS;

if (filter) {
UNHANDLED_DEPRECATIONS = DEPRECATIONS_FOR_TEST.filter(filter);
DEPRECATIONS_FOR_TEST = DEPRECATIONS_FOR_TEST.filter(deprecation => {
return UNHANDLED_DEPRECATIONS.indexOf(deprecation) === -1;
});
} else {
UNHANDLED_DEPRECATIONS = DEPRECATIONS_FOR_TEST;
DEPRECATIONS_FOR_TEST = [];
}

let deprecationStr = UNHANDLED_DEPRECATIONS.reduce((a, b) => {
return `${a}${b.message}\n`;
Expand Down Expand Up @@ -149,10 +158,15 @@ export function configureDeprecationHandler() {

let result = verifyDeprecation(config, label);
this.pushResult(result);
DEPRECATIONS_FOR_TEST = origDeprecations.concat(DEPRECATIONS_FOR_TEST);
if (callback) {
DEPRECATIONS_FOR_TEST = origDeprecations.concat(DEPRECATIONS_FOR_TEST);
}
};

QUnit.assert.expectNoDeprecation = async function(cb, label?: string) {
QUnit.assert.expectNoDeprecation = async function(
cb?: () => unknown,
label?: string,
filter?: (deprecation: FoundDeprecation) => boolean
) {
let origDeprecations = DEPRECATIONS_FOR_TEST;

if (cb) {
Expand All @@ -163,7 +177,7 @@ export function configureDeprecationHandler() {
}
}

let result = verifyNoDeprecation(label);
let result = verifyNoDeprecation(filter, label);
this.pushResult(result);
DEPRECATIONS_FOR_TEST = origDeprecations.concat(DEPRECATIONS_FOR_TEST);
};
Expand Down
6 changes: 3 additions & 3 deletions packages/-ember-data/tests/helpers/test-in-debug.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DEBUG } from '@glimmer/env';
import { test, skip } from 'qunit';

export default function testInDebug() {
export default function testInDebug(label, callback) {
if (DEBUG) {
test(...arguments);
test(`[DEBUG-ONLY] ${label}`, callback);
} else {
skip(...arguments);
skip(`[DEBUG-ONLY] ${label}`, callback);
}
}
49 changes: 34 additions & 15 deletions packages/-ember-data/tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,60 @@ import config from '../config/environment';
import RSVP from 'rsvp';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';
import { DEBUG } from '@glimmer/env';

import QUnit from 'qunit';
import DS from 'ember-data';
import { wait, asyncEqual, invokeAsync } from 'dummy/tests/helpers/async';
import configureAsserts from 'dummy/tests/helpers/qunit-asserts';
import { SHOULD_ASSERT_ALL } from './helpers/deprecated-test';

configureAsserts();

setApplication(Application.create(config.APP));

const { assert } = QUnit;
const transforms = {
boolean: DS.BooleanTransform.create(),
date: DS.DateTransform.create(),
number: DS.NumberTransform.create(),
string: DS.StringTransform.create(),
};

QUnit.begin(() => {
function assertAllDeprecations(assert) {
if (typeof assert.test.expected === 'number') {
assert.test.expected += 1;
}
assert.expectNoDeprecation(undefined, undefined, deprecation => {
// only assert EmberData deprecations
const id = deprecation.options.id;
const isEmberDataDeprecation = id.includes('DS') || id.includes('EmberData') || id.includes('ember-data');

if (!isEmberDataDeprecation) {
// eslint-disable-next-line no-console
console.log('Detected Non-Ember-Data Deprecation', deprecation);
}

return isEmberDataDeprecation;
});
}
// ensure we don't regress quietly
// this plays nicely with `expectDeprecation`
if (DEBUG) {
QUnit.config.modules.forEach(mod => {
const hooks = (mod.hooks.afterEach = mod.hooks.afterEach || []);

if (mod.tests.length !== 0) {
if (SHOULD_ASSERT_ALL) {
hooks.unshift(assertAllDeprecations);
}
}
});
}

RSVP.configure('onerror', reason => {
// only print error messages if they're exceptions;
// otherwise, let a future turn of the event loop
// handle the error.
// TODO kill this off
if (reason && reason instanceof Error) {
throw reason;
}
});

// Prevent all tests involving serialization to require a container
// TODO kill the need for this
DS.JSONSerializer.reopen({
transformFor(attributeType) {
return this._super(attributeType, true) || transforms[attributeType];
},
});
});

assert.wait = wait;
Expand Down

0 comments on commit 3c4a2ce

Please sign in to comment.