Code Monkey home page Code Monkey logo

ghostface's Introduction

ghostface

Evaluate javascript in PhantomJS, and print the output.

Build Status npm install js-standard-style

Inspired by jsdom-eval, this module makes it trivial to run tests in a headless browser.

Installation

npm install -g ghostface will expose the ghostface command in your environment.

ghostface does not include phantomjs as a dependency; you should install that however works best in your environment, and ghostface will find it so long as it exists in your $PATH.

ghostface supports phantomjs versions ^1.9.0 or ^2.0.0.

Example

Imagine a browserify project that you're testing using tape:

// file: test.js

var test = require('tape')

test('always passes', function(t) {
  t.plan(1)
  t.pass()
})

Run it with ghostface:

$ browserify test.js | ghostface | tap-set-exit

See the output printed to the console:

TAP version 13
# always passes
ok 1 (unnamed assert)

1..1
# tests 1
# pass  1

# ok

Note: In the example above, we pipe output to tap-set-exit, which parses TAP output and sets the correct exit codes; ghostface is just evaluating javascript, it doesn't know/care what the output is, or how to set the correct exit codes.

Usage

$ ghostface --help

Usage: ghostface [options] [file]

file    Javascript file to be evaluated

Options:
  -h --html            The HTML file to be used as the page template
  -t --timeout         Milliseconds to wait for output before stopping execution. Default 1000
  -f --forever         Ignore the timeout and wait forever
  -p --phantomPath     Specify the path to the phantomjs executable
  --help               Show this message
  --version            Print version and quit

You can specify the JS file to be evaluated, or pipe a JS file directly into ghostface. For an imaginary file.js in the current directory, these two commands are equivalent:

$ ghostface file.js
$ cat file.js | ghostface

The JS you are evaluating will be done in a blank web page. If you want to provide your own context, use the --html <filename> option to load your own html; the JS will be evaluated after the page loads.

By default, execution is stopped if no console output is seen for over 1000ms. You can override this timeout with --timeout <ms>, or choose to run forever with --forever. Note you will have to send a SIGHUP to end execution.

License

This project is licensed under the Apache License, Version 2.0. See LICENSE for the full license.

ghostface's People

Contributors

awinterman avatar fardog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

jarofghosts

ghostface's Issues

No differentiation between a phantom crash and a timeout

When phantom crashes, or when a timeout occurs, there is no difference to the CLI, it just exits with code 1; there should be a difference between a timeout, and we likely should just be passing the phantom error code directly through (rather than coercing it into a 0/1).

CLI exit ternary ignores code

There is an issue with your ternary at: https://github.com/fardog/ghostface/blob/master/bin/ghostface.js#L45

function onExit(code, signal) {
  if(code > 0) {
    console.error(sprintf('\nphantomjs exited abnormally: %d'), code)
  }

  process.exit(code || signal === 'SIGTERM' ? 0 : 1)
}

Basically you're making the exit code 0 if code != 0! (if code is truthy OR signal == 'SIGTERM' then 0 else 1)
You probably want this?

process.exit(code || (signal === 'SIGTERM' ? 0 : 1))

Odd behaviour when spawning ghostface with child_process.spawn

Hi there,

I'm trying to spawn ghostface using child_process.spawn().
It's strange because none of the output is getting piped to the console, nor do any exit events trigger.

My guess is that the main ghostface isn't the process actually producing the output, nor the exit codes. Is there some other sub-process generating the output?

var spawn = require('child_process').spawn;
var spawned_process;

spawned_process = spawn('./node_modules/.bin/ghostface', ['test/browser_test_compiled.js']);
spawned_process.stdout.pipe(process.stdout);
spawned_process.stderr.pipe(process.stderr);

spawned_process.on('exit', function(code, signal){
    console.log("EXITED with code", code);
});

spawned_process.on('error', function(err){
    console.log("ERROR", err);
});

The same setup works for things like browserify, for example if I do:

spawned_process = spawn('./node_modules/.bin/browserify', ['test/browser_tes.js']);

It outputs the stdout and exits with a 0.

Any thoughts?

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.