why jest mocked function doesnt return anything?

The code which i want to test is

    'use strict';

const FastestValidator = require("fastest-validator");
const { selector } = require("../selector");

/**
 * @param {object} data - Request Body object
 * @return {object} - Valiated results after applying the Schema
 *
 **/

const validator = async (data) => {
  try {
    console.log("FastestValidator>>  ",FastestValidator)
    console.log("selector>>  ",selector)
    const v = new FastestValidator();
    let schema = await selector(data);
    console.log("schema>>  ",schema)
    console.log("v>>  ",v)
    let check = v.compile(schema);
    let inBoundMappedSchema = {};
    let validatingInboundData = {};


    for (let index in schema) {
      inBoundMappedSchema[index] = data[index];
    }
    validatingInboundData = await check(inBoundMappedSchema);
    if (typeof validatingInboundData === 'object') {
      return {
        success: false,
        potentialIssue: validatingInboundData
      }
    } else if (validatingInboundData && typeof boolean) {
      return {
        success: true,
      }
    }
  } catch (err) {
    console.log("error>>", err)
    return {
      success: false,
      errorMessage: err
    }
  }

};

module.exports = {
  validator
};

my test case is ..

    const {validator} = require('./index');

let FastestValidator = require("fastest-validator");
let { selector } = require("../selector");
jest.mock('../selector');
jest.mock('fastest-validator');






test('testing schema validator', async () => {
  let failed = {
    success: false,
    
  }
  let data = {
      message: 'you '
  }
  
  
  console.log("selector",selector)
  selector = jest.fn();
  selector.mockReturnValueOnce(data);

  selector.mockResolvedValue("randomPrivateKey")
  

  

  console.log("FastestValidator",FastestValidator)
  console.log("selector",selector)
  


  console.log("FastestValidator",validator.FastestValidator)
  console.log("selector",validator.selector)
  

  

    
      
    
    await expect(validator(data)).resolves.toEqual(failed);
  });

in the debug log i can see that schema>> is undefined , which i want to be either data or randomprivateKey . i would be very helpfull to know how to mock the objects created with new keyword as wel, because i am using new FastestValidator() to create object and i need to mock all of its method as well.

