1

I am working through web development with node and express from O'reilly. Testing with Mocha has just been introduced and I am trying to run 2 tests. First is a global test to verify that there is a page title, second is a page specific test to check for a link to the contact page. Mocha loads when I test the page, but neither test runs, as I get "passes:0 fails:0".

I have this code in my main app file:

app.use(function(req,res,next){
    res.locals.showTests = app.get('env') !== 'production' && req.query.test === '1';
    next();
});

app.get('/', function(req, res){
    res.render('home');
});
app. get('/about', function(req,res){
    res.render('about', {fortune: fortune.getFortune(),
        pageTestScript: '/qa/tests-about.js'
        });

});

This is my tests-about.js

suite('"About" Page Tests', function(){
    test('page should contain link to contact page', function(){
        assert($('a[href="/contact"]').length);

    });
});

This is my tests-global.js

suite('Global Tests', function(){
    test('page has a valid title', function(){
        assert(document.title && document.title.match(/\S/) &&
            document.title.toUpperCase() !=== 'TODO');
    });
});

This is my main template file (handlebars)

<!doctype html>
<html>
<head>
    <title>Random Pixel Media</title>
    {{#if showTests}}
    <link rel="stylesheet" href="/vendor/mocha">
    {{/if}}
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
    <header><a href="/"><img src="/img/logo.png" alt="Random Pixel Logo" /></a></header>
    {{{body}}}
    {{#if showTests}}
    <div id="mocha"></div>
    <script src="/vendor/mocha.js"></script>
    <script src="/vendor/chai.js"></script>
    <script>
        mocha.ui('tdd');
        var assert = chai.assert;
    </script>
    <script src="/qa/tests-global.js"></script>
    {{#if pageTestScript}}
        <script src="{{pageTestScript}}"></script>
    {{/if}}
    <script>mocha.run();</script>
    {{/if}}
</body>
</html>

According to the book, I should be seeing 2 suites and 1 failure when I visit the about page, but as I stated, I see a blank bullet point, passes:0 and fails:0 Any help to understand what I'm missing would be greatly appreciated.

chris
  • 1,172
  • 12
  • 15
  • how do you run mocha + where are your tests ? If you simply run `mocha` it will run the tests matching the pattern `./test/*.js` – topheman Dec 17 '14 at 23:02
  • If `pageTestScript` is somehow not set, you'll get the results you are reporting. (@topheman Mocha can be run server-side or browser-side. The OP is running Mocha browser-side.) – Louis Dec 17 '14 at 23:29

2 Answers2

0

it's been been quite some time since you posted this. I am reading the book now, and I also had the same problem. I realized that it's because I opened with:

localhost:3000/?test=1 

instead of:

localhost:3000/about?test=1
0

The book example is correct. This could be an issues with the naming convention of your files or file structures. I had a similar issues but I had the wrong names on my files. For example 'Test' vs 'Tests'.

Marvin S
  • 1
  • 3