Skip to content
This repository was archived by the owner on Dec 8, 2024. It is now read-only.

jasmine-node #119

Open
louis-quaintance opened this issue Nov 22, 2013 · 7 comments
Open

jasmine-node #119

louis-quaintance opened this issue Nov 22, 2013 · 7 comments

Comments

@louis-quaintance
Copy link

Hi all,

Has anyone managed to get jasmine-node working with istanbul

At the moment when I run jasmine-node alone my tests pass:

jasmine-node --runWithRequireJs --captureExceptions --requireJsSetup test/require.config test/specs/*

However when I run it with istanbul:

istanbul cover jasmine-node --runWithRequireJs --captureExceptions --requireJsSetup test/require.config test/specs/*

I get this error:

{ [Error: Cannot find module 'requirejs'] code: 'MODULE_NOT_FOUND' }

Any ideas?

Sorry if I am doing it wrong

@gotwarlost
Copy link
Owner

Try the --hook-run-in-context flag to istanbul cover for requireJS modules to be instrumented correctly.

@gotwarlost
Copy link
Owner

@louisandkatherine did this work for you?

Actually there is another problem with your istanbul command. You need to separate out the args you want to pass to the command from the args you want to pass to istanbul using a double-dash in between.

i.e.

$ istanbul cover  --hook-run-in-context jasmine-node -- --runWithRequireJs --captureExceptions --requireJsSetup test/require.config test/specs/*

Note the -- between jasmine-node and --runWithRequireJs

@louis-quaintance
Copy link
Author

Thanks so much for replying, this got it to run however the coverage is inaccurate unfortunately.

It only analyses the unit test js files that don't pull in additional dependencies. I think it is to do with the little script I am using to mock out dependencies when testing a js file in isolation, here is the script that mocks everything else out:

(could this be the problem?)

var fs = require('fs'), path = require('path'), stacktrace = require('test/lib/stack-trace'), vm = require('vm');

define(function(require) {

    return function(module, mocks, globalVars) {

        mocks = mocks || {};
        // 3rd object in stacktrace array is calling file
        var caller = stacktrace.get(this)[2].getFileName();

        var resolve = function(module, base) {
            if (module.charAt(0) !== '.')
                return module;
            return path.resolve(path.dirname(base), module) + '.js';
        };

        var exports = {};

        var sandbox = {
            require : function(name) {
                if (mocks[name]) {
                    return mocks[name];
                } else {
                    var file = path.join(path.dirname(caller), path.dirname(module), name);
                    return require(file);
                }
            },
            console : console,
            exports : exports,
            module : {
                exports : exports
            },

        };

        for (var globalVar in globalVars) {
            sandbox[globalVar] = globalVars[globalVar];
        }

        var filepath = resolve(module, caller);

        vm.runInNewContext(fs.readFileSync(filepath), sandbox);

        return sandbox.module.exports;
    };
});


From: Krishnan Anantheswaran [email protected]
To: gotwarlost/istanbul [email protected]
Cc: louisandkatherine [email protected]
Sent: Wednesday, 27 November 2013, 20:33
Subject: Re: [istanbul] jasmine-node (#119)

@louisandkatherine did this work for you?
Actually there is another problem with your istanbul command. You need to separate out the args you want to pass to the command from the args you want to pass to istanbul using a double-dash in between.
i.e.
$ istanbul cover --hook-run-in-context jasmine-node -- --runWithRequireJs --captureExceptions --requireJsSetup test/require.config test/specs/*
Note the -- between jasmine-node and --runWithRequireJs

Reply to this email directly or view it on GitHub.

@gotwarlost
Copy link
Owner

istanbul works by hooking require and, optionally, vm.runInThisContext - you are using vm.runInNewContext while also sandboxing globals (istanbul needs one global to write coverage information). I'm not surprised that this is not working.

@louis-quaintance
Copy link
Author

Do u know of another way to load requirejs modules, mock out dependencies I.e. set them to be jasmine spies, inject global vars and get istanbul to work?

Thanks again!!!!

Sent from Yahoo Mail on Android

@webuniverseio
Copy link

Hi @gotwarlost , can you please give me a hint what might be wrong? I'm using Windows8 and here is what I get in console when I try to cover jasmine-node:

> istanbul cover -v --hook-run-in-contex node_modules/jasmine-node/bin/jasmine-node -- --match "test" test

Using configuration
-------------------
verbose: true
instrumentation:
    root: .
    default-excludes: true
    excludes: []
    embed-source: false
    variable: __coverage__
    compact: true
    preserve-comments: false
    complete-copy: false
    save-baseline: false
    baseline-file: ./coverage/coverage-baseline.json
    preload-sources: false
reporting:
    print: summary
    reports:
        - lcov
    dir: ./coverage
    watermarks:
        statements: [50, 80]
        lines: [50, 80]
        functions: [50, 80]
        branches: [50, 80]
    report-config:
        clover: {file: clover.xml}
        cobertura: {file: cobertura-coverage.xml}
        json: {file: coverage-final.json}
        json-summary: {file: coverage-summary.json}
        lcovonly: {file: lcov.info}
        teamcity: {file: null}
        text: {file: null, maxCols: 0}
        text-summary: {file: null}
hooks:
    hook-run-in-context: true
    post-require-hook: null
    handle-sigint: false

-------------------

Running: node D:\sz\start-template\permissions\node_modules\jasmine-node\bin\jasmine-node --match test test
......

Finished in 0.006 seconds
6 Tests, 0 Failures, 0 Skipped

No coverage information was collected, exit without writing coverage information

If you want to try it yourself my package is npm i simple-permissions

@silkentrance
Copy link

@gotwarlost basically a usage issue and very old by that. Please close this to raise overall confidence in this nearly perfect tool again.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants