0.0.2 • Published 4 years ago
real-test-dir v0.0.2
real-test-dir
a simple package wrapping the initialization and cleaning of the
Author
Xiao Zhang
Motivation
When I tested a typescript module using ts-jest, I came across a situation where the disk I/O needs to be tested. e.g. create a database file in a test directory and check whether it has been successfully created.
As multiple tests are being carried out in parallel, multiple such 'database file' may be created simultaneously. Therefore it is natural to design a pattern where all test files are created under the same temporary directory, which is deleted after all tests are done. Another requirement is that each individual 'database file' must have different name to avoid conflict.
Usage
npm i real-test-dir
typescript:
import * as path from 'path'
import { RealDir } from 'real-test-dir'
const root : string = path.resolve(__dirname, '_test_')
const realdir = new RealDir(root)
realdir.init(root)
.then() // The root directory has been created
.then(() => fsp.writeFile(realdir.subdir().next().value, 'hello world')) // create a new file with a unique name under the root directory whose content is 'hello world'
.then(() => realdir.reset()) // the root directory and all sub-directories are deleted
javascript
const path = require('path')
const { RealDir } = require('real-test-dir')
const root = path.resolve(__dirname, '_test_')
const realdir = new RealDir(root)
realdir.init(root)
.then() // The root directory has been created
.then(() => fsp.writeFile(realdir.subdir().next().value, 'hello world')) // create a new file with a unique name under the root directory whose content is 'hello world'
.then(() => realdir.reset()) // the root directory and all sub-directories are deleted