最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - mock fs.readFile - Unit test - Stack Overflow

programmeradmin4浏览0评论

I am using Jest framework for unit testing and came across a scenario to mock the fs.readFile.I have used spyOn and mocked the implementation.My code below

test_file.ts

import * as fs from 'fs';


it('read File',  () => {

    const spy = jest.spyOn(fs, 'readFile')
                    .mockImplementation((_, callback) => callback(null, Buffer.from('Sample')));

    // Calling the function
    myFunction('./path');

    expect(spy).toHaveBeenCalled();

});

The spy is not called when i run the test case and the mock is not working.The original implementation is always working.

My function using fs.readFile

myFunction = (path) => {
    // Reading the file
    fs.readFile(path, async (error, file) => {
        console.log(error)      // No such file error thrown instead  of null
        /**  Block of code with async work**/   
    });
};

Briefly,what i am trying to do

How to mock the fs.readFile correctly ?

EDIT
When i tried to console the error in my original callback function,it threw error 'no such file' . But i am expecting the error to be null since I am mocking it to return value as null.

I am using Jest framework for unit testing and came across a scenario to mock the fs.readFile.I have used spyOn and mocked the implementation.My code below

test_file.ts

import * as fs from 'fs';


it('read File',  () => {

    const spy = jest.spyOn(fs, 'readFile')
                    .mockImplementation((_, callback) => callback(null, Buffer.from('Sample')));

    // Calling the function
    myFunction('./path');

    expect(spy).toHaveBeenCalled();

});

The spy is not called when i run the test case and the mock is not working.The original implementation is always working.

My function using fs.readFile

myFunction = (path) => {
    // Reading the file
    fs.readFile(path, async (error, file) => {
        console.log(error)      // No such file error thrown instead  of null
        /**  Block of code with async work**/   
    });
};

Briefly,what i am trying to do

How to mock the fs.readFile correctly ?

EDIT
When i tried to console the error in my original callback function,it threw error 'no such file' . But i am expecting the error to be null since I am mocking it to return value as null.

Share Improve this question edited Jan 20, 2020 at 10:52 Sathya Narayanan GVK asked Jan 18, 2020 at 7:10 Sathya Narayanan GVKSathya Narayanan GVK 9492 gold badges17 silver badges33 bronze badges 9
  • I just tested with function myFunction(path) { fs.readFile(path, (error, res) => console.log('error', error, 'res', res)); } and it works properly. Are you sure there's no issue with your function? EDIT: Be aware that the callback shouldn't be async. – JC Olivares Commented Jan 18, 2020 at 10:31
  • I just tested with your version and it seems to be working fine. What's the current output? – JC Olivares Commented Jan 18, 2020 at 10:38
  • Yes,my callback is async as it does some db work inside .What should i do now ? – Sathya Narayanan GVK Commented Jan 18, 2020 at 10:43
  • 3 The test is passing when using your code. Are you sure you are importing the same fs module on both places? Can you post a full example (all files including imports)? – JC Olivares Commented Jan 18, 2020 at 12:01
  • 1 yes, that was the problem here ! import statements were different..thanks – Sathya Narayanan GVK Commented Jan 20, 2020 at 4:19
 |  Show 4 more ments

1 Answer 1

Reset to default 4

Thanks to @JC Olivares,

'fs' import was the problem here.In the test case file, i imported 'fs' as

import * as fs from 'fs';

But the file with function had import

import fs from 'fs';
发布评论

评论列表(0)

  1. 暂无评论