Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
jscoverage modified to support SSJS
C++ C Shell JavaScript Perl Python Other
branch: master

This branch is 61 commits ahead, 6 commits behind tj:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
doc
example
js
m4
tests
COPYING
Makefile.am
Makefile.in
Makefile.node
Readme.md
aclocal.m4
build.sh
config.guess
config.h.in
config.rpath
config.sub
configure
configure.ac
depcomp
encoding.c
encoding.h
generate-resources.c
global.h
highlight.c
highlight.h
http-connection.c
http-exchange.c
http-host.c
http-message.c
http-server.c
http-server.h
http-url.c
index.js
install-sh
instrument-js.cpp
instrument-js.h
instrument.c
instrument.h
jscoverage-help.txt
jscoverage-highlight.css
jscoverage-ie.css
jscoverage-overlay.js
jscoverage-server-help.txt
jscoverage-server.1
jscoverage-server.c
jscoverage-throbber.gif
jscoverage.1
jscoverage.c
jscoverage.cpp
jscoverage.css
jscoverage.html
jscoverage.js
jscoverage.jsm
jscoverage.manifest
jscoverage.xul
make-bin-dist.sh.in
make-dist.sh.in
missing
package.json
report.js
resource-manager.c
resource-manager.h
stream.c
stream.h
test.js
test_case.js
util.c
util.h
wscript

Readme.md

node-jscoverage

JScoverage for node. Have been Modified to works both for binary executables and node.js modules.

Installation

for use in node.js modules:

  $ npm  install jscoverage

for use by binary executables just like the source version, just pull it to local and:

  $ ./configure && make && make install

How to Use

  /** for nodejs **/
  var jsc = require('jscoverage');
  require = jsc.require(module); // rewrite require function
  // pay attention to the second param, means do jscoverage process for this module
  var test_module = require('module for test',true); 

  describe('module',function(){
     describe('#func',function(){
        it('some test',function(){
           //TODO test code here
        });
     });
  });
  // output the coverage when test process exit, you can custom your own coverage report 
  // because the jscoverage report is really easy to DIY
  process.on('exit',function(){
     jsc.coverage();
  });

  /** 
     for javascript in browser,you need a node server to hold your js code,
     when browser request the js file, using jscoverage to process the file content,
     then output the jscoverage injected code .
  **/
  http.create(function(req,res){
     var path = wwwroot + req.url;
     // process single file
     var content = jsc.processFile(path,encode);
     res.end(content);
  }

API

  jscoverage.processFile(path, encode): process single file, will return the process results
     path: the file to process
     encode: the charset of the file or content , encode: utf-8,gbk

  jscoverage.process(filename, content, encode): process content input, will return the results
     filename: this param is needed for _$jscoverage[filename], it is used as a key name.
     content: script content,string type
     encode: input content encode

  jscoverage.processDir(source_path, dest_path, exclude, exclude_file, options): process every js file in a dir
     source_path: the dir to process, support relative path
     dest_path: the output dir to put result files
     exclude: the dir to exclude, like '.svn|.git'
     exclude_file: the file to exclude '.npmignore|.vimrc'
     options: {}

  jscoverage.require(module): automatic process when flag is true, will mock the node.js`s require
     return the mocked require function ,which call as : 
        var mod = require(path,isInstrument);
  jscoverage.coverage(): show coverage rate on console
     you can custom this function to make your own coverage report!

test private functions in a module

  ======== mo.js ========
  function inner(a,b){
     return a+b;
  }
  exports.mo = function(){}

  ======= test.js =======
  var test_mo = require('./mo.js', true);
  test_mo._test('inner',[1,2]); // test private function inner
  var inner = test_mo._get('inner');
  inner(1,2); // call the private inner function of the mo module 

extend API

  InstrumentedMod._test(funcName,args);
     test inner functions
  InstrumentedMod._call(funcName,args);
     same as _test
  InstrumentedMod._replace(objName,value);
     replace the inner object
  InstrumentedMod._get(objName);
     get the inner object;
  InstrumentedMod._reset(objName);
     rollback the object replace by InstrumentedMod._replace();

  jsc.config(opt);
     you can re-defined the above extend api by call this config function
     opt has properties:
        call:
        test:
        get:
        replace:
        reset:

  jsc will test if your module is conflict with this inject functions , it is save!
Something went wrong with that request. Please try again.