'Unit test custom validator expected to be false but the test fails

Testing a custom validator, I expect the validator to be false and yet the test continues to fail, the validator is supposed to check that if years in employment are greater than the birthdate then it should display an error message. I created two tests, one where I passed parameters that would make the validator to equal true triggering the error message to display, and another test where the parameters would not trigger the validator. enter image description here

Test .spec file

    import {FormValidationService} from './form-validation.service';

fdescribe('FormValidationService', () => {
  
  let service: FormValidationService;
  const form: FormControl = new FormControl();
  beforeEach(() => {
    service = new FormValidationService();
  })
  it('should be created', () => {
    expect(service).toBeDefined();
  });
  it('check the validation if years in employment is greater than years alive', () => 
   {
    
    const birthDate = new Date().setFullYear(1992, 6, 26).toString();
    const years = '40';
    const months = '0';
    
    const fn = service.dobLengthValidator(birthDate, years, months);
    const val = fn(form)
    expect(val).toBeTruthy()
    
  })
  it('check the validation if years in employment is less than years alive', () => {
    
    const birthDate = new Date().setFullYear(1992, 6, 26).toString();
    const years = '4';
    const months = '0';
    
    const fn2 = service.dobLengthValidator(birthDate, years, months);
    const val2 = fn2(form)
    expect(val2).toBeFalsy()
    
  })
   
});

Component .ts file

import {Injectable} from '@angular/core';
import {AbstractControl, FormGroup, ValidationErrors, ValidatorFn } from '@angular/forms';
import { CalculateStartDate } from '@app/utils';
import {environment} from '@environments/environment';
import { isBefore, isValid, subYears } from 'date-fns';
import { from } from 'rxjs';
import { Address } from '../models';

@Injectable({
  providedIn: 'root'
})
export class FormValidationService {


  dobLengthValidator = (dob: string, yrsFieldName: string, monthFieldName: string): ValidatorFn => {
    return (form: FormGroup) : ValidationErrors | null => {
      const yrs: string = !!yrsFieldName ? form.get(yrsFieldName)?.value ?? '0' : '0';
      const mths: string = !!monthFieldName ? form.get(monthFieldName)?.value ?? '0' : '0';
      const timeAliveTimestamp = new Date(dob).getTime();
      const timeEnteredTimestamp = new Date(CalculateStartDate(yrs, mths)).getTime();

      return timeAliveTimestamp >= timeEnteredTimestamp ? {dobLengthValidator: true} : null;
    }
  }

}


Solution 1:[1]

The function dobLengthValidator returns a function, so it will always be truthy.

If you want to test that function, you need to call its returned function and pass it a FormGroup object:

const fn2 = service.dobLengthValidator(birthDate, years, months);
const val2 = fn2(someForm);
expect(val2).toBeFalsy();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben