简体   繁体   中英

Jest mocking module that exports a class and functions

I have a module that exports a class and 2 functions and that module is imported into a file that is being tested.

someFile.js
const {theclass, thefunction} = require("theModule");

const getSomeFileData = () => {
   let obj = new theclass();
   //some logic
   return obj.getData();
}

In the test file, I want to mock the module "theModule" and return a known value when the function obj.getData() is called. How would I go about mocking this module("theModule") when testing file "someFile.js"?

Edit:

.spec.ts

import { someFunction } from './index-test';

jest.mock('lodash', () => {
  return {
    uniqueId: () => 2,
  };
});

describe('', () => {
  it('', () => {
    expect(someFunction()).toBe(2);
  });
});

index-test.ts

import { uniqueId } from 'lodash';

export const someFunction = () => {
  return uniqueId();
};

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