简体   繁体   中英

react js jest unit testing

In the Login function have a function validateForm which returns username and password length should be greater than zero.

function validateForm() {
    return username.length > 0 && password.length > 0;
  }

used jest unit testing tool

import React from "react";
import Login,{validateForm}  from "./Login";
import { render, screen } from '@testing-library/react';



 describe('LoginUser',()=>{
    test('validate username and password should be greater than 0',()=> {
    var username="name";
    var password="pass";
    expect(validateForm(username.length )).toBeGreaterThan(0);
  });
});

TypeError: (0, _Login.validateForm) is not a function facing the error while running the test case how to solve the error

It looks like your forgot to export it:

  ...
  export function validateForm() {
    return username.length > 0 && password.length > 0;
  }
  ...

But if validateForm is defined inside the body of Login component (function) , consider moving it out and exporting separately to make this test work.

  // Put outside and export:
  export function validateForm() {
    return username.length > 0 && password.length > 0;
  }

  const Login = (props) => {
    // And remove from here.
    // function validateForm() {
    //    return username.length > 0 && password.length > 0;
    // }

    return ...
  }

  export default Login;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM