T
T
tostershmoster2021-08-18 16:32:44
Unit testing
tostershmoster, 2021-08-18 16:32:44

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();
  });
});


code under test
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}`);
  // }
}


Model constructor
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);
}


How do you debug these tests? It is not clear where that falls off. The browser does not have these *.test.ts modules. Include them in code?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2021-08-18
@tostershmoster

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 question

Ask a Question

731 491 924 answers to any question