Giter Site home page Giter Site logo

process-test's Introduction

process-test

Easy way to test command line applications

NPM version Build Status Coverage percentage

This project is mainly based on coffee but implemented in TypeScript with no dependencies.

Install

npm install @b4dnewz/process-test

Usage

You can use it in your tests with any framework to test your code execution:

import {fork, spawn} from "@b4dnewz/process-test"

// Spawn a nodejs process
it("will spawn node script", (done) => {
  fork("./test.js", ["foo", "bar"], {})
    .expect("stdout", /foo bar/)
    .expect("code", 0)
    .end(done);
})

// Spawn a system command
it("will spawn system command", (done) => {
  spawn("node", ["--version"], {})
    .expect("stdout", process.version)
    .expect("code", 0)
    .end(done);
})

File used in usage example test.js

#!/usr/bin/env node

const argv = process.argv.slice(2).join(" ")
process.stdout.write(argv);

API

fork

This method is used specifically to spawn new Node.js process using the given file path, arguments and options.

// fork(binPath)
fork("./test.js")

// fork(binPath, args)
fork("./test.js", ["--foo", "bar"])

// fork(binPath, args, options)
fork("./test.js", ["foo"], { env: {} })

spawn

This method spawns a new process using the given command, arguments and options.

// spawn(binPath)
spawn("node")

// spawn(binPath, args)
spawn("node", ["--version"])

// spawn(binPath, args, options)
spawn("ls", ["-l"], {
  cwd: "/home"
})

All the methods returns a test Process class which has the following method to interact with the process and the result:

expect(type, expectation)

This method is used to set an expectation of the process result.

spawn("node", ["--version"])
  .expect("stdout", new RegExp(process.version))
  .expect("code", 0);

notExpect(type, expectation)

This method is used to set an expectation of the process result.

spawn("node", ["--version"])
  .notExpect("code", 1);

License

MIT © Filippo Conti

process-test's People

Stargazers

 avatar

Watchers

 avatar  avatar

process-test's Issues

Assertion errors are cathed from end callback

While using this package on tests I realized the assertion error raised from jest (in my case) was catched by the try/catch block where the end callback is called, more precisely from this code.

process-test/src/process.ts

Lines 292 to 296 in 542abd8

if (this.endCb) {
this.endCb(undefined, result);
} else {
this.emit("complete_success", result);
}

Simply moving that part into a finally block is the solution.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.