UNPKG

3.88 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const lodash_1 = require("./wrap/lodash");
4const args_match_1 = require("./args-match");
5const calls_1 = require("./store/calls");
6const log_1 = require("./log");
7const store_1 = require("./store");
8const arguments_1 = require("./stringify/arguments");
9const stubbings_1 = require("./store/stubbings");
10const notify_after_satisfaction_1 = require("./matchers/notify-after-satisfaction");
11exports.default = (__userDoesRehearsalInvocationHere__, config = {}) => {
12 const last = calls_1.default.pop();
13 ensureRehearsalOccurred(last);
14 if (calls_1.default.wasInvoked(last.testDouble, last.args, config)) {
15 notifyMatchers(last.testDouble, last.args, config);
16 warnIfStubbed(last.testDouble, last.args);
17 }
18 else {
19 log_1.default.fail(unsatisfiedErrorMessage(last.testDouble, last.args, config));
20 }
21};
22var ensureRehearsalOccurred = (last) => {
23 if (!last) {
24 log_1.default.error('td.verify', `\
25No test double invocation detected for \`verify()\`.
26
27 Usage:
28 verify(myTestDouble('foo'))\
29`);
30 }
31};
32const notifyMatchers = (testDouble, expectedArgs, config) => {
33 lodash_1.default.each(calls_1.default.where(testDouble, expectedArgs, config), (invocation) => {
34 notify_after_satisfaction_1.default(expectedArgs, invocation.args);
35 });
36};
37var warnIfStubbed = (testDouble, actualArgs) => {
38 if (lodash_1.default.some(stubbings_1.default.for(testDouble), (stubbing) => args_match_1.default(stubbing.args, actualArgs, stubbing.config))) {
39 log_1.default.warn('td.verify', `test double${stringifyName(testDouble)} was both stubbed and verified with arguments (${arguments_1.default(actualArgs)}), which is redundant and probably unnecessary.`, 'https://github.com/testdouble/testdouble.js/blob/master/docs/B-frequently-asked-questions.md#why-shouldnt-i-call-both-tdwhen-and-tdverify-for-a-single-interaction-with-a-test-double');
40 }
41};
42var unsatisfiedErrorMessage = (testDouble, args, config) => baseSummary(testDouble, args, config) +
43 matchedInvocationSummary(testDouble, args, config) +
44 invocationSummary(testDouble, args, config);
45var stringifyName = (testDouble) => {
46 const name = store_1.default.for(testDouble).name;
47 return name ? ` \`${name}\`` : '';
48};
49var baseSummary = (testDouble, args, config) => `\
50Unsatisfied verification on test double${stringifyName(testDouble)}.
51
52 Wanted:
53 - called with \`(${arguments_1.default(args)})\`${timesMessage(config)}${ignoreMessage(config)}.\
54`;
55var invocationSummary = (testDouble, args, config) => {
56 const calls = calls_1.default.for(testDouble);
57 if (calls.length === 0) {
58 return '\n\n But there were no invocations of the test double.';
59 }
60 else {
61 return lodash_1.default.reduce(calls, (desc, call) => desc + `\n - called with \`(${arguments_1.default(call.args)})\`.`, '\n\n All calls of the test double, in order were:');
62 }
63};
64var matchedInvocationSummary = (testDouble, args, config) => {
65 const calls = calls_1.default.where(testDouble, args, config);
66 const expectedCalls = config.times || 0;
67 if (calls.length === 0 || calls.length > expectedCalls) {
68 return '';
69 }
70 else {
71 return lodash_1.default.reduce(lodash_1.default.groupBy(calls, 'args'), (desc, callsMatchingArgs, args) => desc + `\n - called ${pluralize(callsMatchingArgs.length, 'time')} with \`(${arguments_1.default(callsMatchingArgs[0].args)})\`.`, `\n\n ${pluralize(calls.length, 'call')} that satisfied this verification:`);
72 }
73};
74var pluralize = (x, msg) => `${x} ${msg}${x === 1 ? '' : 's'}`;
75var timesMessage = (config) => config.times != null
76 ? ` ${pluralize(config.times, 'time')}`
77 : '';
78var ignoreMessage = (config) => config.ignoreExtraArgs != null
79 ? ', ignoring any additional arguments'
80 : '';