adding console output :

    FAIL  src/api/schema/validator/index.test.js
  ● Console

    console.log
      selector [Function: selector] {
        _isMockFunction: true,
        getMockImplementation: [Function (anonymous)],
        mock: [Getter/Setter],
        mockClear: [Function (anonymous)],
        mockReset: [Function (anonymous)],
        mockRestore: [Function (anonymous)],
        mockReturnValueOnce: [Function (anonymous)],
        mockResolvedValueOnce: [Function (anonymous)],
        mockRejectedValueOnce: [Function (anonymous)],
        mockReturnValue: [Function (anonymous)],
        mockResolvedValue: [Function (anonymous)],
        mockRejectedValue: [Function (anonymous)],
        mockImplementationOnce: [Function (anonymous)],
        mockImplementation: [Function (anonymous)],
        mockReturnThis: [Function (anonymous)],
        mockName: [Function (anonymous)],
        getMockName: [Function (anonymous)],
        constructor: [Function: AsyncFunction] {
          _isMockFunction: true,
          getMockImplementation: [Function (anonymous)],
          mock: [Getter/Setter],
          mockClear: [Function (anonymous)],
          mockReset: [Function (anonymous)],
          mockRestore: [Function (anonymous)],
          mockReturnValueOnce: [Function (anonymous)],
          mockResolvedValueOnce: [Function (anonymous)],
          mockRejectedValueOnce: [Function (anonymous)],
          mockReturnValue: [Function (anonymous)],
          mockResolvedValue: [Function (anonymous)],
          mockRejectedValue: [Function (anonymous)],
          mockImplementationOnce: [Function (anonymous)],
          mockImplementation: [Function (anonymous)],
          mockReturnThis: [Function (anonymous)],
          mockName: [Function (anonymous)],
          getMockName: [Function (anonymous)]
        }
      }

      at Object.<anonymous> (src/api/schema/validator/index.test.js:23:11)

    console.log
      FastestValidator [Function: Validator] {
        _isMockFunction: true,
        getMockImplementation: [Function (anonymous)],
        mock: [Getter/Setter],
        mockClear: [Function (anonymous)],
        mockReset: [Function (anonymous)],
        mockRestore: [Function (anonymous)],
        mockReturnValueOnce: [Function (anonymous)],
        mockResolvedValueOnce: [Function (anonymous)],
        mockRejectedValueOnce: [Function (anonymous)],
        mockReturnValue: [Function (anonymous)],
        mockResolvedValue: [Function (anonymous)],
        mockRejectedValue: [Function (anonymous)],
        mockImplementationOnce: [Function (anonymous)],
        mockImplementation: [Function (anonymous)],
        mockReturnThis: [Function (anonymous)],
        mockName: [Function (anonymous)],
        getMockName: [Function (anonymous)]
      }

      at Object.<anonymous> (src/api/schema/validator/index.test.js:32:11)

    console.log
      selector [Function: mockConstructor] {
        _isMockFunction: true,
        getMockImplementation: [Function (anonymous)],
        mock: [Getter/Setter],
        mockClear: [Function (anonymous)],
        mockReset: [Function (anonymous)],
        mockRestore: [Function (anonymous)],
        mockReturnValueOnce: [Function (anonymous)],
        mockResolvedValueOnce: [Function (anonymous)],
        mockRejectedValueOnce: [Function (anonymous)],
        mockReturnValue: [Function (anonymous)],
        mockResolvedValue: [Function (anonymous)],
        mockRejectedValue: [Function (anonymous)],
        mockImplementationOnce: [Function (anonymous)],
        mockImplementation: [Function (anonymous)],
        mockReturnThis: [Function (anonymous)],
        mockName: [Function (anonymous)],
        getMockName: [Function (anonymous)]
      }

      at Object.<anonymous> (src/api/schema/validator/index.test.js:33:11)

    console.log
      FastestValidator undefined

      at Object.<anonymous> (src/api/schema/validator/index.test.js:37:11)

    console.log
      selector undefined

      at Object.<anonymous> (src/api/schema/validator/index.test.js:38:11)

    console.log
      FastestValidator>>   [Function: Validator] {
        _isMockFunction: true,
        getMockImplementation: [Function (anonymous)],
        mock: [Getter/Setter],
        mockClear: [Function (anonymous)],
        mockReset: [Function (anonymous)],
        mockRestore: [Function (anonymous)],
        mockReturnValueOnce: [Function (anonymous)],
        mockResolvedValueOnce: [Function (anonymous)],
        mockRejectedValueOnce: [Function (anonymous)],
        mockReturnValue: [Function (anonymous)],
        mockResolvedValue: [Function (anonymous)],
        mockRejectedValue: [Function (anonymous)],
        mockImplementationOnce: [Function (anonymous)],
        mockImplementation: [Function (anonymous)],
        mockReturnThis: [Function (anonymous)],
        mockName: [Function (anonymous)],
        getMockName: [Function (anonymous)]
      }

      at validator (src/api/schema/validator/index.js:14:13)

    console.log
      selector>>   [Function: selector] {
        _isMockFunction: true,
        getMockImplementation: [Function (anonymous)],
        mock: [Getter/Setter],
        mockClear: [Function (anonymous)],
        mockReset: [Function (anonymous)],
        mockRestore: [Function (anonymous)],
        mockReturnValueOnce: [Function (anonymous)],
        mockResolvedValueOnce: [Function (anonymous)],
        mockRejectedValueOnce: [Function (anonymous)],
        mockReturnValue: [Function (anonymous)],
        mockResolvedValue: [Function (anonymous)],
        mockRejectedValue: [Function (anonymous)],
        mockImplementationOnce: [Function (anonymous)],
        mockImplementation: [Function (anonymous)],
        mockReturnThis: [Function (anonymous)],
        mockName: [Function (anonymous)],
        getMockName: [Function (anonymous)],
        constructor: [Function: AsyncFunction] {
          _isMockFunction: true,
          getMockImplementation: [Function (anonymous)],
          mock: [Getter/Setter],
          mockClear: [Function (anonymous)],
          mockReset: [Function (anonymous)],
          mockRestore: [Function (anonymous)],
          mockReturnValueOnce: [Function (anonymous)],
          mockResolvedValueOnce: [Function (anonymous)],
          mockRejectedValueOnce: [Function (anonymous)],
          mockReturnValue: [Function (anonymous)],
          mockResolvedValue: [Function (anonymous)],
          mockRejectedValue: [Function (anonymous)],
          mockImplementationOnce: [Function (anonymous)],
          mockImplementation: [Function (anonymous)],
          mockReturnThis: [Function (anonymous)],
          mockName: [Function (anonymous)],
          getMockName: [Function (anonymous)]
        }
      }

      at validator (src/api/schema/validator/index.js:15:13)

    console.log
      schema>>   undefined

      at validator (src/api/schema/validator/index.js:18:13)

    console.log
      error>> TypeError: check is not a function
          at validator (/Users/h0j020h/amlng-realtime-api/src/api/schema/validator/index.js:27:35)
          at processTicksAndRejections (internal/process/task_queues.js:95:5)
          at Object.<anonymous> (/Users/h0j020h/amlng-realtime-api/src/api/schema/validator/index.test.js:46:5)

      at validator (src/api/schema/validator/index.js:39:13)

Answer

You need to mock the compile method and the selector this way:

const FastestValidator = require("fastest-validator");
const { validator } = require('./validator')
const { selector } = require("../selector");

jest.mock('../selector');
jest.mock('fastest-validator');

const compile = jest.fn();

FastestValidator.mockImplementation(() => ({
  compile
}))

it('testing schema validator', async () => {
  let failed = {
    success: false,
    potentialIssue: {}
  };

  let data = {
      message: 'you ';
  }
  
  selector.mockResolvedValueOnce(data);
  // selector.mockResolvedValue("randomPrivateKey");
  const check = jest.fn();
  compile.mockReturnValue(check);
  check.mockResolvedValue({});
  await expect(validator(data)).resolves.toEqual(failed);
});

The main problem in the original code was the selector = jest.fn();, why? Because you are saying in that line that now selector is a mocked function without any binding to the "../selector" module, so when you call validator(data) the selector in the function is the original mocked module in which you don’t have ran those methods .mockReturnValueOnce(data) or .mockResolvedValue("randomPrivateKey"), but instead you’ve ran them in a jest.fn() instruction, which is not bind to the module being mocked.

Regarding “mocking new”, new FastestValidator() just creates an object with methods and properties inside, so in the test code:

FastestValidator.mockImplementation(() => ({
  compile
}))

the FastestValidator returns an obj which has a method inside that is named compile and that method is a mocked one jest.fn()