英文:
mock fs.readFile with typescript
问题
I'm here to help with your code translation. Here's the translated code:
我试图模拟 fs.readFile:
```javascript
fs.readFile = jest.fn((_, callback) => callback(null, JSON.stringify('sample')));
我试图测试这个函数:
export const readFileAsynchronously = async (pathToFile: string) => {
const fullPath = join(__dirname, pathToFile);
if (existsSync(fullPath)) {
const fileContent = await readFile(fullPath);
return fileContent.toString();
}
return null;
};
我想测试这个函数而不读取实际文件,是否可能?
测试如下:
test('如果文件存在应返回文件内容', async () => {
jest.mock('fs');
const existsSync = () => true;
fs.existsSync = existsSync;
fs.readFile = jest.fn((_, callback) => {
return callback(null, JSON.stringify('sample'));
});
jest.mock('path');
path.join = (path: string) => path;
const pathToFile = './test.txt';
const result = await readFileAsynchronously(pathToFile);
expect(result).toEqual('sample');
});
但我遇到了一个类型错误:
类型“Mock<any, [_: any, callback: any], any>”中未提供属性“promisify”,而在类型“typeof readFile”中是必需的。
我尝试了另一种变体:
const mockedFs = fs as jest.Mocked<typeof fs>;
const existsSync = () => true;
fs.existsSync = existsSync;
mockedFs.readFile.mockImplementationOnce(
() =>
new Promise(function (resolve) {
return resolve('test');
}),
);
但我收到了一个错误:
TypeError: mockedFs.readFile.mockImplementationOnce 不是一个函数
我不知道如何解决它。请问能否帮助我?
I hope this helps with your code translation. If you have any further questions or need assistance with specific issues in the code, please feel free to ask.
<details>
<summary>英文:</summary>
I'm trying to mock fs.readFile:
fs.readFile = jest.fn((_, callback) => callback(null, JSON.stringify('sample')));
I' m trying to test this function:
export const readFileAsynchronously = async (pathToFile: string) => {
const fullPath = join(__dirname, pathToFile);
if (existsSync(fullPath)) {
const fileContent = await readFile(fullPath);
return fileContent.toString();
}
return null;
};
And I want to test this function without reading the real file, is it possible?
The test is:
test('should return file content if file exists', async () => {
jest.mock('fs');
const existsSync = () => true;
fs.existsSync = existsSync;
fs.readFile = jest.fn((_, callback) => {
return callback(null, JSON.stringify('sample'));
});
jest.mock('path');
path.join = (path: string) => path;
const pathToFile = './test.txt';
const result = await readFileAsynchronously(pathToFile);
expect(result).toEqual('sample');
});
But I've got a type error
Property `__promisify__` is not presented in type "Mock<any, [_: any, callback: any], any>" and is required in type "typeof readFile".
I've tried one more variant:
const mockedFs = fs as jest.Mocked<typeof fs>;
const existsSync = () => true;
fs.existsSync = existsSync;
mockedFs.readFile.mockImplementationOnce(
() =>
new Promise(function (resolve) {
return resolve('test');
}),
);
But I've got an error:
TypeError: mockedFs.readFile.mockImplementationOnce is not a function
I don't have any idea how to solve it. Please, could you help me?
</details>
# 答案1
**得分**: 1
首先,`readFile` 不是一个 promise。你需要使用一个回调或者使用一个不同的函数 `readFileSync`。
你的函数实现不正确,下面是修复后的版本:
```typescript
import { join } from 'path';
import { existsSync, readFile } from 'fs';
export const readFileAsynchronously = async (pathToFile: string) => {
const fullPath = join(__dirname, pathToFile);
if (existsSync(fullPath)) {
// 当回调被调用时,使其解析
return new Promise((resolve, reject) => {
readFile(fullPath, 'utf8', (error, fileContent) => {
if (error) return reject(error);
resolve(fileContent);
});
});
}
return Promise.reject(new Error('文件未找到'));
};
以下是你的测试实现:
jest.mock('fs', () => ({
existsSync: jest.fn().mockResolvedValue(true),
readFile: jest.fn().mockImplementation((path, options, callback) => {
callback(null, '我的文件内容');
}),
}));
test('应该在不实际读取文件的情况下解析', async () => {
const result = await readFileAsynchronously('test.txt');
expect(result).toBe('我的文件内容');
});
英文:
First of all readFile
is not a promise. You need to use a callback or use a different function readFileSync
.
Your function is not implemented correctly, here is the fixed version
import { join } from 'path';
import { existsSync, readFile } from 'fs';
export const readFileAsynchronously = async (pathToFile: string) => {
const fullPath = join(__dirname, pathToFile);
if (existsSync(fullPath)) {
// Making it to resolve when callback is invoked
return new Promise((resolve, reject) => {
readFile(fullPath, 'utf8', (error, fileContent) => {
if (error) return reject(error);
resolve(fileContent);
});
});
}
return Promise.reject(new Error('File not found'));
};
And here is your test implementation
jest.mock('fs', () => ({
existsSync: jest.fn().mockResolvedValue(true),
readFile: jest.fn().mockImplementation((path, options, callback) => {
callback(null, 'my file content');
}),
}));
test('should resolve without reading the actual file', async () => {
const result = await readFileAsynchronously('test.txt');
expect(result).toBe('my file content');
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论