How to Write File-Based Tests With Real Files

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • mock-fs

    Configurable mock for the fs module

  • A lot of my past projects in some way had to do with file access. I started to test with mocking libraries like mock-fs, but soon recognized that they do not work for all cases, and sometimes you are using third party libraries internally that you cannot mock easily. So I thought of a different solution and the one I'm using right now for most projects actually uses real files.

  • with-local-tmp-dir

    Creates a temporary folder inside cwd, cds inside the folder, runs a function, and removes the folder. Especially useful for testing.

  • Why not use real files for testing instead of mocking? I built an NPM package called with-local-tmp-dir that basically creates a temporary subfolder inside cwd, cds into it, runs a function, and cds back to the previous cwd afterwards. In this function you can create files and pretty much anything, run your unit under test. Afterwards the folder is removed and everything is cleaned up. You actually do not solely need to use it for tests, you can use it anywhere, but it's mostly useful for tests.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • output-files

    Output a tree of files and directories by providing an object. Especially useful for testing with real files.

  • I also wrote another helper package output-files that creates a whole file tree at once by passing an object. It's much easier than writing a lot of fs.writeFile calls to create many files.

  • mocha

    ☕️ simple, flexible, fun javascript test framework for node.js & the browser

  • Alright, let's dive into it! First of all you need a testing framework. I'm going to use Mocha here, but you can also use Jest or any other framework of your choice. I'm also using expect for assertions. After that, we'll install some packages that we need to write our tests:

  • jest

    Delightful JavaScript Testing.

  • Alright, let's dive into it! First of all you need a testing framework. I'm going to use Mocha here, but you can also use Jest or any other framework of your choice. I'm also using expect for assertions. After that, we'll install some packages that we need to write our tests:

  • expect

    Write better assertions

  • Alright, let's dive into it! First of all you need a testing framework. I'm going to use Mocha here, but you can also use Jest or any other framework of your choice. I'm also using expect for assertions. After that, we'll install some packages that we need to write our tests:

  • fs-extra

    Node.js: extra methods for the fs object like copy(), remove(), mkdirs()

  • fs-extra to read files after running the scaffolding tool.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • endent

    ⬅️ An ES6 string tag makes indentation better

  • endent to declare multi-line strings.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts