"Yes! I have seen the files!"
Library for testing file trees. Made to unit test templating libraries such as fileable and jsonnet.
npm install --save testafile
const { validate } = require('testafile');
const tape = require('tape');
tape('basic test', ({ ok, end }) => {
ok(...validate('.', [
{
name: 'readme.md',
message: 'readme.md should exist'
},
{
name: 'package.json',
message: 'package.json should exist'
},
{
name: 'index.js',
message: 'index.js should exist'
},
{
name: 'test',
missing: false,
children: [{
name: 'index.js',
message: 'test/index.js should exist'
}],
message: 'test should exist'
}
]), { message: '' })
end();
});
Testifile has two exports: validate and validateError
For usage with testing libraries like tape and jest.
validate(path, tree, {message});
Returns an array: [result, resultMessage]
Path of folder to validate
Representation of desired file tree
Description of test
Result of test
Messages concatinated with messages from sub-tests.
const { validate } = require('testafile');
const tape = require('tape');
const parentFolder = './dist';
const tree = [/*...*/];
tape('basic test', ({ ok, end }) => {
ok(...validate(parentFolder, tree, { message: 'it should work' });
});
const { validate } = require('testafile');
const tape = require('tape');
const parentFolder = './dist';
const tree = [/*...*/];
test('basic test', () => {
expect(validate(parentFolder, tree, { message: 'it should work' })[0]).toBe(true);
});
Note: Because This is a single test, messages are concatinated. This can get messy due to the way that some testing libraries (tape) handle new-line characters. It's useful, however; as new lines do render properly when tests fail -- allowing one to easily hone in on the error.
Works like validate, but throws an error if the test fails and returns the message if it passes.
const { validateError } = require('testafile');
const tape = require('tape');
const parentFolder = './dist';
const tree = [/*...*/];
tape('basic test', ({ ok, end }) => {
try{
const message = validateError(parentFolder, tree, { message: 'it should work' });
ok(true, message);
}catch({message}){
ok(false, message);
}finally{
end();
}
});
The desired file tree is represented by an array of objects.
Each object represents a file or a folder and may have a number of specific field.
The only reqiured field - this is the name of the object.
Used as a result message of sub test.
String content of file will be tested to match this exactly.
Array containing objects describing strings to be found in file.
Each object has two fields:
The string to be found.
The index at which to find the string.
A function that test the content (buffer). Must return 'true' to pass.
String content of file will be tested agains a specific format.
Note: o formats are currently supported :/
Mode of the file to match.
If set to true, the object must be a file to pass the test.
If set to true, the object must be a folder to pass the test.
Instead of checking if a file exists,
setting this to true will check that it DOES NOT exist.
Array of children of a folder. Objects within the folder follow this same format.