From 17a1e9dae530704c6bf363be35008119f9ef2e92 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Sat, 26 Mar 2016 21:36:23 -0600 Subject: [PATCH 1/6] WIP: rename same to deepEqual --- lib/assert.js | 26 ++++++-- readme.md | 6 +- test/assert.js | 64 ++++++++++++++----- .../circular-reference-on-assertion.js | 2 +- test/fixture/pkg-conf/defaults/test.js | 2 +- test/fixture/pkg-conf/pkg-overrides/actual.js | 2 +- test/fixture/pkg-conf/precedence/c.js | 4 +- test/fixture/serial.js | 2 +- test/hooks.js | 4 +- test/test.js | 8 +-- 10 files changed, 86 insertions(+), 34 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index 6ce7122db..b82e4bcc3 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -60,12 +60,12 @@ x.not = function (val, expected, msg) { test(val !== expected, create(val, expected, '!==', msg, x.not)); }; -x.same = function (val, expected, msg) { - test(deepEqual(val, expected), create(val, expected, '===', msg, x.same)); +x.deepEqual = function (val, expected, msg) { + test(deepEqual(val, expected), create(val, expected, '===', msg, x.deepEqual)); }; -x.notSame = function (val, expected, msg) { - test(!deepEqual(val, expected), create(val, expected, '!==', msg, x.notSame)); +x.notDeepEqual = function (val, expected, msg) { + test(!deepEqual(val, expected), create(val, expected, '!==', msg, x.notDeepEqual)); }; x.throws = function (fn, err, msg) { @@ -139,3 +139,21 @@ x.regex = function (contents, regex, msg) { x.ifError = x.error = function (err, msg) { test(!err, create(err, 'Error', '!==', msg, x.ifError)); }; + +/* + * deprecated APIs + */ +x.same = function (val, expected, msg) { + deprecationNotice('same()', 'deepEqual()'); + return x.deepEqual(val, expected, msg); +}; + +x.notSame = function (val, expected, msg) { + deprecationNotice('notSame()', 'notDeepEqual()'); + return x.notDeepEqual(val, expected, msg); +}; + +function deprecationNotice(oldApi, newApi) { + console.warn(`DEPRECATION NOTICE: ${oldApi} has been renamed to ${newApi} and will eventually be removed`); +} + diff --git a/readme.md b/readme.md index 382cc86bb..5a989d0e1 100644 --- a/readme.md +++ b/readme.md @@ -51,7 +51,7 @@ Translations: [EspaƱol](https://github.com/sindresorhus/ava-docs/blob/master/es import test from 'ava'; test(t => { - t.same([1, 2], [1, 2]); + t.deepEqual([1, 2], [1, 2]); }); ``` @@ -762,11 +762,11 @@ Assert that `value` is equal to `expected`. Assert that `value` is not equal to `expected`. -### `.same(value, expected, [message])` +### `.deepEqual(value, expected, [message])` Assert that `value` is deep equal to `expected`. -### `.notSame(value, expected, [message])` +### `.notDeepEqual(value, expected, [message])` Assert that `value` is not deep equal to `expected`. diff --git a/test/assert.js b/test/assert.js index c02b46d36..412459a0a 100644 --- a/test/assert.js +++ b/test/assert.js @@ -119,51 +119,51 @@ test('.not()', function (t) { t.end(); }); -test('.same()', function (t) { +test('.deepEqual()', function (t) { t.doesNotThrow(function () { - assert.same({a: 'a'}, {a: 'a'}); + assert.deepEqual({a: 'a'}, {a: 'a'}); }); t.doesNotThrow(function () { - assert.same(['a', 'b'], ['a', 'b']); + assert.deepEqual(['a', 'b'], ['a', 'b']); }); t.throws(function () { - assert.same({a: 'a'}, {a: 'b'}); + assert.deepEqual({a: 'a'}, {a: 'b'}); }); t.throws(function () { - assert.same(['a', 'b'], ['a', 'a']); + assert.deepEqual(['a', 'b'], ['a', 'a']); }); t.throws(function () { - assert.same([['a', 'b'], 'c'], [['a', 'b'], 'd']); + assert.deepEqual([['a', 'b'], 'c'], [['a', 'b'], 'd']); }, / 'c' ].*? 'd' ]/); t.throws(function () { var circular = ['a', 'b']; circular.push(circular); - assert.same([circular, 'c'], [circular, 'd']); + assert.deepEqual([circular, 'c'], [circular, 'd']); }, / 'c' ].*? 'd' ]/); t.end(); }); -test('.notSame()', function (t) { +test('.notDeepEqual()', function (t) { t.doesNotThrow(function () { - assert.notSame({a: 'a'}, {a: 'b'}); + assert.notDeepEqual({a: 'a'}, {a: 'b'}); }); t.doesNotThrow(function () { - assert.notSame(['a', 'b'], ['c', 'd']); + assert.notDeepEqual(['a', 'b'], ['c', 'd']); }); t.throws(function () { - assert.notSame({a: 'a'}, {a: 'a'}); + assert.notDeepEqual({a: 'a'}, {a: 'a'}); }); t.throws(function () { - assert.notSame(['a', 'b'], ['a', 'b']); + assert.notDeepEqual(['a', 'b'], ['a', 'b']); }); t.end(); @@ -259,7 +259,7 @@ test('.ifError()', function (t) { t.end(); }); -test('.same() should not mask RangeError from underlying assert', function (t) { +test('.deepEqual() should not mask RangeError from underlying assert', function (t) { var Circular = function () { this.test = this; }; @@ -268,12 +268,46 @@ test('.same() should not mask RangeError from underlying assert', function (t) { var b = new Circular(); t.throws(function () { - assert.notSame(a, b); + assert.notDeepEqual(a, b); }); t.doesNotThrow(function () { - assert.same(a, b); + assert.deepEqual(a, b); }); t.end(); }); + +test('.same() should log a deprecation notice', function (t) { + var calledWith; + var originalConsole = console.warn; + console.warn = function () { + calledWith = arguments; + }; + + assert.same({}, {}); + + t.true(calledWith[0].indexOf('DEPRECATION NOTICE') !== -1); + t.true(calledWith[0].indexOf('same()') !== -1); + t.true(calledWith[0].indexOf('deepEqual()') !== -1); + + console.warn = originalConsole; + t.end(); +}); + +test('.notSame() should log a deprecation notice', function (t) { + var calledWith; + var originalConsole = console.warn; + console.warn = function () { + calledWith = arguments; + }; + + assert.notSame({foo: 'bar'}, {bar: 'foo'}); + + t.true(calledWith[0].indexOf('DEPRECATION NOTICE') !== -1); + t.true(calledWith[0].indexOf('notSame()') !== -1); + t.true(calledWith[0].indexOf('notDeepEqual()') !== -1); + + console.warn = originalConsole; + t.end(); +}); diff --git a/test/fixture/circular-reference-on-assertion.js b/test/fixture/circular-reference-on-assertion.js index d9a0da855..3603efe86 100644 --- a/test/fixture/circular-reference-on-assertion.js +++ b/test/fixture/circular-reference-on-assertion.js @@ -3,5 +3,5 @@ import test from '../../'; test(t => { const circular = ['a', 'b']; circular.push(circular); - t.same([circular, 'c'], [circular, 'd']); + t.deepEqual([circular, 'c'], [circular, 'd']); }); diff --git a/test/fixture/pkg-conf/defaults/test.js b/test/fixture/pkg-conf/defaults/test.js index 756cd4260..b5464b732 100644 --- a/test/fixture/pkg-conf/defaults/test.js +++ b/test/fixture/pkg-conf/defaults/test.js @@ -6,5 +6,5 @@ test(t => { t.is(opts.failFast, false); t.is(opts.serial, false); t.is(opts.cacheEnabled, true); - t.same(opts.require, []); + t.deepEqual(opts.require, []); }); diff --git a/test/fixture/pkg-conf/pkg-overrides/actual.js b/test/fixture/pkg-conf/pkg-overrides/actual.js index dce9a16b1..5320272e0 100644 --- a/test/fixture/pkg-conf/pkg-overrides/actual.js +++ b/test/fixture/pkg-conf/pkg-overrides/actual.js @@ -7,7 +7,7 @@ test(t => { t.is(opts.failFast, true); t.is(opts.serial, true); t.is(opts.cacheEnabled, false); - t.same(opts.require, [ + t.deepEqual(opts.require, [ path.join(__dirname, "required.js") ]); }); diff --git a/test/fixture/pkg-conf/precedence/c.js b/test/fixture/pkg-conf/precedence/c.js index 51ef8b553..c0c75968f 100644 --- a/test/fixture/pkg-conf/precedence/c.js +++ b/test/fixture/pkg-conf/precedence/c.js @@ -7,8 +7,8 @@ test('foo', t => { t.is(opts.failFast, false); t.is(opts.serial, false); t.is(opts.cacheEnabled, true); - t.same(opts.match, ['foo*']); - t.same(opts.require, [ + t.deepEqual(opts.match, ['foo*']); + t.deepEqual(opts.require, [ path.join(__dirname, "required.js") ]); }); diff --git a/test/fixture/serial.js b/test/fixture/serial.js index fe6c7688d..29aee03d1 100644 --- a/test/fixture/serial.js +++ b/test/fixture/serial.js @@ -17,5 +17,5 @@ test.cb('second', t => { }); test(t => { - t.same(tests, ['first', 'second']); + t.deepEqual(tests, ['first', 'second']); }); diff --git a/test/hooks.js b/test/hooks.js index 8a49780e6..d438b26f3 100644 --- a/test/hooks.js +++ b/test/hooks.js @@ -270,12 +270,12 @@ test('shared context', function (t) { runner.test(function (a) { a.context.arr.push('b'); - a.same(a.context.arr, ['a', 'b']); + a.deepEqual(a.context.arr, ['a', 'b']); }); runner.afterEach(function (a) { a.context.arr.push('c'); - a.same(a.context.arr, ['a', 'b', 'c']); + a.deepEqual(a.context.arr, ['a', 'b', 'c']); }); runner.run({}).then(function () { diff --git a/test/test.js b/test/test.js index 68db6dbe1..8638f25e6 100644 --- a/test/test.js +++ b/test/test.js @@ -166,7 +166,7 @@ test('handle non-assertion errors even when planned', function (t) { test('handle testing of arrays', function (t) { var result = ava(function (a) { - a.same(['foo', 'bar'], ['foo', 'bar']); + a.deepEqual(['foo', 'bar'], ['foo', 'bar']); }).run(); t.is(result.passed, true); @@ -176,7 +176,7 @@ test('handle testing of arrays', function (t) { test('handle falsy testing of arrays', function (t) { var result = ava(function (a) { - a.notSame(['foo', 'bar'], ['foo', 'bar', 'cat']); + a.notDeepEqual(['foo', 'bar'], ['foo', 'bar', 'cat']); }).run(); t.is(result.passed, true); @@ -186,7 +186,7 @@ test('handle falsy testing of arrays', function (t) { test('handle testing of objects', function (t) { var result = ava(function (a) { - a.same({foo: 'foo', bar: 'bar'}, {foo: 'foo', bar: 'bar'}); + a.deepEqual({foo: 'foo', bar: 'bar'}, {foo: 'foo', bar: 'bar'}); }).run(); t.is(result.passed, true); @@ -196,7 +196,7 @@ test('handle testing of objects', function (t) { test('handle falsy testing of objects', function (t) { var result = ava(function (a) { - a.notSame({foo: 'foo', bar: 'bar'}, {foo: 'foo', bar: 'bar', cat: 'cake'}); + a.notDeepEqual({foo: 'foo', bar: 'bar'}, {foo: 'foo', bar: 'bar', cat: 'cake'}); }).run(); t.is(result.passed, true); From ef49c0f00fdd7d3e90744559bb15711b5e3b9529 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Mon, 4 Apr 2016 11:42:20 -0600 Subject: [PATCH 2/6] Update deprecation method to use `util.deprecate` --- lib/assert.js | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/assert.js b/lib/assert.js index b82e4bcc3..5f09b267b 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -143,17 +143,11 @@ x.ifError = x.error = function (err, msg) { /* * deprecated APIs */ -x.same = function (val, expected, msg) { - deprecationNotice('same()', 'deepEqual()'); - return x.deepEqual(val, expected, msg); -}; - -x.notSame = function (val, expected, msg) { - deprecationNotice('notSame()', 'notDeepEqual()'); - return x.notDeepEqual(val, expected, msg); -}; +x.same = util.deprecate(x.deepEqual, getDeprecationNotice('same()', 'deepEqual()')); +x.notSame = util.deprecate(x.notDeepEqual, getDeprecationNotice('notSame()', 'notDeepEqual()')); -function deprecationNotice(oldApi, newApi) { - console.warn(`DEPRECATION NOTICE: ${oldApi} has been renamed to ${newApi} and will eventually be removed`); +function getDeprecationNotice(oldApi, newApi) { + return `DEPRECATION NOTICE: ${oldApi} has been renamed to ${newApi} and will eventually be removed.` + + ` See https://github.com/jamestalmage/ava-codemods to help upgrade your codebase automatically.`; } From 55cf5c9b1e51336105d7a54a1213431744500e8e Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Mon, 4 Apr 2016 13:23:31 -0600 Subject: [PATCH 3/6] Finish fixing all tests --- index.d.ts | 4 ++-- lib/assert.js | 8 +++----- lib/enhance-assert.js | 4 ++-- test/assert.js | 34 ---------------------------------- 4 files changed, 7 insertions(+), 43 deletions(-) diff --git a/index.d.ts b/index.d.ts index eb851b2c2..ae1c9d651 100644 --- a/index.d.ts +++ b/index.d.ts @@ -120,11 +120,11 @@ export interface AssertContext { /** * Assert that value is deep equal to expected. */ - same(value: U, expected: U, message?: string): void; + deepEqual(value: U, expected: U, message?: string): void; /** * Assert that value is not deep equal to expected. */ - notSame(value: U, expected: U, message?: string): void; + notDeepEqual(value: U, expected: U, message?: string): void; /** * Assert that function throws an error or promise rejects. * @param error Can be a constructor, regex, error message or validation function. diff --git a/lib/assert.js b/lib/assert.js index 5f09b267b..18f795935 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -130,8 +130,6 @@ x.notThrows = function (fn, msg) { } }; -x.doesNotThrow = util.deprecate(x.notThrows, 't.doesNotThrow is renamed to t.notThrows. The old name still works, but will be removed in AVA 1.0.0. Update your references.'); - x.regex = function (contents, regex, msg) { test(regex.test(contents), create(regex, contents, '===', msg, x.regex)); }; @@ -143,11 +141,11 @@ x.ifError = x.error = function (err, msg) { /* * deprecated APIs */ + +x.doesNotThrow = util.deprecate(x.notThrows, getDeprecationNotice('doesNotThrow()', 'notThrows()')); x.same = util.deprecate(x.deepEqual, getDeprecationNotice('same()', 'deepEqual()')); x.notSame = util.deprecate(x.notDeepEqual, getDeprecationNotice('notSame()', 'notDeepEqual()')); function getDeprecationNotice(oldApi, newApi) { - return `DEPRECATION NOTICE: ${oldApi} has been renamed to ${newApi} and will eventually be removed.` + - ` See https://github.com/jamestalmage/ava-codemods to help upgrade your codebase automatically.`; + return `DEPRECATION NOTICE: ${oldApi} has been renamed to ${newApi} and will eventually be removed. See https://github.com/jamestalmage/ava-codemods to help upgrade your codebase automatically.`; } - diff --git a/lib/enhance-assert.js b/lib/enhance-assert.js index 17f98a159..f9ddf43fd 100644 --- a/lib/enhance-assert.js +++ b/lib/enhance-assert.js @@ -8,8 +8,8 @@ module.exports.PATTERNS = [ 't.false(value, [message])', 't.is(value, expected, [message])', 't.not(value, expected, [message])', - 't.same(value, expected, [message])', - 't.notSame(value, expected, [message])', + 't.deepEqual(value, expected, [message])', + 't.notDeepEqual(value, expected, [message])', 't.regex(contents, regex, [message])' ]; diff --git a/test/assert.js b/test/assert.js index 412459a0a..6f5cf558e 100644 --- a/test/assert.js +++ b/test/assert.js @@ -277,37 +277,3 @@ test('.deepEqual() should not mask RangeError from underlying assert', function t.end(); }); - -test('.same() should log a deprecation notice', function (t) { - var calledWith; - var originalConsole = console.warn; - console.warn = function () { - calledWith = arguments; - }; - - assert.same({}, {}); - - t.true(calledWith[0].indexOf('DEPRECATION NOTICE') !== -1); - t.true(calledWith[0].indexOf('same()') !== -1); - t.true(calledWith[0].indexOf('deepEqual()') !== -1); - - console.warn = originalConsole; - t.end(); -}); - -test('.notSame() should log a deprecation notice', function (t) { - var calledWith; - var originalConsole = console.warn; - console.warn = function () { - calledWith = arguments; - }; - - assert.notSame({foo: 'bar'}, {bar: 'foo'}); - - t.true(calledWith[0].indexOf('DEPRECATION NOTICE') !== -1); - t.true(calledWith[0].indexOf('notSame()') !== -1); - t.true(calledWith[0].indexOf('notDeepEqual()') !== -1); - - console.warn = originalConsole; - t.end(); -}); From e9a2b2ac2d0369e2d670163c3708342894ed6feb Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Mon, 4 Apr 2016 13:24:48 -0600 Subject: [PATCH 4/6] --amend --- lib/assert.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/assert.js b/lib/assert.js index 18f795935..380d5fe37 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -141,7 +141,6 @@ x.ifError = x.error = function (err, msg) { /* * deprecated APIs */ - x.doesNotThrow = util.deprecate(x.notThrows, getDeprecationNotice('doesNotThrow()', 'notThrows()')); x.same = util.deprecate(x.deepEqual, getDeprecationNotice('same()', 'deepEqual()')); x.notSame = util.deprecate(x.notDeepEqual, getDeprecationNotice('notSame()', 'notDeepEqual()')); From 4a804f7727091aa175c8d659f3dc9aba77ef3106 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Mon, 4 Apr 2016 13:48:59 -0600 Subject: [PATCH 5/6] Responding to PR comments --- index.d.ts | 12 ++++++++++++ lib/assert.js | 2 +- lib/enhance-assert.js | 5 ++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index ae1c9d651..0a48ff6b1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -129,6 +129,18 @@ export interface AssertContext { * Assert that function throws an error or promise rejects. * @param error Can be a constructor, regex, error message or validation function. */ + /** + * Assert that value is deep equal to expected. (DEPRECATED, use `deepEqual`) + */ + same(value: U, expected: U, message?: string): void; + /** + * Assert that value is not deep equal to expected. (DEPRECATED use `notDeepEqual`) + */ + notSame(value: U, expected: U, message?: string): void; + /** + * Assert that function throws an error or promise rejects. + * @param error Can be a constructor, regex, error message or validation function. + */ throws(value: Promise<{}>, error?: ErrorValidator, message?: string): Promise; throws(value: () => void, error?: ErrorValidator, message?: string): any; /** diff --git a/lib/assert.js b/lib/assert.js index 380d5fe37..21953c536 100644 --- a/lib/assert.js +++ b/lib/assert.js @@ -146,5 +146,5 @@ x.same = util.deprecate(x.deepEqual, getDeprecationNotice('same()', 'deepEqual() x.notSame = util.deprecate(x.notDeepEqual, getDeprecationNotice('notSame()', 'notDeepEqual()')); function getDeprecationNotice(oldApi, newApi) { - return `DEPRECATION NOTICE: ${oldApi} has been renamed to ${newApi} and will eventually be removed. See https://github.com/jamestalmage/ava-codemods to help upgrade your codebase automatically.`; + return 'DEPRECATION NOTICE: ' + oldApi + ' has been renamed to ' + newApi + ' and will eventually be removed. See https://github.com/jamestalmage/ava-codemods to help upgrade your codebase automatically.'; } diff --git a/lib/enhance-assert.js b/lib/enhance-assert.js index f9ddf43fd..67eb0698f 100644 --- a/lib/enhance-assert.js +++ b/lib/enhance-assert.js @@ -10,7 +10,10 @@ module.exports.PATTERNS = [ 't.not(value, expected, [message])', 't.deepEqual(value, expected, [message])', 't.notDeepEqual(value, expected, [message])', - 't.regex(contents, regex, [message])' + 't.regex(contents, regex, [message])', + // deprecated apis + 't.same(value, expected, [message])', + 't.notSame(value, expected, [message])' ]; module.exports.NON_ENHANCED_PATTERNS = [ From 4c15c4f430c4f28c332b41303442dbba14063f77 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Mon, 4 Apr 2016 14:10:20 -0600 Subject: [PATCH 6/6] Move DEPRECATED in .d.ts --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 0a48ff6b1..aa47f2831 100644 --- a/index.d.ts +++ b/index.d.ts @@ -130,11 +130,11 @@ export interface AssertContext { * @param error Can be a constructor, regex, error message or validation function. */ /** - * Assert that value is deep equal to expected. (DEPRECATED, use `deepEqual`) + * DEPRECATED, use `deepEqual`. Assert that value is deep equal to expected. */ same(value: U, expected: U, message?: string): void; /** - * Assert that value is not deep equal to expected. (DEPRECATED use `notDeepEqual`) + * DEPRECATED use `notDeepEqual`. Assert that value is not deep equal to expected. */ notSame(value: U, expected: U, message?: string): void; /**