12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502 |
- 'use strict';
- require('../lib/chalk').set();
- require('../lib/worker/options').set({color: false});
- const path = require('path');
- const stripAnsi = require('strip-ansi');
- const React = require('react');
- const renderer = require('react-test-renderer');
- const test = require('tap').test;
- const Observable = require('zen-observable');
- const assert = require('../lib/assert');
- const snapshotManager = require('../lib/snapshot-manager');
- const Test = require('../lib/test');
- const HelloMessage = require('./fixture/hello-message');
- let lastFailure = null;
- let lastPassed = false;
- const assertions = assert.wrapAssertions({
- pass(testObj) {
- if (testObj !== assertions && !(testObj instanceof Test)) {
- throw new Error('Expected testObj');
- }
- lastPassed = true;
- },
- pending(testObj, promise) {
- if (testObj !== assertions && !(testObj instanceof Test)) {
- throw new Error('Expected testObj');
- }
- promise.then(() => {
- lastPassed = true;
- }, err => {
- lastFailure = err;
- });
- },
- fail(testObj, error) {
- if (testObj !== assertions && !(testObj instanceof Test)) {
- throw new Error('Expected testObj');
- }
- lastFailure = error;
- }
- });
- function assertFailure(t, subset) {
- if (!lastFailure) {
- t.fail('Expected assertion to fail');
- return;
- }
- t.is(lastFailure.assertion, subset.assertion);
- t.is(lastFailure.message, subset.message);
- t.is(lastFailure.name, 'AssertionError');
- t.is(lastFailure.operator, subset.operator);
- if (subset.raw) {
- t.is(lastFailure.raw.expected, subset.raw.expected);
- t.is(lastFailure.raw.actual, subset.raw.actual);
- }
- if (subset.statements) {
- t.is(lastFailure.statements.length, subset.statements.length);
- lastFailure.statements.forEach((s, i) => {
- t.is(s[0], subset.statements[i][0]);
- t.match(s[1], subset.statements[i][1]);
- });
- } else {
- t.same(lastFailure.statements, []);
- }
- if (subset.values) {
- t.is(lastFailure.values.length, subset.values.length);
- lastFailure.values.forEach((s, i) => {
- t.is(s.label, subset.values[i].label);
- t.match(stripAnsi(s.formatted), subset.values[i].formatted);
- });
- } else {
- t.same(lastFailure.values, []);
- }
- }
- let gathering = false;
- let gatheringPromise = Promise.resolve();
- function gather(run) {
- return t => {
- if (gathering) {
- throw new Error('Cannot nest gather()');
- }
- gathering = true;
- try {
- run(t);
- return gatheringPromise;
- } finally {
- gathering = false;
- gatheringPromise = Promise.resolve();
- }
- };
- }
- function add(fn) {
- if (!gathering) {
- throw new Error('Cannot add promise, must be called from gather() callback');
- }
- gatheringPromise = gatheringPromise.then(fn);
- return gatheringPromise;
- }
- function failsWith(t, fn, subset) {
- lastFailure = null;
- fn();
- assertFailure(t, subset);
- }
- function eventuallyFailsWith(t, fn, subset) {
- return add(() => {
- lastFailure = null;
- return fn().then(() => {
- assertFailure(t, subset);
- });
- });
- }
- function fails(t, fn) {
- lastFailure = null;
- fn();
- if (lastFailure) {
- t.pass();
- } else {
- t.fail('Expected assertion to fail');
- }
- }
- /* Might be useful
- function eventuallyFails(t, fn) {
- return add(() => {
- lastFailure = null;
- return fn().then(() => {
- if (lastFailure) {
- t.pass();
- } else {
- t.fail('Expected assertion to fail');
- }
- });
- });
- }
- */
- function passes(t, fn) {
- lastPassed = false;
- lastFailure = null;
- fn();
- if (lastPassed) {
- t.pass();
- } else {
- t.ifError(lastFailure, 'Expected assertion to pass');
- }
- }
- function eventuallyPasses(t, fn) {
- return add(() => {
- lastPassed = false;
- lastFailure = null;
- return fn().then(() => {
- if (lastPassed) {
- t.pass();
- } else {
- t.ifError(lastFailure, 'Expected assertion to pass');
- }
- });
- });
- }
- test('.pass()', t => {
- passes(t, () => {
- assertions.pass();
- });
- t.end();
- });
- test('.fail()', t => {
- failsWith(t, () => {
- assertions.fail();
- }, {
- assertion: 'fail',
- message: 'Test failed via `t.fail()`'
- });
- failsWith(t, () => {
- assertions.fail('my message');
- }, {
- assertion: 'fail',
- message: 'my message'
- });
- t.end();
- });
- test('.is()', t => {
- passes(t, () => {
- assertions.is('foo', 'foo');
- });
- passes(t, () => {
- assertions.is('', '');
- });
- passes(t, () => {
- assertions.is(true, true);
- });
- passes(t, () => {
- assertions.is(false, false);
- });
- passes(t, () => {
- assertions.is(null, null);
- });
- passes(t, () => {
- assertions.is(undefined, undefined);
- });
- passes(t, () => {
- assertions.is(1, 1);
- });
- passes(t, () => {
- assertions.is(0, 0);
- });
- passes(t, () => {
- assertions.is(-0, -0);
- });
- passes(t, () => {
- assertions.is(NaN, NaN);
- });
- passes(t, () => {
- assertions.is(0 / 0, NaN);
- });
- passes(t, () => {
- const someRef = {foo: 'bar'};
- assertions.is(someRef, someRef);
- });
- fails(t, () => {
- assertions.is(0, -0);
- });
- fails(t, () => {
- assertions.is(0, false);
- });
- fails(t, () => {
- assertions.is('', false);
- });
- fails(t, () => {
- assertions.is('0', 0);
- });
- fails(t, () => {
- assertions.is('17', 17);
- });
- fails(t, () => {
- assertions.is([1, 2], '1,2');
- });
- fails(t, () => {
- // eslint-disable-next-line no-new-wrappers, unicorn/new-for-builtins
- assertions.is(new String('foo'), 'foo');
- });
- fails(t, () => {
- assertions.is(null, undefined);
- });
- fails(t, () => {
- assertions.is(null, false);
- });
- fails(t, () => {
- assertions.is(undefined, false);
- });
- fails(t, () => {
- // eslint-disable-next-line no-new-wrappers, unicorn/new-for-builtins
- assertions.is(new String('foo'), new String('foo'));
- });
- fails(t, () => {
- assertions.is(0, null);
- });
- fails(t, () => {
- assertions.is(0, NaN);
- });
- fails(t, () => {
- assertions.is('foo', NaN);
- });
- failsWith(t, () => {
- assertions.is({foo: 'bar'}, {foo: 'bar'});
- }, {
- assertion: 'is',
- message: '',
- actual: {foo: 'bar'},
- expected: {foo: 'bar'},
- values: [{
- label: `Values are deeply equal to each other, but they are not the same:`,
- formatted: /foo/
- }]
- });
- failsWith(t, () => {
- assertions.is('foo', 'bar');
- }, {
- assertion: 'is',
- message: '',
- raw: {actual: 'foo', expected: 'bar'},
- values: [
- {label: 'Difference:', formatted: /- 'foo'\n\+ 'bar'/}
- ]
- });
- failsWith(t, () => {
- assertions.is('foo', 42);
- }, {
- actual: 'foo',
- assertion: 'is',
- expected: 42,
- message: '',
- values: [
- {label: 'Difference:', formatted: /- 'foo'\n\+ 42/}
- ]
- });
- failsWith(t, () => {
- assertions.is('foo', 42, 'my message');
- }, {
- assertion: 'is',
- message: 'my message',
- values: [
- {label: 'Difference:', formatted: /- 'foo'\n\+ 42/}
- ]
- });
- failsWith(t, () => {
- assertions.is(0, -0, 'my message');
- }, {
- assertion: 'is',
- message: 'my message',
- values: [
- {label: 'Difference:', formatted: /- 0\n\+ -0/}
- ]
- });
- failsWith(t, () => {
- assertions.is(-0, 0, 'my message');
- }, {
- assertion: 'is',
- message: 'my message',
- values: [
- {label: 'Difference:', formatted: /- -0\n\+ 0/}
- ]
- });
- t.end();
- });
- test('.not()', t => {
- passes(t, () => {
- assertions.not('foo', 'bar');
- });
- fails(t, () => {
- assertions.not(NaN, NaN);
- });
- fails(t, () => {
- assertions.not(0 / 0, NaN);
- });
- failsWith(t, () => {
- assertions.not('foo', 'foo');
- }, {
- assertion: 'not',
- message: '',
- raw: {actual: 'foo', expected: 'foo'},
- values: [{label: 'Value is the same as:', formatted: /foo/}]
- });
- failsWith(t, () => {
- assertions.not('foo', 'foo', 'my message');
- }, {
- assertion: 'not',
- message: 'my message',
- values: [{label: 'Value is the same as:', formatted: /foo/}]
- });
- t.end();
- });
- test('.deepEqual()', t => {
- // Tests starting here are to detect regressions in the underlying libraries
- // used to test deep object equality
- fails(t, () => {
- assertions.deepEqual({a: false}, {a: 0});
- });
- passes(t, () => {
- assertions.deepEqual({
- a: 'a',
- b: 'b'
- }, {
- b: 'b',
- a: 'a'
- });
- });
- passes(t, () => {
- assertions.deepEqual({
- a: 'a',
- b: 'b',
- c: {
- d: 'd'
- }
- }, {
- c: {
- d: 'd'
- },
- b: 'b',
- a: 'a'
- });
- });
- fails(t, () => {
- assertions.deepEqual([1, 2, 3], [1, 2, 3, 4]);
- });
- passes(t, () => {
- assertions.deepEqual([1, 2, 3], [1, 2, 3]);
- });
- fails(t, () => {
- const fnA = a => a;
- const fnB = a => a;
- assertions.deepEqual(fnA, fnB);
- });
- passes(t, () => {
- const x1 = {z: 4};
- const y1 = {x: x1};
- x1.y = y1;
- const x2 = {z: 4};
- const y2 = {x: x2};
- x2.y = y2;
- assertions.deepEqual(x1, x2);
- });
- passes(t, () => {
- function Foo(a) {
- this.a = a;
- }
- const x = new Foo(1);
- const y = new Foo(1);
- assertions.deepEqual(x, y);
- });
- fails(t, () => {
- function Foo(a) {
- this.a = a;
- }
- function Bar(a) {
- this.a = a;
- }
- const x = new Foo(1);
- const y = new Bar(1);
- assertions.deepEqual(x, y);
- });
- fails(t, () => {
- assertions.deepEqual({
- a: 'a',
- b: 'b',
- c: {
- d: false
- }
- }, {
- c: {
- d: 0
- },
- b: 'b',
- a: 'a'
- });
- });
- fails(t, () => {
- assertions.deepEqual({}, []);
- });
- fails(t, () => {
- assertions.deepEqual({0: 'a', 1: 'b'}, ['a', 'b']);
- });
- fails(t, () => {
- assertions.deepEqual({a: 1}, {a: 1, b: undefined});
- });
- fails(t, () => {
- assertions.deepEqual(new Date('1972-08-01'), null);
- });
- fails(t, () => {
- assertions.deepEqual(new Date('1972-08-01'), undefined);
- });
- passes(t, () => {
- assertions.deepEqual(new Date('1972-08-01'), new Date('1972-08-01'));
- });
- passes(t, () => {
- assertions.deepEqual({x: new Date('1972-08-01')}, {x: new Date('1972-08-01')});
- });
- fails(t, () => {
- assertions.deepEqual(() => {}, () => {});
- });
- passes(t, () => {
- assertions.deepEqual(undefined, undefined);
- assertions.deepEqual({x: undefined}, {x: undefined});
- assertions.deepEqual({x: [undefined]}, {x: [undefined]});
- });
- passes(t, () => {
- assertions.deepEqual(null, null);
- assertions.deepEqual({x: null}, {x: null});
- assertions.deepEqual({x: [null]}, {x: [null]});
- });
- passes(t, () => {
- assertions.deepEqual(0, 0);
- assertions.deepEqual(1, 1);
- assertions.deepEqual(3.14, 3.14);
- });
- fails(t, () => {
- assertions.deepEqual(0, 1);
- });
- fails(t, () => {
- assertions.deepEqual(1, -1);
- });
- fails(t, () => {
- assertions.deepEqual(3.14, 2.72);
- });
- fails(t, () => {
- assertions.deepEqual({0: 'a', 1: 'b'}, ['a', 'b']);
- });
- passes(t, () => {
- assertions.deepEqual(
- [
- {foo: {z: 100, y: 200, x: 300}},
- 'bar',
- 11,
- {baz: {d: 4, a: 1, b: 2, c: 3}}
- ],
- [
- {foo: {x: 300, y: 200, z: 100}},
- 'bar',
- 11,
- {baz: {c: 3, b: 2, a: 1, d: 4}}
- ]
- );
- });
- passes(t, () => {
- assertions.deepEqual(
- {x: {a: 1, b: 2}, y: {c: 3, d: 4}},
- {y: {d: 4, c: 3}, x: {b: 2, a: 1}}
- );
- });
- passes(t, () => {
- assertions.deepEqual(
- renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON(),
- React.createElement('div', null, 'Hello ', React.createElement('mark', null, 'Sindre'))
- );
- });
- // Regression test end here
- passes(t, () => {
- assertions.deepEqual({a: 'a'}, {a: 'a'});
- });
- passes(t, () => {
- assertions.deepEqual(['a', 'b'], ['a', 'b']);
- });
- fails(t, () => {
- assertions.deepEqual({a: 'a'}, {a: 'b'});
- });
- fails(t, () => {
- assertions.deepEqual(['a', 'b'], ['a', 'a']);
- });
- fails(t, () => {
- assertions.deepEqual([['a', 'b'], 'c'], [['a', 'b'], 'd']);
- });
- fails(t, () => {
- const circular = ['a', 'b'];
- circular.push(circular);
- assertions.deepEqual([circular, 'c'], [circular, 'd']);
- });
- failsWith(t, () => {
- assertions.deepEqual('foo', 'bar');
- }, {
- assertion: 'deepEqual',
- message: '',
- raw: {actual: 'foo', expected: 'bar'},
- values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 'bar'/}]
- });
- failsWith(t, () => {
- assertions.deepEqual('foo', 42);
- }, {
- assertion: 'deepEqual',
- message: '',
- raw: {actual: 'foo', expected: 42},
- values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 42/}]
- });
- failsWith(t, () => {
- assertions.deepEqual('foo', 42, 'my message');
- }, {
- assertion: 'deepEqual',
- message: 'my message',
- values: [{label: 'Difference:', formatted: /- 'foo'\n\+ 42/}]
- });
- t.end();
- });
- test('.notDeepEqual()', t => {
- passes(t, () => {
- assertions.notDeepEqual({a: 'a'}, {a: 'b'});
- });
- passes(t, () => {
- assertions.notDeepEqual(['a', 'b'], ['c', 'd']);
- });
- const actual = {a: 'a'};
- const expected = {a: 'a'};
- failsWith(t, () => {
- assertions.notDeepEqual(actual, expected);
- }, {
- actual,
- assertion: 'notDeepEqual',
- expected,
- message: '',
- raw: {actual, expected},
- values: [{label: 'Value is deeply equal:', formatted: /.*\{.*\n.*a: 'a'/}]
- });
- failsWith(t, () => {
- assertions.notDeepEqual(['a', 'b'], ['a', 'b'], 'my message');
- }, {
- assertion: 'notDeepEqual',
- message: 'my message',
- values: [{label: 'Value is deeply equal:', formatted: /.*\[.*\n.*'a',\n.*'b',/}]
- });
- t.end();
- });
- test('.throws()', gather(t => {
- // Fails because function doesn't throw.
- failsWith(t, () => {
- assertions.throws(() => {});
- }, {
- assertion: 'throws',
- message: '',
- values: [{label: 'Function returned:', formatted: /undefined/}]
- });
- // Fails because function doesn't throw. Asserts that 'my message' is used
- // as the assertion message (*not* compared against the error).
- failsWith(t, () => {
- assertions.throws(() => {}, null, 'my message');
- }, {
- assertion: 'throws',
- message: 'my message',
- values: [{label: 'Function returned:', formatted: /undefined/}]
- });
- // Fails because thrown exception is not an error
- failsWith(t, () => {
- assertions.throws(() => {
- const err = 'foo';
- throw err;
- });
- }, {
- assertion: 'throws',
- message: '',
- values: [
- {label: 'Function threw exception that is not an error:', formatted: /'foo'/}
- ]
- });
- // Fails because thrown error's message is not equal to 'bar'
- failsWith(t, () => {
- const err = new Error('foo');
- assertions.throws(() => {
- throw err;
- }, 'bar');
- }, {
- assertion: 'throws',
- message: '',
- values: [
- {label: 'Function threw unexpected exception:', formatted: /foo/},
- {label: 'Expected message to equal:', formatted: /bar/}
- ]
- });
- // Fails because thrown error is not the right instance
- failsWith(t, () => {
- const err = new Error('foo');
- assertions.throws(() => {
- throw err;
- }, class Foo {});
- }, {
- assertion: 'throws',
- message: '',
- values: [
- {label: 'Function threw unexpected exception:', formatted: /foo/},
- {label: 'Expected instance of:', formatted: /Foo/}
- ]
- });
- // Passes because thrown error's message is equal to 'bar'
- passes(t, () => {
- const err = new Error('foo');
- assertions.throws(() => {
- throw err;
- }, 'foo');
- });
- // Passes because an error is thrown.
- passes(t, () => {
- assertions.throws(() => {
- throw new Error('foo');
- });
- });
- // Passes because the correct error is thrown.
- passes(t, () => {
- const err = new Error('foo');
- assertions.throws(() => {
- throw err;
- }, {is: err});
- });
- // Fails because the thrown value is not an error
- fails(t, () => {
- const obj = {};
- assertions.throws(() => {
- throw obj;
- }, {is: obj});
- });
- // Fails because the thrown value is not the right one
- fails(t, () => {
- const err = new Error('foo');
- assertions.throws(() => {
- throw err;
- }, {is: {}});
- });
- // Passes because the correct error is thrown.
- passes(t, () => {
- assertions.throws(() => {
- throw new TypeError(); // eslint-disable-line unicorn/error-message
- }, {name: 'TypeError'});
- });
- // Fails because the thrown value is not an error
- fails(t, () => {
- assertions.throws(() => {
- const err = {name: 'Bob'};
- throw err;
- }, {name: 'Bob'});
- });
- // Fails because the thrown value is not the right one
- fails(t, () => {
- assertions.throws(() => {
- throw new Error('foo');
- }, {name: 'TypeError'});
- });
- // Passes because the correct error is thrown.
- passes(t, () => {
- assertions.throws(() => {
- const err = new TypeError(); // eslint-disable-line unicorn/error-message
- err.code = 'ERR_TEST';
- throw err;
- }, {code: 'ERR_TEST'});
- });
- // Fails because the thrown value is not the right one
- fails(t, () => {
- assertions.throws(() => {
- const err = new TypeError(); // eslint-disable-line unicorn/error-message
- err.code = 'ERR_NOPE';
- throw err;
- }, {code: 'ERR_TEST'});
- });
- // Fails because the promise is resolved, not rejected.
- eventuallyFailsWith(t, () => assertions.throws(Promise.resolve('foo')), {
- assertion: 'throws',
- message: '',
- values: [{label: 'Promise resolved with:', formatted: /'foo'/}]
- });
- // Fails because the promise is resolved with an Error
- eventuallyFailsWith(t, () => assertions.throws(Promise.resolve(new Error())), {
- assertion: 'throws',
- message: '',
- values: [{label: 'Promise resolved with:', formatted: /Error/}]
- });
- // Fails because the function returned a promise that resolved, not rejected.
- eventuallyFailsWith(t, () => assertions.throws(() => Promise.resolve('foo')), {
- assertion: 'throws',
- message: '',
- values: [{label: 'Returned promise resolved with:', formatted: /'foo'/}]
- });
- // Passes because the promise was rejected with an error.
- eventuallyPasses(t, () => assertions.throws(Promise.reject(new Error())));
- // Passes because the function returned a promise rejected with an error.
- eventuallyPasses(t, () => assertions.throws(() => Promise.reject(new Error())));
- // Passes because the error's message matches the regex
- eventuallyPasses(t, () => assertions.throws(Promise.reject(new Error('abc')), /abc/));
- // Fails because the error's message does not match the regex
- eventuallyFailsWith(t, () => assertions.throws(Promise.reject(new Error('abc')), /def/), {
- assertion: 'throws',
- message: '',
- values: [
- {label: 'Promise rejected with unexpected exception:', formatted: /Error/},
- {label: 'Expected message to match:', formatted: /\/def\//}
- ]
- });
- const complete = arg => Observable.of(arg);
- const error = err => new Observable(observer => observer.error(err));
- // Fails because the observable completed, not errored.
- eventuallyFailsWith(t, () => assertions.throws(complete('foo')), {
- assertion: 'throws',
- message: '',
- values: [{label: 'Observable completed with:', formatted: /'foo'/}]
- });
- // Fails because the observable completed with an Error
- eventuallyFailsWith(t, () => assertions.throws(complete(new Error())), {
- assertion: 'throws',
- message: '',
- values: [{label: 'Observable completed with:', formatted: /Error/}]
- });
- // Fails because the function returned a observable that completed, not rejected.
- eventuallyFailsWith(t, () => assertions.throws(() => complete('foo')), {
- assertion: 'throws',
- message: '',
- values: [{label: 'Returned observable completed with:', formatted: /'foo'/}]
- });
- // Passes because the observable errored with an error.
- eventuallyPasses(t, () => assertions.throws(error(new Error())));
- // Passes because the function returned an observable errored with an error.
- eventuallyPasses(t, () => assertions.throws(() => error(new Error())));
- // Passes because the error's message matches the regex
- eventuallyPasses(t, () => assertions.throws(error(new Error('abc')), /abc/));
- // Fails because the error's message does not match the regex
- eventuallyFailsWith(t, () => assertions.throws(error(new Error('abc')), /def/), {
- assertion: 'throws',
- message: '',
- values: [
- {label: 'Observable errored with unexpected exception:', formatted: /Error/},
- {label: 'Expected message to match:', formatted: /\/def\//}
- ]
- });
- }));
- test('.throws() returns the thrown error', t => {
- const expected = new Error();
- const actual = assertions.throws(() => {
- throw expected;
- });
- t.is(actual, expected);
- t.end();
- });
- test('.throws() returns the rejection reason of promise', t => {
- const expected = new Error();
- return assertions.throws(Promise.reject(expected)).then(actual => {
- t.is(actual, expected);
- t.end();
- });
- });
- test('.throws() returns the rejection reason of a promise returned by the function', t => {
- const expected = new Error();
- return assertions.throws(() => {
- return Promise.reject(expected);
- }).then(actual => {
- t.is(actual, expected);
- t.end();
- });
- });
- test('.throws() returns the error of an observable returned by the function', t => {
- const expected = new Error();
- return assertions.throws(() => {
- return new Observable(observer => observer.error(expected));
- }).then(actual => {
- t.is(actual, expected);
- t.end();
- });
- });
- test('.throws() fails if passed a bad value', t => {
- failsWith(t, () => {
- assertions.throws('not a function');
- }, {
- assertion: 'throws',
- message: '`t.throws()` must be called with a function, observable or promise',
- values: [{label: 'Called with:', formatted: /not a function/}]
- });
- t.end();
- });
- test('.throws() fails if passed a bad expectation', t => {
- failsWith(t, () => {
- assertions.throws(() => {}, true);
- }, {
- assertion: 'throws',
- message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',
- values: [{label: 'Called with:', formatted: /true/}]
- });
- failsWith(t, () => {
- assertions.throws(() => {}, {});
- }, {
- assertion: 'throws',
- message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',
- values: [{label: 'Called with:', formatted: /\{\}/}]
- });
- failsWith(t, () => {
- assertions.throws(() => {}, []);
- }, {
- assertion: 'throws',
- message: 'The second argument to `t.throws()` must be a function, string, regular expression, expectation object or `null`',
- values: [{label: 'Called with:', formatted: /\[\]/}]
- });
- failsWith(t, () => {
- assertions.throws(() => {}, {code: 42});
- }, {
- assertion: 'throws',
- message: 'The `code` property of the second argument to `t.throws()` must be a string',
- values: [{label: 'Called with:', formatted: /code: 42/}]
- });
- failsWith(t, () => {
- assertions.throws(() => {}, {instanceOf: null});
- }, {
- assertion: 'throws',
- message: 'The `instanceOf` property of the second argument to `t.throws()` must be a function',
- values: [{label: 'Called with:', formatted: /instanceOf: null/}]
- });
- failsWith(t, () => {
- assertions.throws(() => {}, {message: null});
- }, {
- assertion: 'throws',
- message: 'The `message` property of the second argument to `t.throws()` must be a string or regular expression',
- values: [{label: 'Called with:', formatted: /message: null/}]
- });
- failsWith(t, () => {
- assertions.throws(() => {}, {name: null});
- }, {
- assertion: 'throws',
- message: 'The `name` property of the second argument to `t.throws()` must be a string',
- values: [{label: 'Called with:', formatted: /name: null/}]
- });
- failsWith(t, () => {
- assertions.throws(() => {}, {is: {}, message: '', name: '', of() {}, foo: null});
- }, {
- assertion: 'throws',
- message: 'The second argument to `t.throws()` contains unexpected properties',
- values: [{label: 'Called with:', formatted: /foo: null/}]
- });
- t.end();
- });
- test('.notThrows()', gather(t => {
- // Passes because the function doesn't throw
- passes(t, () => {
- assertions.notThrows(() => {});
- });
- // Fails because the function throws.
- failsWith(t, () => {
- assertions.notThrows(() => {
- throw new Error('foo');
- });
- }, {
- assertion: 'notThrows',
- message: '',
- values: [{label: 'Function threw:', formatted: /foo/}]
- });
- // Fails because the function throws. Asserts that message is used for the
- // assertion, not to validate the thrown error.
- failsWith(t, () => {
- assertions.notThrows(() => {
- throw new Error('foo');
- }, 'my message');
- }, {
- assertion: 'notThrows',
- message: 'my message',
- values: [{label: 'Function threw:', formatted: /foo/}]
- });
- // Passes because the promise is resolved
- eventuallyPasses(t, () => assertions.notThrows(Promise.resolve()));
- // Fails because the promise is rejected
- eventuallyFailsWith(t, () => assertions.notThrows(Promise.reject(new Error())), {
- assertion: 'notThrows',
- message: '',
- values: [{label: 'Promise rejected with:', formatted: /Error/}]
- });
- // Passes because the function returned a resolved promise
- eventuallyPasses(t, () => assertions.notThrows(() => Promise.resolve()));
- // Fails because the function returned a rejected promise
- eventuallyFailsWith(t, () => assertions.notThrows(() => Promise.reject(new Error())), {
- assertion: 'notThrows',
- message: '',
- values: [{label: 'Returned promise rejected with:', formatted: /Error/}]
- });
- const complete = arg => Observable.of(arg);
- const error = err => new Observable(observer => observer.error(err));
- // Passes because the observable completed
- eventuallyPasses(t, () => assertions.notThrows(complete()));
- // Fails because the observable errored
- eventuallyFailsWith(t, () => assertions.notThrows(error(new Error())), {
- assertion: 'notThrows',
- message: '',
- values: [{label: 'Observable errored with:', formatted: /Error/}]
- });
- // Passes because the function returned a completed observable
- eventuallyPasses(t, () => assertions.notThrows(() => complete()));
- // Fails because the function returned an errored observable
- eventuallyFailsWith(t, () => assertions.notThrows(() => error(new Error())), {
- assertion: 'notThrows',
- message: '',
- values: [{label: 'Returned observable errored with:', formatted: /Error/}]
- });
- }));
- test('.notThrows() returns undefined for a fulfilled promise', t => {
- return assertions.notThrows(Promise.resolve(Symbol(''))).then(actual => {
- t.is(actual, undefined);
- });
- });
- test('.notThrows() returns undefined for a fulfilled promise returned by the function', t => {
- return assertions.notThrows(() => {
- return Promise.resolve(Symbol(''));
- }).then(actual => {
- t.is(actual, undefined);
- });
- });
- test('.notThrows() returns undefined for an observable returned by the function', t => {
- return assertions.notThrows(() => {
- return Observable.of(Symbol(''));
- }).then(actual => {
- t.is(actual, undefined);
- });
- });
- test('.notThrows() fails if passed a bad value', t => {
- failsWith(t, () => {
- assertions.notThrows('not a function');
- }, {
- assertion: 'notThrows',
- message: '`t.notThrows()` must be called with a function, observable or promise',
- values: [{label: 'Called with:', formatted: /not a function/}]
- });
- t.end();
- });
- test('.snapshot()', t => {
- // Set to `true` to update the snapshot, then run:
- // "$(npm bin)"/tap --no-cov -R spec test/assert.js
- //
- // Ignore errors and make sure not to run tests with the `-b` (bail) option.
- const updating = false;
- const projectDir = path.join(__dirname, 'fixture');
- const manager = snapshotManager.load({
- file: __filename,
- name: 'assert.js',
- projectDir,
- relFile: 'test/assert.js',
- fixedLocation: null,
- testDir: projectDir,
- updating
- });
- const setup = title => {
- return new Test({
- title,
- compareTestSnapshot: options => manager.compare(options)
- });
- };
- passes(t, () => {
- const testInstance = setup('passes');
- assertions.snapshot.call(testInstance, {foo: 'bar'});
- assertions.snapshot.call(testInstance, {foo: 'bar'}, {id: 'fixed id'}, 'message not included in snapshot report');
- assertions.snapshot.call(testInstance, React.createElement(HelloMessage, {name: 'Sindre'}));
- assertions.snapshot.call(testInstance, renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON());
- });
- {
- const testInstance = setup('fails');
- if (updating) {
- assertions.snapshot.call(testInstance, {foo: 'bar'});
- } else {
- failsWith(t, () => {
- assertions.snapshot.call(testInstance, {foo: 'not bar'});
- }, {
- assertion: 'snapshot',
- message: 'Did not match snapshot',
- values: [{label: 'Difference:', formatted: ' {\n- foo: \'not bar\',\n+ foo: \'bar\',\n }'}]
- });
- }
- }
- failsWith(t, () => {
- const testInstance = setup('fails (fixed id)');
- assertions.snapshot.call(testInstance, {foo: 'not bar'}, {id: 'fixed id'}, 'different message, also not included in snapshot report');
- }, {
- assertion: 'snapshot',
- message: 'different message, also not included in snapshot report',
- values: [{label: 'Difference:', formatted: ' {\n- foo: \'not bar\',\n+ foo: \'bar\',\n }'}]
- });
- {
- const testInstance = setup('fails');
- if (updating) {
- assertions.snapshot.call(testInstance, {foo: 'bar'}, 'my message');
- } else {
- failsWith(t, () => {
- assertions.snapshot.call(testInstance, {foo: 'not bar'}, 'my message');
- }, {
- assertion: 'snapshot',
- message: 'my message',
- values: [{label: 'Difference:', formatted: ' {\n- foo: \'not bar\',\n+ foo: \'bar\',\n }'}]
- });
- }
- }
- {
- const testInstance = setup('rendered comparison');
- if (updating) {
- assertions.snapshot.call(testInstance, renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON());
- } else {
- passes(t, () => {
- assertions.snapshot.call(testInstance, React.createElement('div', null, 'Hello ', React.createElement('mark', null, 'Sindre')));
- });
- }
- }
- {
- const testInstance = setup('rendered comparison');
- if (updating) {
- assertions.snapshot.call(testInstance, renderer.create(React.createElement(HelloMessage, {name: 'Sindre'})).toJSON());
- } else {
- failsWith(t, () => {
- assertions.snapshot.call(testInstance, renderer.create(React.createElement(HelloMessage, {name: 'Vadim'})).toJSON());
- }, {
- assertion: 'snapshot',
- message: 'Did not match snapshot',
- values: [{label: 'Difference:', formatted: ' <div>\n Hello \n <mark>\n- Vadim\n+ Sindre\n </mark>\n </div>'}]
- });
- }
- }
- {
- const testInstance = setup('element comparison');
- if (updating) {
- assertions.snapshot.call(testInstance, React.createElement(HelloMessage, {name: 'Sindre'}));
- } else {
- failsWith(t, () => {
- assertions.snapshot.call(testInstance, React.createElement(HelloMessage, {name: 'Vadim'}));
- }, {
- assertion: 'snapshot',
- message: 'Did not match snapshot',
- values: [{label: 'Difference:', formatted: ' <HelloMessage⍟\n- name="Vadim"\n+ name="Sindre"\n />'}]
- });
- }
- }
- manager.save();
- t.end();
- });
- test('.truthy()', t => {
- failsWith(t, () => {
- assertions.truthy(0);
- }, {
- assertion: 'truthy',
- message: '',
- operator: '!!',
- values: [{label: 'Value is not truthy:', formatted: /0/}]
- });
- failsWith(t, () => {
- assertions.truthy(false, 'my message');
- }, {
- assertion: 'truthy',
- message: 'my message',
- operator: '!!',
- values: [{label: 'Value is not truthy:', formatted: /false/}]
- });
- passes(t, () => {
- assertions.truthy(1);
- assertions.truthy(true);
- });
- t.end();
- });
- test('.falsy()', t => {
- failsWith(t, () => {
- assertions.falsy(1);
- }, {
- assertion: 'falsy',
- message: '',
- operator: '!',
- values: [{label: 'Value is not falsy:', formatted: /1/}]
- });
- failsWith(t, () => {
- assertions.falsy(true, 'my message');
- }, {
- assertion: 'falsy',
- message: 'my message',
- operator: '!',
- values: [{label: 'Value is not falsy:', formatted: /true/}]
- });
- passes(t, () => {
- assertions.falsy(0);
- assertions.falsy(false);
- });
- t.end();
- });
- test('.true()', t => {
- failsWith(t, () => {
- assertions.true(1);
- }, {
- assertion: 'true',
- message: '',
- values: [{label: 'Value is not `true`:', formatted: /1/}]
- });
- failsWith(t, () => {
- assertions.true(0);
- }, {
- assertion: 'true',
- message: '',
- values: [{label: 'Value is not `true`:', formatted: /0/}]
- });
- failsWith(t, () => {
- assertions.true(false);
- }, {
- assertion: 'true',
- message: '',
- values: [{label: 'Value is not `true`:', formatted: /false/}]
- });
- failsWith(t, () => {
- assertions.true('foo', 'my message');
- }, {
- assertion: 'true',
- message: 'my message',
- values: [{label: 'Value is not `true`:', formatted: /foo/}]
- });
- passes(t, () => {
- assertions.true(true);
- });
- t.end();
- });
- test('.false()', t => {
- failsWith(t, () => {
- assertions.false(0);
- }, {
- assertion: 'false',
- message: '',
- values: [{label: 'Value is not `false`:', formatted: /0/}]
- });
- failsWith(t, () => {
- assertions.false(1);
- }, {
- assertion: 'false',
- message: '',
- values: [{label: 'Value is not `false`:', formatted: /1/}]
- });
- failsWith(t, () => {
- assertions.false(true);
- }, {
- assertion: 'false',
- message: '',
- values: [{label: 'Value is not `false`:', formatted: /true/}]
- });
- failsWith(t, () => {
- assertions.false('foo', 'my message');
- }, {
- assertion: 'false',
- message: 'my message',
- values: [{label: 'Value is not `false`:', formatted: /foo/}]
- });
- passes(t, () => {
- assertions.false(false);
- });
- t.end();
- });
- test('.regex()', t => {
- passes(t, () => {
- assertions.regex('abc', /^abc$/);
- });
- failsWith(t, () => {
- assertions.regex('foo', /^abc$/);
- }, {
- assertion: 'regex',
- message: '',
- values: [
- {label: 'Value must match expression:', formatted: /foo/},
- {label: 'Regular expression:', formatted: /\/\^abc\$\//}
- ]
- });
- failsWith(t, () => {
- assertions.regex('foo', /^abc$/, 'my message');
- }, {
- assertion: 'regex',
- message: 'my message',
- values: [
- {label: 'Value must match expression:', formatted: /foo/},
- {label: 'Regular expression:', formatted: /\/\^abc\$\//}
- ]
- });
- t.end();
- });
- test('.regex() fails if passed a bad value', t => {
- failsWith(t, () => {
- assertions.regex(42, /foo/);
- }, {
- assertion: 'regex',
- message: '`t.regex()` must be called with a string',
- values: [{label: 'Called with:', formatted: /42/}]
- });
- failsWith(t, () => {
- assertions.regex('42', {});
- }, {
- assertion: 'regex',
- message: '`t.regex()` must be called with a regular expression',
- values: [{label: 'Called with:', formatted: /\{\}/}]
- });
- t.end();
- });
- test('.notRegex()', t => {
- passes(t, () => {
- assertions.notRegex('abc', /def/);
- });
- failsWith(t, () => {
- assertions.notRegex('abc', /abc/);
- }, {
- assertion: 'notRegex',
- message: '',
- values: [
- {label: 'Value must not match expression:', formatted: /abc/},
- {label: 'Regular expression:', formatted: /\/abc\//}
- ]
- });
- failsWith(t, () => {
- assertions.notRegex('abc', /abc/, 'my message');
- }, {
- assertion: 'notRegex',
- message: 'my message',
- values: [
- {label: 'Value must not match expression:', formatted: /abc/},
- {label: 'Regular expression:', formatted: /\/abc\//}
- ]
- });
- t.end();
- });
- test('.notRegex() fails if passed a bad value', t => {
- failsWith(t, () => {
- assertions.notRegex(42, /foo/);
- }, {
- assertion: 'notRegex',
- message: '`t.notRegex()` must be called with a string',
- values: [{label: 'Called with:', formatted: /42/}]
- });
- failsWith(t, () => {
- assertions.notRegex('42', {});
- }, {
- assertion: 'notRegex',
- message: '`t.notRegex()` must be called with a regular expression',
- values: [{label: 'Called with:', formatted: /\{\}/}]
- });
- t.end();
- });
|