5

I'm trying to write some simple tests on JS code that can be run from the command line to test code that has nothing to do with HTML, documents or user interface. To do this I need to include one file within another to pull the code being tested into the test script. What I've found involved HTML or DOM to do the job; for example, something like document.write( ) or some such. Is there a simple way to do this? I'm imaging something like include( "code2test.js" ); I'd appreciate any help in solving this. Can JQuery help or does it have to be used in a HTML/browser context?

Thanks in advance.j

1
  • What are you using to run the tests? Commented Mar 19, 2013 at 2:19

4 Answers 4

1

It sounds like you just need two script tags:

<script type="text/javascript" src="code2test.js"></script>
<script type="text/javascript" src="testscript.js"></script>

If that doesn't work, try reversing the order.

Sign up to request clarification or add additional context in comments.

4 Comments

I think he's asking how to include a javascript on the webpage AFTER its loaded, via command line, not how to include it in the html.
No, he's talking about running javascript from the command line, not in a web page at all.
In that case, you need to append the script to the document: document.getElementByTagName("head")[0].appendChild(document.createElement("script")).src = "code2test.js";
no, according to JacobM, OP is asking for a way to test javascript without going through a browser.
1

Sounds like what you need is Require.js. It's designed to allow inclusion of javascript in the web page using javascript rather than script tags. For example, instead of:

<script scr="foo.js"></script>
<script>
    use_foo_here();
</script>

using Require.js you can write:

require(["foo.js"],function(foo){
    use_foo_here();
})

The cool thing about Require.js is that it can even be used on Node.js. So for command line invocation you can use Node to run your scripts and the require() statements would work just like it does on the web page.

Comments

0

If you just want access to a javascript engine (eg Webkit) without using a browser, you can use something like nodeJS or PhantomJS

Comments

0

We are using Chutzpah as our test runner, and are very satisfied with it. We write the tests using Jasmine. Jasmine does not require a DOM.

At the start of the test file, the references to the JavaScript files under test are added, like this:

/// <reference path="dependantModule.js" />
/// <reference path="code2test.js" />

and then the test code follows:

describe("code2test test suite", function () {

    it("should do something"", function () {
        var result;

        // Assuming code2test.js exposes a global called 'code2test'
        result = code2test.doSomething();
        expect(result).toEqual("the expected result");
    });
});

Chutzpah uses the PhantomJS headless browser. So you can write tests that interact with the DOM if required.

We run the tests through Chutzpah from the command line for continuous integration, but also run them inside Visual Studio 2010 using the Chutzpah Visual Studio Extension. I believe integration of Chutzpah in VS2012 is even easier, but haven't tried it myself.

The tests can also be ran inside a 'real' browser, which is great for debugging. My browser of choice for debugging the test code is Chrome - the developer tools are great.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.