test-filesystem
v1.5.1
Published
Set up and tear down a temp directory for running filesystem tests.
Maintainers
Readme
test-fs
Set up and tear down a temp directory for running filesystem tests.
There are three options for accessing these functions:
- CommonJs modules: dist/*.js (default)
- ES6 modules: dist/*.mjs
- Bundles Browser file: browser/test-fs.js
Installation
In your project's root directory, run: npm install --save-dev test-filesystem
(or yarn add --dev test-filesystem if you use Yarn).
Usage
To use the temporary directory in tests. Do the following in your tests:
import { setUp } from 'test-filesystem'
// Setup the name for your temporary directory
const dirName = 'test-temp/'
// Use the name
setUp.setDefaults(dirName)
// use the beforeEach and afterEach methods of jest to run the setup and teardown functions
beforeEach(
() => setUp.beforeEach()
.then(
() => {
// Put any additional setup logic here
}
)
)
afterEach(
() => setUp.afterEach()
.then(
() => {
// Put any additional teardown logic here
}
)
)test-fs
An assortment of objects that can be used in tests and some functions to help debug and write tests.
Version: 1.0.0
Author: Joshua Heagle [email protected]
- test-fs
- .nodeTree : Object.<string, (string|Object|Array)>
- .multiReferenceObject : Object.<string, (string|number|Object)>
- .linkedList : Object.<string, (string|Object)>
- .jsonDom : Object.<string, (string|number|Array|Object)>
- .domItem : Object.<string, (string|number|Array|Object)>
- .deepReferenceObject : Object.<string, (string|number|Object)>
- .circularObject : Object.<string, (string|Object|Array)>
- .writePackageJson(dirPath, fields) ⇒ undefined
- .writeFixtureFile(filePath, content) ⇒ undefined
- .afterEach() ⇒ Promise.<*>
- .exports.afterEach([exists]) ⇒ Promise.<(*|void)>
- .exports.createTempDir() ⇒ Promise.<(*|void)>
- .exports.beforeEach([dirPath]) ⇒ void
- .removeDirectory(dirPath) ⇒ Promise.<*>
- .logObject(object, [label], [outputType], [forceOutputType]) ⇒ string | undefined
- .fileExists(filePath) ⇒ boolean
- .countMatches(content, search) ⇒ number
- .copyRealModules(destModulesDir, moduleNames, [sourceModulesDir]) ⇒ undefined
test-fs.nodeTree : Object.<string, (string|Object|Array)>
Sample NodeTree for testing circular references and arrays.
Kind: static constant of test-fs
test-fs.multiReferenceObject : Object.<string, (string|number|Object)>
Sample of an object containing multiple references.
Kind: static constant of test-fs
test-fs.linkedList : Object.<string, (string|Object)>
Sample LinkedList for testing circular references.
Kind: static constant of test-fs
test-fs.jsonDom : Object.<string, (string|number|Array|Object)>
Sample of jsonDom object containing an empty nested array and objects
Kind: static constant of test-fs
test-fs.domItem : Object.<string, (string|number|Array|Object)>
Sample of domItem child with nested child and optional details
Kind: static constant of test-fs
test-fs.deepReferenceObject : Object.<string, (string|number|Object)>
Sample object with deep references.
Kind: static constant of test-fs
test-fs.circularObject : Object.<string, (string|Object|Array)>
Multilayered node tree-like structure with parent references
Kind: static constant of test-fs
test-fs.writePackageJson(dirPath, fields) ⇒ undefined
Write a package.json file for a directory, creating any missing parent directories first. Parses and
re-serializes with a plain 2-space indent regardless of how fields was built, so callers never need to worry
about matching JSON formatting by hand.
Kind: static method of test-fs
| Param | Type | Description | | --- | --- | --- | | dirPath | string | The directory to write the package.json file into. | | fields | Object.<string, *> | The package.json fields to write. |
test-fs.writeFixtureFile(filePath, content) ⇒ undefined
Write a file, creating any missing parent directories first.
Kind: static method of test-fs
| Param | Type | Description | | --- | --- | --- | | filePath | string | The path of the file to write. | | content | string | The content to write into the file. |
test-fs.afterEach() ⇒ Promise.<*>
In the Jest.afterEach function call this one to clean up and remove the temp directory.
Kind: static method of test-fs
Returns: Promise.<*> - Resolves once the temp directory (tempDir, see setDefaults) has been removed.
test-fs.exports.afterEach([exists]) ⇒ Promise.<(*|void)>
Ensure that the del has completed, recursively attempt to delete and recreate
Kind: static method of test-fs
Returns: Promise.<(*|void)> - Resolves once the temp directory has been removed and recreated.
| Param | Type | Default | Description | | --- | --- | --- | --- | | [exists] | boolean | true | Whether the temp directory currently exists. Callers normally omit this; it's used internally to recurse until removeDirectory reports the directory is gone, then create it fresh. |
test-fs.exports.createTempDir() ⇒ Promise.<(*|void)>
In the Jest.beforeEach function call this one to set up the temp directory.
Kind: static method of test-fs
Returns: Promise.<(*|void)> - Resolves once the temp directory (tempDir, see setDefaults) has been created.
test-fs.exports.beforeEach([dirPath]) ⇒ void
Override the temp directory path used by afterEach, beforeEach, and createTempDir. Call this once, before your tests run, if the default ('test-temp/') doesn't suit your project.
Kind: static method of test-fs
| Param | Type | Default | Description | | --- | --- | --- | --- | | [dirPath] | string | null | The directory path to use for temp files instead of the default. Ignored (the existing default stays in effect) if falsy. |
test-fs.removeDirectory(dirPath) ⇒ Promise.<*>
Return a promise to be completed once the specified directory is deleted.
Kind: static method of test-fs
Returns: Promise.<*> - Resolves with dirPath once removed (or immediately, if it didn't exist); rejects with the
removal error otherwise.
| Param | Type | Description | | --- | --- | --- | | dirPath | string | The path of the directory to remove, if it exists. |
test-fs.logObject(object, [label], [outputType], [forceOutputType]) ⇒ string | undefined
Log out an object in a nicely formatted way.
Kind: static method of test-fs
Returns: string | undefined - The formatted string when outputType is 'string' (or forced to it); otherwise
undefined, since the object is logged directly to the console.
| Param | Type | Default | Description | | --- | --- | --- | --- | | object | Object | | The object (or any value) to log. | | [label] | string | "logging" | A label printed alongside the object, to identify this log call. | | [outputType] | string | "log" | Which console method to use ('debug'|'error'|'log'|'warn'), or 'string' to return a formatted string instead of logging. | | [forceOutputType] | boolean | false | If true, use specified output regardless of environment. |
test-fs.fileExists(filePath) ⇒ boolean
Detect if a file exists and is usable.
Kind: static method of test-fs
Returns: boolean - True if the file exists and is accessible.
| Param | Type | Description | | --- | --- | --- | | filePath | string | The path of the file to check. |
test-fs.countMatches(content, search) ⇒ number
Simple way to count string occurrences for testing.
Kind: static method of test-fs
Returns: number - How many times search occurs in content.
| Param | Type | Description | | --- | --- | --- | | content | string | The text to search within. | | search | string | The substring to count occurrences of. |
test-fs.copyRealModules(destModulesDir, moduleNames, [sourceModulesDir]) ⇒ undefined
Copy real, installed node_modules packages into a destination directory, for use as realistic test fixtures instead of hand-written stand-ins.
Kind: static method of test-fs
| Param | Type | Default | Description | | --- | --- | --- | --- | | destModulesDir | string | | The destination node_modules-style directory to copy each package into. | | moduleNames | Array.<string> | | The package names to copy. | | [sourceModulesDir] | string | "'./node_modules'" | The source node_modules directory to copy each package from. |
