paint-brush
Mocking File System In Node.jsby@sanketmeghani
8,754 reads
8,754 reads

Mocking File System In Node.js

by Sanket MeghaniJanuary 30th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Recently I created a command line utility in JavaScript which parses the file system. I was exploring options to write unit tests for the utility. Since the utility looks at the file system (directories and files), one of the option was to create required directories and files using node’s fs module before the tests execution starts and delete them after the tests execution is completed.

Company Mentioned

Mention Thumbnail
featured image - Mocking File System In Node.js
Sanket Meghani HackerNoon profile picture

Recently I created a command line utility in JavaScript which parses the file system. I was exploring options to write unit tests for the utility. Since the utility looks at the file system (directories and files), one of the option was to create required directories and files using node’s fs module before the tests execution starts and delete them after the tests execution is completed.

The above approach would work, but is dependent on file system and could leave the file system dirty if it could not delete the files in the end. We also need to take care of platform specific paths.

While exploring alternative approach, I came across mock-fs. As mentioned on the github page, this library allows us to mock fs module to use memory as file system instead of actual file system. This lets us run tests against a set of mock files and directories instead of lugging around a bunch of test fixtures.

Example

To mock the file system, we need to call the mock function provided by mock-fs with the file system structure we want to create.

import mock from 'mock-fs'

mock({
  'sample-file.txt': 'This is file content',
  'path': {
    'to': {
      'sub-dir': {
        'another-file.md': 'Markdown content'
      }
    }
  }
})

The above code would create a temporary file system in memory with sample-file.txt in current directory and another-file.md in path/to/sub-dir directory in current directory.

As you might have noticed, if the value of config is a string, then it results into a file with corresponding key as filename and value as file content. If the config value is an object, then it results into a directory.

Once mock is configured, we can use regular fs module and it’s apis to perform any file operations on above mocked file system as usual.

When, we are done with the testing, we need to restore the fs binding to real file system by using restore function.

mock.restore()

Conclusion

I think mocking file system using mock-fs is cleaner, flexible and light-weight approach while writing tests for code dealing with file system.

Please refer official github page of mock-fs for detailed apis and some of the advanced configuration options available.

Do share if you have used or are aware about alternative approaches along with it’s pros/cons.