Unexpected token ILLEGAL while running node.js moc

2019-07-21 04:23发布

问题:

I have searched all over but have not figured out what I am doing wrong. I am trying to set up mocha for testing node.js javascript application files. I have node installed and have successfully ran basic things on it to confirm it is working.

I installed mocha in my project file, and also have a Makefile and a file called "test" within my project file as well.

Here is the error my terminal(osx 10) is spitting out when I run the command "make test".

    humbleMousesMBP:chapter02 humbleMouse$ make test

    /Users/humbleMouse/chapter02/test/exchange.test.js:22

        ^
        SyntaxError: Unexpected token ILLEGAL
        at Module._compile (module.js:439:25)
        at Object.Module._extensions..js (module.js:474:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:312:12)
        at Module.require (module.js:364:17)
        at require (module.js:380:17)
        at /Users/humbleMouse/chapter02/node_modules/mocha/bin/_mocha:313:27
        at Array.forEach (native)
        at load (/Users/humbleMouse/chapter02/node_modules/mocha/bin/_mocha:310:9)
        at Object.<anonymous> (/Users/humbleMouse/chapter02/node_modules/mocha           /bin/_mocha:301:1)
        at Module._compile (module.js:456:26)
        at Object.Module._extensions..js (module.js:474:10)
        at Module.load (module.js:356:32)
        at Function.Module._load (module.js:312:12)
        at Function.Module.runMain (module.js:497:10)
        at startup (node.js:119:16)
        at node.js:906:3
    make: *** [test] Error 8

This is the test I am trying to run:

'use strict';

var assert = require('assert')
  , should = require('should');

var exchangeData = {};

suite('exchange', function() {



test('buy should add a BUY nockmarket order', function(done) {
    exchangeData = exchange.buy(40, 100, exchangeData);
    exchangeData.buys.volumes[40].should.eql(100);
    done();
});


test('sell should add a SELL nockmarket order', function(done) {
  exchangeData = exchange.sell(41, 200, exchangeData);
  exchangeData.sells.volumes['41'].should.eql(200); //this is line 22
  done();
});


test('sell should produce trades', function(done) {
  exchangeData = exchange.sell(40, 75, exchangeData);
  exchangeData.trades[0].price.should.eql(40);
  exchangeData.trades[0].volume.should.eql(75);
  exchangeData.buys.volumes[40].should.eql(25);
  exchangeData.sells.volumes[41].should.eql(200);
  done();
});





});

回答1:

There are some invalid characters in your code, if you used proper text editor, you'd see them. The line numbering is a bit off, but this is clearly the cause.

Here's a screenshot from Sublime Text:

It's \uFFFC, more info here.

Just delete them (they can't be seen, so delete all from the semicolon to the next test().