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

Make it Modular: Issue when using named imports for fs #700

Open
reccanti opened this issue Jan 24, 2020 · 2 comments · May be fixed by #710
Open

Make it Modular: Issue when using named imports for fs #700

reccanti opened this issue Jan 24, 2020 · 2 comments · May be fixed by #710

Comments

@reccanti
Copy link

Hello! I noticed a potential issue in the "Make it Modular" exercise. When importing readdir using a named import like this:

const { readdir }  = require("fs");
const { extname } = require("path");

module.exports = function filterFilesByExtension(directory, extension, callback) {
    function isExtension(file) {
        return extname(file) === "." + extension;
    }
    readdir(directory, (err, files) => {
        if (err) {
            return callback(err);
        }
        const matchingFiles = files.filter(isExtension)
        callback(null, matchingFiles);
    });
}

I get the following error:

 Your additional module file [mymodule.js] does not appear to pass back an
 error received from fs.readdir(). Use the following idiomatic Node.js
 pattern inside your callback to fs.readdir(): if (err) return
 callback(err)

 # FAIL Your solution to MAKE IT MODULAR didn't pass. Try again!

However, if I use default imports like this:

const fs  = require("fs");
const { extname } = require("path");

module.exports = function filterFilesByExtension(directory, extension, callback) {
    function isExtension(file) {
        return extname(file) === "." + extension;
    }
    fs.readdir(directory, (err, files) => {
        if (err) {
            return callback(err);
        }
        const matchingFiles = files.filter(isExtension)
        callback(null, matchingFiles);
    });
}

It verifies correctly. I think this may be due to how you're mocking fs.readdir.

Not sure what the more flexible fix would be, but I'd be happy to help if you point me in the right direction!

@ccarruitero
Copy link
Contributor

Hi @reccanti , thanks for notice this.

In order to solve this, I think we can use a mocking library (like sinon) instead doing the mock manually.

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

Successfully merging a pull request may close this issue.

3 participants
@ccarruitero @reccanti and others