Skip to content

Commit 41d87b5

Browse files
committed
Place all throws/notThrows tests in test/assert.js
Move tests from test/promise.js and test/observable.js.
1 parent ae50a13 commit 41d87b5

File tree

3 files changed

+175
-377
lines changed

3 files changed

+175
-377
lines changed

test/assert.js

Lines changed: 175 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ const assertions = assert.wrapAssertions({
2727
throw new Error('Expected testObj');
2828
}
2929

30-
promise.catch(err => {
30+
promise.then(() => {
31+
lastPassed = true;
32+
}, err => {
3133
lastFailure = err;
3234
});
3335
},
@@ -75,16 +77,44 @@ function assertFailure(t, subset) {
7577
}
7678
}
7779

80+
let gathering = false;
81+
let gatheringPromise = Promise.resolve();
82+
function gather(run) {
83+
return t => {
84+
if (gathering) {
85+
throw new Error('Cannot nest gather()');
86+
}
87+
88+
gathering = true;
89+
try {
90+
run(t);
91+
return gatheringPromise;
92+
} finally {
93+
gathering = false;
94+
gatheringPromise = Promise.resolve();
95+
}
96+
};
97+
}
98+
function add(fn) {
99+
if (!gathering) {
100+
throw new Error('Cannot add promise, must be called from gather() callback');
101+
}
102+
gatheringPromise = gatheringPromise.then(fn);
103+
return gatheringPromise;
104+
}
105+
78106
function failsWith(t, fn, subset) {
79107
lastFailure = null;
80108
fn();
81109
assertFailure(t, subset);
82110
}
83111

84-
function eventuallyFailsWith(t, promise, subset) {
85-
lastFailure = null;
86-
return promise.then(() => {
87-
assertFailure(t, subset);
112+
function eventuallyFailsWith(t, fn, subset) {
113+
return add(() => {
114+
lastFailure = null;
115+
return fn().then(() => {
116+
assertFailure(t, subset);
117+
});
88118
});
89119
}
90120

@@ -98,6 +128,19 @@ function fails(t, fn) {
98128
}
99129
}
100130

131+
function eventuallyFails(t, fn) {
132+
return add(() => {
133+
lastFailure = null;
134+
return fn().then(() => {
135+
if (lastFailure) {
136+
t.pass();
137+
} else {
138+
t.fail('Expected assertion to fail');
139+
}
140+
});
141+
});
142+
}
143+
101144
function passes(t, fn) {
102145
lastPassed = false;
103146
fn();
@@ -108,6 +151,19 @@ function passes(t, fn) {
108151
}
109152
}
110153

154+
function eventuallyPasses(t, fn) {
155+
return add(() => {
156+
lastPassed = false;
157+
return fn().then(() => {
158+
if (lastPassed) {
159+
t.pass();
160+
} else {
161+
t.fail('Expected assertion to pass');
162+
}
163+
});
164+
});
165+
}
166+
111167
test('.pass()', t => {
112168
passes(t, () => {
113169
assertions.pass();
@@ -633,7 +689,8 @@ test('.notDeepEqual()', t => {
633689
t.end();
634690
});
635691

636-
test('.throws()', t => {
692+
test('.throws()', gather(t => {
693+
// Fails because function doesn't throw.
637694
failsWith(t, () => {
638695
assertions.throws(() => {});
639696
}, {
@@ -642,6 +699,8 @@ test('.throws()', t => {
642699
values: []
643700
});
644701

702+
// Fails because function doesn't throw. Asserts that 'my message' is used
703+
// as the assertion message (*not* compared against the error).
645704
failsWith(t, () => {
646705
assertions.throws(() => {}, Error, 'my message');
647706
}, {
@@ -650,6 +709,7 @@ test('.throws()', t => {
650709
values: []
651710
});
652711

712+
// Fails because thrown error's message is not equal to 'bar'
653713
const err = new Error('foo');
654714
failsWith(t, () => {
655715
assertions.throws(() => {
@@ -661,14 +721,89 @@ test('.throws()', t => {
661721
values: [{label: 'Threw unexpected exception:', formatted: /foo/}]
662722
});
663723

724+
// Passes because thrown error's message is equal to 'bar'
725+
passes(t, () => {
726+
assertions.throws(() => {
727+
throw err;
728+
}, 'foo');
729+
});
730+
731+
// Passes because an error is thrown.
664732
passes(t, () => {
665733
assertions.throws(() => {
666734
throw new Error('foo');
667735
});
668736
});
669737

670-
t.end();
671-
});
738+
// Fails because the promise is resolved, not rejected.
739+
eventuallyFailsWith(t, () => assertions.throws(Promise.resolve('foo')), {
740+
assertion: 'throws',
741+
message: 'Expected promise to be rejected, but it was resolved instead',
742+
values: [{label: 'Resolved with:', formatted: /'foo'/}]
743+
});
744+
745+
// Fails because the promise is resolved with an Error
746+
eventuallyFailsWith(t, () => assertions.throws(Promise.resolve(new Error())), {
747+
assertion: 'throws',
748+
message: 'Expected promise to be rejected, but it was resolved instead',
749+
values: [{label: 'Resolved with:', formatted: /Error/}]
750+
});
751+
752+
// Fails because the function returned a promise that resolved, not rejected.
753+
eventuallyFailsWith(t, () => assertions.throws(() => Promise.resolve('foo')), {
754+
assertion: 'throws',
755+
message: 'Expected promise to be rejected, but it was resolved instead',
756+
values: [{label: 'Resolved with:', formatted: /'foo'/}]
757+
});
758+
759+
// Passes because the promise was rejected with an error.
760+
eventuallyPasses(t, () => assertions.throws(Promise.reject(new Error())));
761+
762+
// Passes because the function returned a promise rejected with an error.
763+
eventuallyPasses(t, () => assertions.throws(() => Promise.reject(new Error())));
764+
765+
// Passes because the error's message matches the regex
766+
eventuallyPasses(t, () => assertions.throws(Promise.reject(new Error('abc')), /abc/));
767+
768+
// Fails because the error's message does not match the regex
769+
eventuallyFails(t, () => assertions.throws(Promise.reject(new Error('abc')), /def/));
770+
771+
const complete = arg => Observable.of(arg);
772+
const error = err => new Observable(observer => observer.error(err));
773+
774+
// Fails because the observable completed, not errored.
775+
eventuallyFailsWith(t, () => assertions.throws(complete('foo')), {
776+
assertion: 'throws',
777+
message: 'Expected promise to be rejected, but it was resolved instead',
778+
values: [{label: 'Resolved with:', formatted: /'foo'/}]
779+
});
780+
781+
// Fails because the observable completed with an Error
782+
eventuallyFailsWith(t, () => assertions.throws(complete(new Error())), {
783+
assertion: 'throws',
784+
message: 'Expected promise to be rejected, but it was resolved instead',
785+
values: [{label: 'Resolved with:', formatted: /Error/}]
786+
});
787+
788+
// Fails because the function returned a observable that completed, not rejected.
789+
eventuallyFailsWith(t, () => assertions.throws(() => complete('foo')), {
790+
assertion: 'throws',
791+
message: 'Expected promise to be rejected, but it was resolved instead',
792+
values: [{label: 'Resolved with:', formatted: /'foo'/}]
793+
});
794+
795+
// Passes because the observable errored with an error.
796+
eventuallyPasses(t, () => assertions.throws(error(new Error())));
797+
798+
// Passes because the function returned an observable errored with an error.
799+
eventuallyPasses(t, () => assertions.throws(() => error(new Error())));
800+
801+
// Passes because the error's message matches the regex
802+
eventuallyPasses(t, () => assertions.throws(error(new Error('abc')), /abc/));
803+
804+
// Fails because the error's message does not match the regex
805+
eventuallyFails(t, () => assertions.throws(error(new Error('abc')), /def/));
806+
}));
672807

673808
test('.throws() returns the thrown error', t => {
674809
const expected = new Error();
@@ -724,19 +859,13 @@ test('.throws() fails if passed a bad value', t => {
724859
t.end();
725860
});
726861

727-
test('promise .throws() fails when promise is resolved', t => {
728-
return eventuallyFailsWith(t, assertions.throws(Promise.resolve('foo')), {
729-
assertion: 'throws',
730-
message: 'Expected promise to be rejected, but it was resolved instead',
731-
values: [{label: 'Resolved with:', formatted: /'foo'/}]
732-
});
733-
});
734-
735-
test('.notThrows()', t => {
862+
test('.notThrows()', gather(t => {
863+
// Passes because the function doesn't throw
736864
passes(t, () => {
737865
assertions.notThrows(() => {});
738866
});
739867

868+
// Fails because the function throws.
740869
failsWith(t, () => {
741870
assertions.notThrows(() => {
742871
throw new Error('foo');
@@ -747,6 +876,8 @@ test('.notThrows()', t => {
747876
values: [{label: 'Threw:', formatted: /foo/}]
748877
});
749878

879+
// Fails because the function throws. Asserts that message is used for the
880+
// assertion, not to validate the thrown error.
750881
failsWith(t, () => {
751882
assertions.notThrows(() => {
752883
throw new Error('foo');
@@ -757,8 +888,33 @@ test('.notThrows()', t => {
757888
values: [{label: 'Threw:', formatted: /foo/}]
758889
});
759890

760-
t.end();
761-
});
891+
// Passes because the promise is resolved
892+
eventuallyPasses(t, () => assertions.notThrows(Promise.resolve()));
893+
894+
// Fails because the promise is rejected
895+
eventuallyFails(t, () => assertions.notThrows(Promise.reject(new Error())));
896+
897+
// Passes because the function returned a resolved promise
898+
eventuallyPasses(t, () => assertions.notThrows(() => Promise.resolve()));
899+
900+
// Fails because the function returned a rejected promise
901+
eventuallyFails(t, () => assertions.notThrows(() => Promise.reject(new Error())));
902+
903+
const complete = arg => Observable.of(arg);
904+
const error = err => new Observable(observer => observer.error(err));
905+
906+
// Passes because the observable completed
907+
eventuallyPasses(t, () => assertions.notThrows(complete()));
908+
909+
// Fails because the observable errored
910+
eventuallyFails(t, () => assertions.notThrows(error(new Error())));
911+
912+
// Passes because the function returned a completed observable
913+
eventuallyPasses(t, () => assertions.notThrows(() => complete()));
914+
915+
// Fails because the function returned an errored observable
916+
eventuallyFails(t, () => assertions.notThrows(() => error(new Error())));
917+
}));
762918

763919
test('.notThrows() returns undefined for a fulfilled promise', t => {
764920
return assertions.notThrows(Promise.resolve(Symbol(''))).then(actual => {

0 commit comments

Comments
 (0)