🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

@poppinss/dev-utils

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@poppinss/dev-utils

Development utilities

2.0.3
latest
Source
npm
Version published
Maintainers
1
Created
Source

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.

Keywords

dev

FAQs

Package last updated on 04 Apr 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts