Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@nexssp/test

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nexssp/test

Just test and validate your code, FAST and easy.

  • 1.1.16
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

@nexssp/test

Testing library, easy, many ways to accomplish testing..

image

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();
  • NEW file types:

  • *.nexss-assert.js - create files with extension .nexss-assert.js and put testing there .. You can use there also great NodeJS assert library. For more please look at compare function test in this repository tests\compare.nexss-assert.js. Results from this type are also taken to the overall statistics of the @nexssp/test

// compare.nexss-assert.js
const assert = require("assert"); // GREAT NodeJS library for testing
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

// Basic Example
{
  title: "File should have content",
  type: "fileHasContent",
  params: ["myfilename.txt","content in the file"],
},
  • NEW - now you can also pass functions!
// Advanced Example (use of function): test creates file and test for its content.
{
  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
// Example of the test with function
{
  type: "equal",
  params: [
    () => { // function to check (function MUST return value to check)
      const os = require("@nexssp/os");
      return os.name();
    },
    process.platform === "win32" // check the result by regular expression (or string)
      ? /(?: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 # install for devDependencies

Example commands

nexssp-test --select=project # will only perform project.nexss-test.js
nexssp-test --ignore=languages # which should be ignored, can be array.
nexssp-test --ignore=languages --dry # not running tests, displays only test files which can be run without --dry option
nexssp-test --select=languages --debug # displays all data which are happening during tests. great dev helper.

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: [
    {
      // it will run command nexss and compare with the specified regexp.
      params: ["nexss", /"nexss":"(\d).(\d*).(\d*)"/,{
        exitCode:1, // if not specified 0 will be checked
        // chdir: "MyTestProject", // only once will change dir
        keepchdir: "MyTestProject", // will keep changing dir on the next tests in that file.
      }],

    },
],
  • 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", // optional / if not exists param[0] will be used for title.
    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");

// We create test dolder
const { createNewTestFolder } = require("@nexssp/test");
const testFolder = createNewTestFolder();
console.log(`@test: ${testFolder}`);
process.chdir(testFolder);
// Default help
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..

Keywords

FAQs

Package last updated on 11 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc