From 4976e179774aea28bc788c6e237330ce932d218d Mon Sep 17 00:00:00 2001 From: Christopher Hiller Date: Wed, 26 Aug 2020 16:37:34 -0700 Subject: [PATCH] refactor assertions into an actual unexpected plugin `test/assertions.js` now conforms to this style and is loaded via `expect.use()` like anything else Signed-off-by: Christopher Hiller --- test/assertions.js | 661 +++++++++++++++++++++++---------------------- test/setup.js | 14 +- 2 files changed, 340 insertions(+), 335 deletions(-) diff --git a/test/assertions.js b/test/assertions.js index ef678ff4ea..3bc72ffa5b 100644 --- a/test/assertions.js +++ b/test/assertions.js @@ -1,341 +1,346 @@ 'use strict'; -exports.mixinMochaAssertions = function(expect) { - return expect - .addType({ - name: 'RawResult', - base: 'object', - identify: function(v) { - return ( - Object.prototype.toString.call(v) === '[object Object]' && - typeof v.output === 'string' && - 'code' in v && // may be null - Array.isArray(v.args) - ); - } - }) - .addType({ - name: 'JSONRunResult', - base: 'object', - identify: function(v) { - return ( - Object.prototype.toString.call(v) === '[object Object]' && - Object.prototype.toString.call(v.stats) === '[object Object]' && - Array.isArray(v.failures) && - typeof v.code === 'number' - ); - } - }) - .addType({ - name: 'RawRunResult', - base: 'object', - identify: function(v) { - return ( - Object.prototype.toString.call(v) === '[object Object]' && - typeof v.passing === 'number' && - typeof v.failing === 'number' && - typeof v.pending === 'number' && - typeof v.output === 'string' && - typeof v.code === 'number' - ); - } - }) - .addAssertion(' [not] to have (passed|succeeded)', function( - expect, - result - ) { - expect(result, 'to satisfy', { - code: expect.it('[not] to be', 0), - stats: { - failures: expect.it('[not] to be', 0) - }, - failures: expect.it('[not] to be empty') - }); - }) - .addAssertion( - ' [not] to have (passed|succeeded)', - function(expect, result) { - expect(result, '[not] to have property', 'code', 0); - } - ) - .addAssertion( - ' [not] to have completed with [exit] code ', - function(expect, result, code) { - expect(result.code, '[not] to be', code); - } - ) - .addAssertion( - ' [not] to have passed (with|having) count ', - function(expect, result, count) { - expect(result, '[not] to pass').and('[not] to satisfy', { - stats: {passes: expect.it('to be', count)} - }); - } - ) - .addAssertion( - ' [not] to have failed (with|having) count ', - function(expect, result, count) { - expect(result, '[not] to have failed').and('[not] to satisfy', { - stats: {failures: expect.it('to be', count)} - }); - } - ) - .addAssertion(' [not] to have failed', function( - expect, - result - ) { - expect(result, '[not] to satisfy', { - code: expect.it('to be greater than', 0), - stats: { - failures: expect.it('to be greater than', 0) - }, - failures: expect.it('to be non-empty') - }); - }) - .addAssertion(' [not] to have failed', function( - expect, - result - ) { - expect(result, '[not] to satisfy', { - code: expect.it('to be greater than', 0) - }); - }) - .addAssertion( - ' [not] to have failed (with|having) output ', - function(expect, result, output) { +const {version} = require('../package.json'); + +module.exports = { + name: 'unexpected-mocha-internal', + version, + installInto(expect) { + expect + .addType({ + name: 'RawResult', + base: 'object', + identify: function(v) { + return ( + Object.prototype.toString.call(v) === '[object Object]' && + typeof v.output === 'string' && + 'code' in v && // may be null + Array.isArray(v.args) + ); + } + }) + .addType({ + name: 'JSONRunResult', + base: 'object', + identify: function(v) { + return ( + Object.prototype.toString.call(v) === '[object Object]' && + Object.prototype.toString.call(v.stats) === '[object Object]' && + Array.isArray(v.failures) && + typeof v.code === 'number' + ); + } + }) + .addType({ + name: 'RawRunResult', + base: 'object', + identify: function(v) { + return ( + Object.prototype.toString.call(v) === '[object Object]' && + typeof v.passing === 'number' && + typeof v.failing === 'number' && + typeof v.pending === 'number' && + typeof v.output === 'string' && + typeof v.code === 'number' + ); + } + }) + .addAssertion( + ' [not] to have (passed|succeeded)', + function(expect, result) { + expect(result, 'to satisfy', { + code: expect.it('[not] to be', 0), + stats: { + failures: expect.it('[not] to be', 0) + }, + failures: expect.it('[not] to be empty') + }); + } + ) + .addAssertion( + ' [not] to have (passed|succeeded)', + function(expect, result) { + expect(result, '[not] to have property', 'code', 0); + } + ) + .addAssertion( + ' [not] to have completed with [exit] code ', + function(expect, result, code) { + expect(result.code, '[not] to be', code); + } + ) + .addAssertion( + ' [not] to have passed (with|having) count ', + function(expect, result, count) { + expect(result, '[not] to pass').and('[not] to satisfy', { + stats: {passes: expect.it('to be', count)} + }); + } + ) + .addAssertion( + ' [not] to have failed (with|having) count ', + function(expect, result, count) { + expect(result, '[not] to have failed').and('[not] to satisfy', { + stats: {failures: expect.it('to be', count)} + }); + } + ) + .addAssertion(' [not] to have failed', function( + expect, + result + ) { expect(result, '[not] to satisfy', { code: expect.it('to be greater than', 0), - output: output + stats: { + failures: expect.it('to be greater than', 0) + }, + failures: expect.it('to be non-empty') }); - } - ) - .addAssertion( - ' [not] to have passed (with|having) output ', - function(expect, result, output) { + }) + .addAssertion(' [not] to have failed', function( + expect, + result + ) { expect(result, '[not] to satisfy', { - code: 0, - output: output + code: expect.it('to be greater than', 0) }); - } - ) - .addAssertion( - ' [not] to have failed [test] count ', - function(expect, result, count) { - expect(result.failing, '[not] to be', count); - } - ) - .addAssertion( - ' [not] to have passed [test] count ', - function(expect, result, count) { - expect(result.passing, '[not] to be', count); - } - ) - .addAssertion( - ' [not] to have pending [test] count ', - function(expect, result, count) { - expect(result.pending, '[not] to be', count); - } - ) - .addAssertion(' [not] to have test count ', function( - expect, - result, - count - ) { - expect(result.stats.tests, '[not] to be', count); - }) - .addAssertion( - ' [not] to have failed [test] count ', - function(expect, result, count) { - expect(result.stats.failures, '[not] to be', count); - } - ) - .addAssertion( - ' [not] to have passed [test] count ', - function(expect, result, count) { - expect(result.stats.passes, '[not] to be', count); - } - ) - .addAssertion( - ' [not] to have pending [test] count ', - function(expect, result, count) { - expect(result.stats.pending, '[not] to be', count); - } - ) - .addAssertion( - ' [not] to have run (test|tests) ', - function(expect, result, titles) { - Array.prototype.slice.call(arguments, 2).forEach(function(title) { - expect( - result, - '[not] to have a value satisfying', - expect.it('to have an item satisfying', {title: title}) - ); - }); - } - ) - .addAssertion( - ' [not] to have failed (test|tests) ', - function(expect, result, titles) { - Array.prototype.slice.call(arguments, 2).forEach(function(title) { - expect(result.failures, '[not] to have an item satisfying', { - title: title - }); - }); - } - ) - .addAssertion( - ' [not] to have failed with (error|errors) ', - function(expect, result, errors) { - Array.prototype.slice.call(arguments, 2).forEach(function(error) { - expect(result, '[not] to have failed').and('[not] to satisfy', { - failures: expect.it('to have an item satisfying', { - err: expect - .it('to satisfy', error) - .or('to satisfy', {message: error}) - }) + }) + .addAssertion( + ' [not] to have failed (with|having) output ', + function(expect, result, output) { + expect(result, '[not] to satisfy', { + code: expect.it('to be greater than', 0), + output: output }); - }); - } - ) - .addAssertion( - ' [not] to have (error|errors) ', - function(expect, result, errors) { - Array.prototype.slice.call(arguments, 2).forEach(function(error) { + } + ) + .addAssertion( + ' [not] to have passed (with|having) output ', + function(expect, result, output) { expect(result, '[not] to satisfy', { - failures: expect.it('to have an item satisfying', { - err: expect - .it('to satisfy', error) - .or('to satisfy', {message: error}) + code: 0, + output: output + }); + } + ) + .addAssertion( + ' [not] to have failed [test] count ', + function(expect, result, count) { + expect(result.failing, '[not] to be', count); + } + ) + .addAssertion( + ' [not] to have passed [test] count ', + function(expect, result, count) { + expect(result.passing, '[not] to be', count); + } + ) + .addAssertion( + ' [not] to have pending [test] count ', + function(expect, result, count) { + expect(result.pending, '[not] to be', count); + } + ) + .addAssertion( + ' [not] to have test count ', + function(expect, result, count) { + expect(result.stats.tests, '[not] to be', count); + } + ) + .addAssertion( + ' [not] to have failed [test] count ', + function(expect, result, count) { + expect(result.stats.failures, '[not] to be', count); + } + ) + .addAssertion( + ' [not] to have passed [test] count ', + function(expect, result, count) { + expect(result.stats.passes, '[not] to be', count); + } + ) + .addAssertion( + ' [not] to have pending [test] count ', + function(expect, result, count) { + expect(result.stats.pending, '[not] to be', count); + } + ) + .addAssertion( + ' [not] to have run (test|tests) ', + function(expect, result, titles) { + Array.prototype.slice.call(arguments, 2).forEach(function(title) { + expect( + result, + '[not] to have a value satisfying', + expect.it('to have an item satisfying', {title: title}) + ); + }); + } + ) + .addAssertion( + ' [not] to have failed (test|tests) ', + function(expect, result, titles) { + Array.prototype.slice.call(arguments, 2).forEach(function(title) { + expect(result.failures, '[not] to have an item satisfying', { + title: title + }); + }); + } + ) + .addAssertion( + ' [not] to have failed with (error|errors) ', + function(expect, result, errors) { + Array.prototype.slice.call(arguments, 2).forEach(function(error) { + expect(result, '[not] to have failed').and('[not] to satisfy', { + failures: expect.it('to have an item satisfying', { + err: expect + .it('to satisfy', error) + .or('to satisfy', {message: error}) + }) + }); + }); + } + ) + .addAssertion( + ' [not] to have (error|errors) ', + function(expect, result, errors) { + Array.prototype.slice.call(arguments, 2).forEach(function(error) { + expect(result, '[not] to satisfy', { + failures: expect.it('to have an item satisfying', { + err: expect + .it('to satisfy', error) + .or('to satisfy', {message: error}) + }) + }); + }); + } + ) + .addAssertion( + ' [not] to have passed (test|tests) ', + function(expect, result, titles) { + Array.prototype.slice.call(arguments, 2).forEach(function(title) { + expect(result.passes, '[not] to have an item satisfying', { + title: title + }); + }); + } + ) + .addAssertion( + ' [not] to have test order ', + function(expect, result, state, titles) { + expect( + result[state].slice(0, titles.length), + '[not] to satisfy', + titles.map(function(title) { + return typeof title === 'string' ? {title: title} : title; }) + ); + } + ) + .addAssertion( + ' [not] to have passed test order ', + function(expect, result, titles) { + expect(result, '[not] to have test order', 'passes', titles); + } + ) + .addAssertion( + ' [not] to have passed test order ', + function(expect, result, titles) { + expect( + result, + '[not] to have test order', + 'passes', + Array.prototype.slice.call(arguments, 2) + ); + } + ) + .addAssertion( + ' [not] to have failed test order ', + function(expect, result, titles) { + expect(result, '[not] to have test order', 'failures', titles); + } + ) + .addAssertion( + ' [not] to have failed test order ', + function(expect, result, titles) { + expect( + result, + '[not] to have test order', + 'failures', + Array.prototype.slice.call(arguments, 2) + ); + } + ) + .addAssertion( + ' [not] to have pending test order ', + function(expect, result, titles) { + expect(result, '[not] to have test order', 'pending', titles); + } + ) + .addAssertion( + ' [not] to have pending test order ', + function(expect, result, titles) { + expect( + result, + '[not] to have test order', + 'pending', + Array.prototype.slice.call(arguments, 2) + ); + } + ) + .addAssertion(' [not] to have pending tests', function( + expect, + result + ) { + expect(result.stats.pending, '[not] to be greater than', 0); + }) + .addAssertion(' [not] to have passed tests', function( + expect, + result + ) { + expect(result.stats.passes, '[not] to be greater than', 0); + }) + .addAssertion(' [not] to have failed tests', function( + expect, + result + ) { + expect(result.stats.failed, '[not] to be greater than', 0); + }) + .addAssertion(' [not] to have tests', function( + expect, + result + ) { + expect(result.stats.tests, '[not] to be greater than', 0); + }) + .addAssertion( + ' [not] to have retried test ', + function(expect, result, title) { + expect(result.tests, '[not] to have an item satisfying', { + title: title, + currentRetry: expect.it('to be positive') }); - }); - } - ) - .addAssertion( - ' [not] to have passed (test|tests) ', - function(expect, result, titles) { - Array.prototype.slice.call(arguments, 2).forEach(function(title) { - expect(result.passes, '[not] to have an item satisfying', { - title: title + } + ) + .addAssertion( + ' [not] to have retried test ', + function(expect, result, title, count) { + expect(result.tests, '[not] to have an item satisfying', { + title: title, + currentRetry: count }); - }); - } - ) - .addAssertion( - ' [not] to have test order ', - function(expect, result, state, titles) { - expect( - result[state].slice(0, titles.length), - '[not] to satisfy', - titles.map(function(title) { - return typeof title === 'string' ? {title: title} : title; - }) - ); - } - ) - .addAssertion( - ' [not] to have passed test order ', - function(expect, result, titles) { - expect(result, '[not] to have test order', 'passes', titles); - } - ) - .addAssertion( - ' [not] to have passed test order ', - function(expect, result, titles) { - expect( - result, - '[not] to have test order', - 'passes', - Array.prototype.slice.call(arguments, 2) - ); - } - ) - .addAssertion( - ' [not] to have failed test order ', - function(expect, result, titles) { - expect(result, '[not] to have test order', 'failures', titles); - } - ) - .addAssertion( - ' [not] to have failed test order ', - function(expect, result, titles) { - expect( - result, - '[not] to have test order', - 'failures', - Array.prototype.slice.call(arguments, 2) - ); - } - ) - .addAssertion( - ' [not] to have pending test order ', - function(expect, result, titles) { - expect(result, '[not] to have test order', 'pending', titles); - } - ) - .addAssertion( - ' [not] to have pending test order ', - function(expect, result, titles) { - expect( - result, - '[not] to have test order', - 'pending', - Array.prototype.slice.call(arguments, 2) - ); - } - ) - .addAssertion(' [not] to have pending tests', function( - expect, - result - ) { - expect(result.stats.pending, '[not] to be greater than', 0); - }) - .addAssertion(' [not] to have passed tests', function( - expect, - result - ) { - expect(result.stats.passes, '[not] to be greater than', 0); - }) - .addAssertion(' [not] to have failed tests', function( - expect, - result - ) { - expect(result.stats.failed, '[not] to be greater than', 0); - }) - .addAssertion(' [not] to have tests', function( - expect, - result - ) { - expect(result.stats.tests, '[not] to be greater than', 0); - }) - .addAssertion( - ' [not] to have retried test ', - function(expect, result, title) { - expect(result.tests, '[not] to have an item satisfying', { - title: title, - currentRetry: expect.it('to be positive') - }); - } - ) - .addAssertion( - ' [not] to have retried test ', - function(expect, result, title, count) { - expect(result.tests, '[not] to have an item satisfying', { - title: title, - currentRetry: count - }); - } - ) - .addAssertion( - ' [not] to contain [output] ', - function(expect, result, output) { - expect(result.output, '[not] to satisfy', output); - } - ) - .addAssertion( - ' to have [exit] code ', - function(expect, result, code) { - expect(result.code, 'to be', code); - } - ); + } + ) + .addAssertion( + ' [not] to contain [output] ', + function(expect, result, output) { + expect(result.output, '[not] to satisfy', output); + } + ) + .addAssertion( + ' to have [exit] code ', + function(expect, result, code) { + expect(result.code, 'to be', code); + } + ); + } }; diff --git a/test/setup.js b/test/setup.js index 1732ae9d84..58bda9cb95 100644 --- a/test/setup.js +++ b/test/setup.js @@ -1,9 +1,9 @@ 'use strict'; -var unexpected = require('unexpected'); -global.expect = require('./assertions').mixinMochaAssertions( - unexpected - .clone() - .use(require('unexpected-sinon')) - .use(require('unexpected-eventemitter')) -); +const unexpected = require('unexpected'); + +global.expect = unexpected + .clone() + .use(require('unexpected-sinon')) + .use(require('unexpected-eventemitter')) + .use(require('./assertions'));