Answer the question
In order to leave comments, you need to log in
Why does the test with different values pass?
Why does a Jest test pass with invalid values?
import { ISettings } from '../RangeSlider/types';
import { Model } from './Model';
let settings: ISettings;
function getPrivateMethod(methodName: string, settings: ISettings) {
const modelProto = Model.prototype as any;
const instance = Object.create(modelProto);
return () => modelProto[methodName].call(instance, settings);
}
describe('validateSettings function check: ', () => {
beforeEach(() => {
settings = {
min: 0,
max: 1500,
valueFrom: 1000,
valueTo: 1490,
isScaleVisible: true,
isTwoRunners: true,
isVertical: false,
isTooltipsVisible: true,
step: 10,
};
});
test('"settings.min >= settings.max" should throw Error', () => {
// тест не должен проходить при таких значениях, должен только при min > max
settings.min = 1000;
settings.max = 1500;
const minIsBiggerThanMax = getPrivateMethod('validateSettings', settings);
expect(minIsBiggerThanMax).toThrow();
});
});
private static validateSettings(settings: ISettings): void {
if (settings.min >= settings.max) {
throw new Error("'max' must be greater than 'min'");
}
// if (settings.valueFrom < settings.min) {
// throw new Error("'valueFrom' must be greater than 'min'");
// }
// if (settings.valueFrom > settings.valueTo) {
// throw new Error("'valueFrom' must be less than 'valueTo'");
// }
// if (settings.valueTo > settings.max) {
// throw new Error("'valueTo' must be less than 'max'");
// }
// if (settings.max - settings.min < settings.step) {
// throw new Error(`'step' must be less than ${settings.max - settings.min}`);
// }
}
constructor(settings: ISettings) {
Model.validateSettings(settings);
this.settings = settings;
// default options
this.minValue = this.settings.min;
this.maxValue = this.settings.max;
this.isTwoRunners = this.settings.isTwoRunners;
this.isScaleVisible = this.settings.isScaleVisible;
this.isVertical = this.settings.isVertical;
this.isTooltipsVisible = this.settings.isTooltipsVisible;
this.valueFrom = this.getThumbValue(this.settings, 'from');
this.valueTo = this.getThumbValue(this.settings, 'to');
this.step = this.getStepInPercents(this.settings.step);
this.rangePercent = (this.settings.max - this.settings.min) / 100;
this.getStepInPercents = this.getStepInPercents.bind(this);
}
Answer the question
In order to leave comments, you need to log in
Because the function does not throw an exception, but catches it itself.
You need to remove the try-catch wrapper.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question