我正在尝试使用玩笑在第三方节点模块中模拟一个函数,特别是fs.readFileSync()函数。有很多例子,但我还没有找到一个使用Typescript的例子。我在github有一个简单的例子,希望它最少。对于熟悉玩笑的人来说,这可能是一个简单的问题。
答案 0 :(得分:1)
以下是模拟fs.readFileSync()
之类的几种不同方式:
模拟功能
要模拟功能,请结合使用jest.spyOn()
和mockImplementation()
之类的功能:
import { returnNameInJsonFile } from './index';
import * as fs from 'fs';
describe('index', () => {
it('returnNameInJsonFile', () => {
const mock = jest.spyOn(fs, 'readFileSync'); // spy on fs.readFileSync()
mock.mockImplementation(() => JSON.stringify({ name: 'myname' })); // replace the implementation
const name: string = returnNameInJsonFile('test.json');
expect(name).toBe('myname');
mock.mockRestore(); // restore fs.readFileSync()
});
});
使用工厂模拟模块
通过module factory to jest.mock()
:
import { returnNameInJsonFile } from './index';
jest.mock('fs', () => {
const MOCK_FILE_INFO = { 'test.json': JSON.stringify({ name: 'myname' }) };
return {
readFileSync: (fpath, opts) => {
if (fpath in MOCK_FILE_INFO) {
return MOCK_FILE_INFO[fpath]
}
throw 'unexpected fpath'
}
}
});
describe('index', () => {
it('returnNameInJsonFile', () => {
const name: string = returnNameInJsonFile('test.json');
expect(name).toBe('myname'); // 1.0.0 is installed and 2.0.0 is available
});
});
自动模拟模块
Jest
将自动使用模拟,除非它是核心Node模块(如fs
),在这种情况下为calling jest.mock()
is required。
__ mocks __ / fs.ts:
const fs = jest.genMockFromModule('fs');
let mockFiles: object = {};
function __setMockFiles (newMockFiles: object) {
mockFiles = newMockFiles;
}
function readFileSync(filePath: string) {
return mockFiles[filePath] || '';
}
// If anyone knows how to avoid the type assertion feel free to edit this answer
(fs as any).__setMockFiles = __setMockFiles;
(fs as any).readFileSync = readFileSync;
module.exports = fs;
index.test.ts:
import { returnNameInJsonFile } from './index';
jest.mock('fs'); // Required since fs is a core Node module
describe('index', () => {
const MOCK_FILE_INFO = { 'test.json': JSON.stringify({ name: 'myname' }) };
beforeEach(() => {
require('fs').__setMockFiles(MOCK_FILE_INFO);
});
it('returnNameInJsonFile', () => {
const name: string = returnNameInJsonFile('test.json');
expect(name).toBe('myname'); // 1.0.0 is installed and 2.0.0 is available
});
});