Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock child process #298

Closed
johnsonsamuel opened this issue May 5, 2020 · 2 comments
Closed

Mock child process #298

johnsonsamuel opened this issue May 5, 2020 · 2 comments

Comments

@johnsonsamuel
Copy link

johnsonsamuel commented May 5, 2020

What is this about?

Mock node's child process exec or spawn ?

Just wanted to check if there is a possibility of mocking node process.

__installPackage.js__

const {promisify} = require('util');
const exec = (require('child_process').exec);

const listFolderContent = async (root) => {
    const options = {
        cwd: root
    };

    await exec('ls -l', options, (err, data) => {
        if(err) console.log('error', err);
    });
};

_installPackage.test.js_

const mock = require('mock-fs');
const {listFolderContent} = require('./listFolderContent');

describe('listFolderContent', async assert => {
    const root = 'my-app';
 
    mock({
        'my-app': {
            'test.txt': 'samuel'
        }

    });

   await listFolderContent(root);    

   mock.restore();
});


My question is, will the exec work and list the content from the mock fs?

which is test.txt?

@3cp
Copy link
Collaborator

3cp commented May 5, 2020

The answer is no. Mockfs cannot do it.
First, exec or spawn creates new OS process which is isolated from current nodejs process. Even if you do exec("node some.js") to create another nodejs process, the new process is not affected by mockfs on current process.

Furthermore, ls uses libc to access file system, it's irrelevant to mockfs which only mocks nodejs's fs module.

@3cp 3cp closed this as completed May 8, 2020
@johnsonsamuel
Copy link
Author

Thank you @3cp for your detailed explanation. I was able to mock exec using sinon. I was able to use mock-fs as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants