@nexssp/test
Testing library, easy, many ways to accomplish testing..
Note
Users ask: "How to test binary from /bin/program" when it is not installed and we don't have it available in the shell.
If your 'bin executable' is as bin/nexssp-config.js:
const { join } = require("path");
const binFolder = join(process.cwd(), "bin");
module.exports = {
nexsstests: [
{
title: "display menu",
params: [
`node ${join(binFolder, "nexssp-config.js")}`,
/^\@nexssp.*config.*get\|g/s,
],
},
],
};
- NEW new function getNewTestFolder, createNewTestFolder
const { createNewTestFolder } = require("@nexssp/test");
const testFolder = createNewTestFolder();
const assert = require("assert");
const obj1 = { x: 1, y: { z: 1 } };
const obj2 = { x: 1, y: { z: 11 } };
assert.deepStrictEqual(obj1, obj2);
- NEW File checking exists, content
New tests: fileExists, notFileExists, fileHasContent, notFileHasContent
{
title: "File should have content",
type: "fileHasContent",
params: ["myfilename.txt","content in the file"],
},
- NEW - now you can also pass functions!
{
title: "File should have content",
type: "fileHasContent",
params: [
() => {
const filename = "xxx.txt";
const _fs = require("fs");
_fs.writeFileSync(filename, "works!");
return filename;
},
"works!",
],
},
- Another example of using functions
{
type: "equal",
params: [
() => {
const os = require("@nexssp/os");
return os.name();
},
process.platform === "win32"
? /(?:Windows)/
: /(?:Ubuntu|Alpine|somethingelse)/,
],
}
Just FAST, basic testing and code validator.
NOTE: This module is experimental! It works, but may have some issues.
Installation
npm i @nexssp/test -D
Example commands
nexssp-test --select=project
nexssp-test --ignore=languages
nexssp-test --ignore=languages --dry
nexssp-test --select=languages --debug
Package.json
Below example of testing
"scripts": {
"test": "nexssp-test --ignore=languages # will ignore languages.nexss-test.js",
"test:selected": "nexssp-test --ignore=languages --select=platform --debug # now will display with the details",
"test:continue": "nexssp-test --ignore=languages --continueOnError --debug # will not stop on errors",
"test:list": "nexssp-test --dry # just display files which are selected. ommiting ignored ones",
"test:languages": "nexssp-test --select=languages --debug",
"test:all:long": "nexssp-test",
},
Test types
Program types
- shouldContain - is used for cli command - default if type is not specified,
- shouldNotContain - the same as above but negative of result.
nexsstests: [
{
params: ["nexss", /"nexss":"(\d).(\d*).(\d*)"/,{
exitCode:1,
keepchdir: "MyTestProject",
}],
},
],
- equal, match - is used to compare values. (also you can use regular expression, see above example)
- notEqual, notMatch - negative of equal, match
nexsstests: [
{
title: "Should be equal",
type: "equal",
params: ["XXXX", /XXXX/],
},
];
Difference between .nexss-test.js and nexss-assert.js tests
Example of .nexss-test.js
.nexss-test.js have more automatic things done like create test folder
const commandBinPath = require("path").resolve(
__dirname,
"../bin/nexssp-command.js"
);
module.exports = {
nexsstests: [
{
type: "shouldContain",
params: [`node ${commandBinPath}`, /add.*command.*delete.*list.*/s],
},
],
};
Example of .assert-test.js
This example assert test is doing exacly the same as above *.nexss-test.js. As you can see you have more control here on what is done. It is really up to you how you will use them.
Assert test are just program, but there can be used for example great NodeJS library assert. But you can have a choice what you want to use it there. But these files are also taken to the overall statistics of @nexss/test
const assert = require("assert");
const { nSpawn } = require("@nexssp/system");
process.chdir(__dirname);
const commandBinPath = require("path").resolve("../bin/nexssp-command.js");
const { createNewTestFolder } = require("@nexssp/test");
const testFolder = createNewTestFolder();
console.log(`@test: ${testFolder}`);
process.chdir(testFolder);
const result1 = nSpawn(`node ${commandBinPath}`);
assert.match(result1.stdout, /add.*command.*delete.*list.*/s);
More Examples
- For more see Nexss Programmer's tests folder in the other @nexssp packages..