[英]Jest mocking module that exports a class and functions
我有一個導出 class 和 2 個函數的模塊,並且該模塊被導入到正在測試的文件中。
someFile.js
const {theclass, thefunction} = require("theModule");
const getSomeFileData = () => {
let obj = new theclass();
//some logic
return obj.getData();
}
在測試文件中,我想模擬模塊“theModule”並在調用 function obj.getData() 時返回一個已知值。 在測試文件“someFile.js”時,我將如何 go 關於 mocking 這個模塊(“theModule”)?
編輯:
.spec.ts
import { someFunction } from './index-test';
jest.mock('lodash', () => {
return {
uniqueId: () => 2,
};
});
describe('', () => {
it('', () => {
expect(someFunction()).toBe(2);
});
});
索引測試.ts
import { uniqueId } from 'lodash';
export const someFunction = () => {
return uniqueId();
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.