Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

poppinss/dev-utils

Repository files navigation

Dev Utils

Collection of development utilities.

gh-workflow-image typescript-image npm-image license-image synk-image

A collection of utilities to make testing easier when developing packages. The package is written specially to cater the needs of the AdonisJS core team.

Table of contents

Available Utilities

Installation

Install the module from npm registry as follows:

npm i @poppinss/dev-utils

# yarn
yarn add @poppinss/dev-utils

Filesystem

When writing tests, you may want to create some Javascript, or JSON files and then remove them after each test.

The process seems straight forward, until you realize that Node.js caches the script files and removing a file from the disk, doesn't removes it from Node.js cache.

The problem

test('do something', async () => {
  await fsExtra.outputFile('foo.js', `module.exports = 'foo'`)

  // test code
  await fsExtra.remove('foo.js')
})

test('do something different', async () => {
  await fsExtra.outputFile('foo.js', `module.exports = 'bar'`)

  require('foo.js') // returns 'foo' (because the file is cached)
})

The solution

The Filesystem class exported by this module takes care of removing the module from the cache, when you remove it from the disk. It does this for .js, .ts and .json files.

import { join } from 'path'
import { Filesystem } from '@poppinss/dev-utils'
const fs = new Filesystem()

test.group((group) => {
  group.afterEach(async () => {
    await fs.cleanup()
  })

  test('do something', async () => {
    await fs.add('foo.js', `module.exports = 'foo'`)
    require(join(fs.basePath, 'foo.js')) // 'foo'
  })

  test('do something', async () => {
    await fs.add('foo.js', `module.exports = 'bar'`)
    require(join(fs.basePath, 'foo.js')) // 'bar'
  })
})

The fs.cleanup method removes all the files created via fs.add and also removes the modules from the cache.