Unit Tests

Unit tests are short sections of code that test small pieces of functionality belonging to a larger program. By demonstrating that each piece of a program is correct, it is easier to infer that the entire program is correct.

Node.js uses unit tests extensively to validate scenarios while designing a program. Node.js Tools for Visual Studio includes support for discovering and executing unit tests. This allows you to author your tests and run them without having to switch to a command prompt.

Discovering Tests

Discovering Mocha Tests

To ensure your mocha test(s) can be found and run, follow these rules:

  • Install mocha locally. NTVS will not use a global mocha installation.

  • Set the Test Framework property for the JavaScript or TypeScript file to "Mocha".

  • Define your test functions using describe and it.

Blank tests that follow these rules can be added by selecting Project, Add New Item (CTRL+SHIFT+A) and choosing "JavaScript Mocha UnitTest file".

Add New Unit Test Item

This will add a new file containing a mocha unit test.

var assert = require('assert');

describe('Test Suite 1', function() {
    it('Test 1', function() {
        assert.ok(true, "This shouldn't fail");
    })

    it('Test 2', function() {
        assert.ok(1 === 1, "This shouldn't fail");
        assert.ok(false, "This should fail");
    })
})

The Test Framework property is automatically set to "Mocha".

Test Framework

Hit Build (CTRL+SHIFT+B), and the tests will be discovered and displayed in the Test Explorer. (If you do not see the Test Explorer window, click the Test menu, then Windows and Test Explorer.)

Test Discovery

Discovering Exported Tests

To ensure your exported test(s) can be found and run, follow these rules:

  • Set the Test Framework property for the JavaScript or TypeScript file to "ExportRunner".

  • Export your test functions

Blank tests that follow these rules can be added by selecting Project, Add New Item (CTRL+SHIFT+A) and choosing "JavaScript UnitTest file".

Add New Unit Test Item

This will add a new file containing a basic unit test.

var assert = require('assert');

exports['Test 1'] = function (test) {
    assert.ok(true, "This shouldn't fail");
}

exports['Test 2'] = function (test) {
    assert.ok(1 === 1, "This shouldn't fail");
    assert.ok(false, "This should fail");
}

The Test Framework property is automatically set to "ExportRunner".

Test Framework

Hit Build (CTRL+SHIFT+B), and the tests will be discovered and displayed in the Test Explorer. (If you do not see the Test Explorer window, click the Test menu, then Windows and Test Explorer.)

Test Discovery

Test Explorer Groups/Filters

As you add more tests to your project, you may prefer to group or filter the tests that are displayed. The "Group By" menu on the toolbar will allow you to collect your tests into different groups, and the search toolbox will filter by matching names. Double-clicking a test will open the source file containing the test implementation.

Group By Traits

Running Tests

Tests can be run by clicking "Run All" in the Test Explorer window, or by selecting one or more tests or groups, right-clicking and selecting "Run Selected Tests". Tests will be run in the background and the display will be updated to show the results.

Tests that pass are shown with a green tick. The amount of time taken to run the test is also displayed.

Test Passed

Tests that fail are shown with a red cross.

Test Failed

The "Output" link can be clicked to display the text that was printed to the console during the test, including the standard unittest output.

Test Output

Test Framework Extensibility

NTVS can be extended to support additional test frameworks by implementing the discovery and execution logic using JavaScript.

In the following location: <VisualStudioFolder>\Common7\IDE\Extensions\Microsoft\Node.js Tools for Visual Studio\1.0\TestFrameworks

You'll see folders for ExportRunner and Mocha.

Under each folder, a JavaScript file named after the folder contains 2 exported functions:

  • find_tests
  • run_tests

See the implementation of ExportRunner and Mocha for examples of find_tests and run_tests implementations.

In your NTVS project, make sure to set the TestFramework property on your test file(s) to match the name of the subfolder under TestFrameworks.

If you implement support for other test frameworks and wish to contribute them, please let us know!



This page was generated using a tool. Changes will be lost when the html is regenerated. Source file: UnitTests.md

3eb73f16fe6ab95b6646d631df3a73d30dd9d04a5220ab1c739cd6ffc1218585

Last edited Dec 2, 2014 at 10:38 PM by huguesv, version 6

Comments

blairstark Jul 19, 2014 at 8:06 AM 
all though if you want to step through your test, you can do something like:


=======================================
'use strict';

var Mocha = require('mocha');
var options = {
ui: 'bdd',
timeout: 10000
};
var mocha = new Mocha(options);
mocha.files = ['./spec/someunittest.js'];
mocha.run();

======================

I'm still a total noob, so I am sure there is a better way to do this

blairstark Jul 19, 2014 at 4:48 AM 
Not sure when it was added, but mocha support exists.

use npm module manager to add mocha to your project as a development install.

then use Add New Item -> JavaScript Mocha Unit Test File. (the test framework type will be mocha)

The added unit test contains an example.

Build the project to initiate discovery ( if the tests aren't discovered, make sure you used the npm to add the mocha project )

Debug is still not supported :(

Any ETA ?