alsatian
Version:
TypeScript and JavaScript testing framework for beautiful and readable tests
119 lines • 6.08 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const spying_1 = require("../spying");
const function_spy_matcher_1 = require("./function-spy-matcher");
const matcher_1 = require("./matcher");
const stringification_1 = require("../stringification");
class FunctionMatcher extends matcher_1.Matcher {
toThrow() {
const error = this._getError();
this._registerMatcher((error === null) !== this.shouldMatch, `Expected an error ` +
`${this.shouldMatch ? "" : "not "}to be thrown ` +
`but ${this.shouldMatch ? "no errors were" : "an error was"} thrown.`, this.shouldMatch ? "an error thrown" : "no errors thrown", {
errorThrown: error ? error.toString() : "none"
});
}
toThrowAsync() {
return __awaiter(this, void 0, void 0, function* () {
const error = yield this._getAsyncError();
this._registerMatcher((error === null) !== this.shouldMatch, `Expected an error ` +
`${this.shouldMatch ? "" : "not "}to be thrown ` +
`but ${this.shouldMatch ? "no errors were" : "an error was"} thrown.`, this.shouldMatch ? "an error thrown" : "no errors thrown", {
errorThrown: error ? error.toString() : "none"
});
});
}
toThrowError(errorType, errorMessage) {
const error = this._getError();
this._errorMatches(error, errorType, errorMessage);
}
toThrowErrorAsync(errorType, errorMessage) {
return __awaiter(this, void 0, void 0, function* () {
const error = yield this._getAsyncError();
this._errorMatches(error, errorType, errorMessage);
});
}
toHaveBeenCalled() {
if (this._isFunctionSpyOrSpiedOnFunction(this.actualValue) === false) {
throw new TypeError("toHaveBeenCalled requires value passed in to Expect to be a FunctionSpy or a spied on function.");
}
const spy = this.actualValue;
this._registerMatcher((spy.calls.length === 0) !== this.shouldMatch, `Expected function ${!this.shouldMatch ? "not " : ""}to be called.`, `function ${!this.shouldMatch ? "not " : ""}to be called`);
return new function_spy_matcher_1.FunctionSpyMatcher(spy);
}
toHaveBeenCalledWith(...expectedArguments) {
if (this._isFunctionSpyOrSpiedOnFunction(this.actualValue) === false) {
throw new TypeError("toHaveBeenCalledWith requires value passed in to Expect to be a FunctionSpy or a spied on function.");
}
const spy = this.actualValue;
this._registerMatcher(spy.calls.some(call => this._callArgumentsMatch(call, expectedArguments)) === this.shouldMatch, `Expected function ${!this.shouldMatch ? "not " : ""}to be called` +
`${this._stringifyArguments(expectedArguments)}.`, `function ${!this.shouldMatch ? "not " : ""}to be called` +
`${this._stringifyArguments(expectedArguments)}.`, {
expectedArguments: stringification_1.stringify(expectedArguments),
actualArguments: stringification_1.stringify(spy.calls.map(call => call.args))
});
return new function_spy_matcher_1.FunctionSpyMatcher(spy, expectedArguments);
}
_stringifyArguments(expectedArguments) {
return expectedArguments ? ` with ${stringification_1.stringify(expectedArguments)}` : "";
}
_getError() {
try {
this.actualValue();
return null;
}
catch (error) {
return error;
}
}
_getAsyncError() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this.actualValue();
return null;
}
catch (error) {
return error;
}
});
}
_errorMatches(error, errorType, errorMessage) {
const threwRightError = error instanceof errorType && error.message === errorMessage;
this._registerMatcher(threwRightError === this.shouldMatch, `Expected an error with ` +
`${errorMessage ? `message "${errorMessage}"` : ""} ` +
`${errorMessage && errorType ? "and " : ""}` +
`${errorType ? `type ${errorType.name} to ${!this.shouldMatch ? "not " : ""}` : ""}` +
`have been thrown, but it was${!this.shouldMatch ? "" : "n't"}.`, this.shouldMatch ? "an error of the right type thrown" : "no errors of type thrown", {
actualError: error ? error.toString() : "none",
expectedError: errorType.name,
expectedErrorMessage: errorMessage
});
}
_callArgumentsMatch(call, expectedArguments) {
if (call.args.length !== expectedArguments.length) {
return false;
}
return call.args.every((arg, index) => {
const expectedArgument = expectedArguments[index];
return (arg === expectedArgument ||
expectedArgument === spying_1.Any ||
(expectedArgument instanceof spying_1.TypeMatcher &&
expectedArgument.test(arg)));
});
}
_isFunctionSpyOrSpiedOnFunction(value) {
return (value instanceof spying_1.FunctionSpy ||
(value instanceof Function && value.calls !== undefined));
}
}
exports.FunctionMatcher = FunctionMatcher;
//# sourceMappingURL=function-matcher.js.map
;