Collection of Node.js Test Helpers.
TestEnv
sets up up a read/write environment that can be easily accessed,
populated and then cleaned up after a test run.
npm install @mhio/test --save-dev
yarn add @mhio/test --dev
TestEnv expects test
to contain you test files by default. Fixtures are expected to be in test/fixture
. Output goes into test/output
.
These paths can be overridden when you setup your TestEnv
.
See examples for more
let test_env, out
before('Copy `files` from fixtures', function(){
// Create a test environment from a file in your `test/` directory.
test_env = TestEnv.setupTestDir(__dirname)
// Create an output area called `newdir` in `test/output/newdir`.
out = test_env.output('newdir')
// Copy `test/fixture/files` to `test/output/subdir`
return out.copyFrom('files', 'subdir')
})
after('Cleanup', function(){
return out.clean()
})
it('should now have fixtures in the output directory', function(){
let dir = out.path('subdir')
expect( dir ).to.be.a.directory().with.contents(['firstfile'])
})
it('should write a file to output', function(){
let file = out.path('subdir', 'testfile')
return fs.writeFileAsync(file, 'data\n').then( ()=> {
expect( path.join(__dirname,'output','newdir','subdir','testfile') ).to.be.a.file()
})
})
Setting DEBUG_CLEAN=true
in your environment prevents cleanup so
you can inspect files after the tests have run.
DEBUG_CLEAN=true mocha -b
- TestEnv.setup()
- TestEnv.setupTestDir()
- TestEnv.join()
- TestEnv.resolve()
- basePath()
- fixturePath()
- outputPath()
- tmpOutputPath()
- randomHex()
- cleanAsync()
- cleanAllOutputAsync()
- cleanOutputAsync()
- cleanAllOutputTmpAsync()
- cleanOutputTmpAsync()
- mkdirOutputAsync()
- mkdirOutputTmpAsync()
- removeTmpPrefixFromPath()
- copyAsync()
- copyFixtureToTmpOutputAsync()
- copyFixtureToOutputAsync()
Create a path from the base path of this Path instance
Promise to copy a file from a source to destination
Promise to copy data to this Output or path from a Fixture or path
Source and destination can be:
undefined
/null
- Parents fixture path.String
- Parents fixture path + strArray
- Parents fixture path join with ArrayTestEnvPath
- TEP's base bath
Promise to clean up file system contents of this Path instance
Promise to copy data from this Fixture or path to an Output or path
Source and destination can be:
undefined
/null
- Parents fixture path.String
- Parents fixture path + strArray
- Parents fixture path join with ArrayTestEnvPath
- TEP's base bath
Requires the path to your app directory that contains test
.
The fixture
and output
directories will be in test
.
See TestEnv.setupTestDir( __dirname )
for quicker setup from
your inside your tests.
Options
-
base_path
The project/apps base path. Take a guess if the user doesn't provide one. The guess removes thenode_modules/@mhio/test/lib
dirs. -
test_dir
This directory will be appended tobase_path
that contains your tests. Defaults to:test
-
fixture_dir
This directory will be appended totest_dir
and contains your fixtures. Defaults to:fixture
-
output_dir
This directory will be appended totest_dir
for test output. Defaults to:output
-
tmp_output_dir_prefix
The prefix used for a temp dirs in output. Defaults totmp-
Same as above but accepts the test directory instead of the app path.
Allows TestEnv.setupTestDir( __dirname )
from test files.
Return a directory path from TestEnv base_path
.
Joins all arguments with path.join
Return a directory path from the fixture/
path
Joins all arguments with path.join
TestEnv.fixturePath('a', 'b')
// = '/project/test/fixture/a/b'
Return a directory from the output/
path
Joins all arguments with path.join
TestEnv.outputPath('one', 'two')
// = '/project/test/output/one/two'
Return a random tmp dir path in the output path
TestEnv.tmpOutputPath('blah', 'one', 'two')
// = '/project/test/output/tmp-blah/one/two'
Create a random hex string n chars long
TestEnv.randomHex(5)
// = 'c8fd2'
Promise to clean a directory that must be inside the base path.
DEBUG_CLEAN
makes this skip the removals
Promise to clean everything in the output/
dir
DEBUG_CLEAN
makes this skip the removals
Promise to clean a named output/subdir
DEBUG_CLEAN
makes this skip the removals
Promise to clean any tmp-*
dirs created (Named with tmp_output_dir_prefix
)
DEBUG_CLEAN
makes this skip the removals
Promise to clean a named output/tmp-suffix
dir
DEBUG_CLEAN
makes this skip the removals
Promise to make the named directorys in output/
.
Promise to make a temp directory output/tmp-${suffix}
.
Remove the current temp directory from a path
TestEnv.removeTmpPrefixFromPath('/project/test/output/tmp-output/whatever')
// = 'output/whatever'
Promise to copt a directory to a destination
Promise to copy a fixture/
path to output/tmp-{random}
Promise to copy a fixture/
path to output/
TestEnv.copyFixtureToOutputAsync('config', 'somedir')
// = cp -r /project/test/fixture/config /project/test/output/somedir
@mhio/test is released under the MIT license.
Copyright 2019 mhio