Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError (Jest Test) why would this test fail? Error says function is not a function

TypeError: (0 , _validators.validateEmail)(...).toBe is not a function

Is the error

Here is my test

// Utils
import { validateEmail, validatePassord } from './validators';

describe('validating email addresses', () => {
  it('should return true with a valid email', () => {
    expect(validateEmail('[email protected]').toBe(true));
  });

  it('should return false with an invalid email', () => {
    expect(validateEmail('badEmail').toBe(false));
  });
});

The utility it's testing

export const validateEmail = email => /@google.com\s*$/.test(email);

export const validatePassord = (password) => {
  const hasSpecial = /[!]+/.test(password);
  const hasAlpha = /[a-zA-Z]+/.test(password);
  const hasUppercase = /(?=.*[A-Z])+/.test(password);
  const hasNumber = /[0-9]+/.test(password);
  const hasMinChars = password.length >= 8;

  return hasSpecial && hasAlpha && hasUppercase && hasNumber && hasMinChars;
};

They are in the same directory. Error output:

enter image description here

like image 827
Leon Gaban Avatar asked Sep 12 '26 06:09

Leon Gaban


1 Answers

Try moving your parens around to something like...

expect(validateEmail('[email protected]')).toBe(true);

or something like this for the second test

expect(validateEmail('badEmail')).toBe(false);

like image 110
Kulix Avatar answered Sep 13 '26 20:09

Kulix



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!