Reputation:
I'm trying to get into unit testing, but when I try to test my code I'm getting this in the terminal:
D:\VS Code\VS Code\tests\test\test.js:1
(function (exports, require, module, __filename, __dirname) { ��'
^
SyntaxError: Invalid or unexpected token
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at C:\Users\SSD\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:230:27
at Array.forEach (native)
at Mocha.loadFiles (C:\Users\SSD\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:227:14)
at Mocha.run (C:\Users\SSD\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:495:10)
at Object.<anonymous> (C:\Users\SSD\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:469:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:504:3
I followed the steps given in the mocha documentation here: https://mochajs.org/#installation
Then I literally copied and pasted the example
var assert = require('assert');
describe('Array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
Set up the test in package.json:
"scripts": {
"test": "mocha"
}
Then ran the command npm test
No matter what test example I take the error is the same. I've also tried to uninstall and install mocha again, I've tried chai also. Same error.
Upvotes: 1
Views: 2195
Reputation: 11
I have just delete de file math.js, copy his content and created new file if same name and paste the same program again, and it works.
example:
const assert = require('assert');
const Math = require('../scr/math.js');
describe('Math class', function() {
it('Sum two numbers', function() {
const math = new Math();
assert.equal(math.sum(5, 5), 10);
});
});
file math.js:
class Math {
sum = function sum(a,b) { };
}
module.exports = Math;
Upvotes: 1
Reputation: 1
I had the same problem. Actually I tried creating the test js file using the windows cmd instead of touch cmd from git bash, then I pasted the code as it was mentioned in this tutorial https://buddy.works/guides/how-automate-nodejs-unit-tests-with-mocha-chai.
Just delete the test.js file and create using touch cmd. It should work fine after that.
Upvotes: 0