Collection of development utilities.
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.
Install the module from npm registry as follows:
npm i @poppinss/dev-utils
# yarn
yarn add @poppinss/dev-utils
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.
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 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.