@poppinss/dev-utils
Development utilities
Last updated 10 days ago by virk .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @poppinss/dev-utils 
SYNC missed versions from official npm registry.

Dev utils

Collection of development utilities.

circleci-image typescript-image npm-image license-image

This module exposes a collection of utilities that can be used during tests for easy development.

Table of contents

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.

API Docs

The API docs are generated using Typedoc.

Maintainers

Harminder virk

Current Tags

  • 1.0.2                                ...           latest (10 days ago)

3 Versions

  • 1.0.2                                ...           10 days ago
  • 1.0.1                                ...           3 months ago
  • 1.0.0                                ...           6 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 9
Last Day 0
Last Week 7
Last Month 35
Dependencies (3)
Dev Dependencies (17)

Copyright 2014 - 2016 © taobao.org |