用jest嘲弄fs与配置模块冲突



我正在尝试对以下文件进行单元测试:

import { readFileSync } from 'fs';
import logger from './logger.utils';
export const readFile = async (targetDir: string, fileName: string): Promise<string> => {
logger.info(`start reading from ${targetDir}/${fileName}`);
const data = readFileSync(`${targetDir}/${fileName}`, {encoding: 'utf8', flag: 'r'});
return data;
};

jest test file

import { mocked } from 'ts-jest/utils';
import fs from 'fs';
jest.mock('fs');
import * as fsUtils from '../../src/utils/fs.utils';
let readFileSyncMock: jest.SpyInstance;
describe('fs.utils', () => {
describe('readFile', () => {
afterEach(() => {
readFileSyncMock.mockReset();
});
afterAll(() => {
readFileSyncMock.mockRestore();
});
it('should create a new log directory if one doesn't already exist', async () => {
mocked(fs.readFileSync as jest.Mock).mockImplementation(() => { ('some string') });

const fileData = await fsUtils.readFile('target_directory', 'file_name');

expect(fileData).toEqual('some string');
});
});
});

当我运行测试时,我得到以下错误:

Config file /Users/dfaizulaev/Documents/projectname/config/runtime.json cannot be read. Error code is: undefined. Error message is: Cannot read property 'replace' of undefined
1 | import loggingContext from './loggingContext';
> 2 | import config from 'config';
| ^
3 | import os from 'os';
4 | import constants from '../../config/constants';
5 |
at Config.Object.<anonymous>.util.parseFile (node_modules/config/lib/config.js:821:13)
at Config.Object.<anonymous>.util.loadFileConfigs (node_modules/config/lib/config.js:697:26)
at new Config (node_modules/config/lib/config.js:116:27)
at Object.<anonymous> (node_modules/config/lib/config.js:1492:31)
at Object.<anonymous> (src/utils/logger.utils.ts:3:1)

错误来自上述fs模块文件导入的日志文件。

logger.utilsfile

import loggingContext from './loggingContext';
import config from 'config';
import os from 'os';
import constants from '../../config/constants';
const LOG_LEVEL: string = config.get(constants.LOG_LEVEL);
.... more logic ....

我认为错误的原因是我没有正确地模拟fs模块,但我尝试了各种方法并不断得到这个错误。

请建议。

问题是因为我在模拟config包中使用的整个fs模块。

删除jest.mock('fs');并分别模拟方法

例如:

import fs, { Stats, promises } from 'fs';
import * as fsUtils from '../../src/utils/fs.utils';
describe('fs.utils', () => {
afterEach(() => {
jest.resetAllMocks();
});
describe('statSync', () => {
it('Should successfully call statSync', () => {
jest.spyOn(fs, 'statSync').mockReturnValue({ size: 1000 } as Stats);
const result = fsUtils.getFileSize('file path');
expect(result).toEqual(1000);
});
});
describe('writeFile', () => {
it('Should successfully call writeFile', async () => {
jest.spyOn(promises, 'writeFile').mockResolvedValue();
await fsUtils.writeFile('file path', 'data');
});
});
describe('writeFile', () => {
it('Should successfully call writeFile', async () => {
jest.spyOn(promises, 'readFile').mockResolvedValue('some file content');
const result = await fsUtils.readFile('filePath', 'fileName');
expect(result).toEqual('some file content');
});
});
});

相关内容

  • 没有找到相关文章

最新更新