我尝试使用Phaser 3,Mocha和NodeJS测试我的javascript应用程序。 如果我运行测试命令“ npm test”,则会出现以下错误:
我该如何解决?
这是我每次都收到的错误消息:
(function (exports, require, module, __filename, __dirname) { import phaser from 'phaser';
^^^^^^
SyntaxError: Unexpected identifier
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:656:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (C:\Users\Chris\Documents\GitHub\gamejam-challenge-nov18\test\appTest.js:2:23)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
at require (internal/modules/cjs/helpers.js:20:18)
at C:\Users\Chris\Documents\GitHub\gamejam-challenge-nov18\node_modules\mocha\lib\mocha.js:250:27
at Array.forEach (<anonymous>)
at Mocha.loadFiles (C:\Users\Chris\Documents\GitHub\gamejam-challenge-nov18\node_modules\mocha\lib\mocha.js:247:14)
at Mocha.run (C:\Users\Chris\Documents\GitHub\gamejam-challenge-nov18\node_modules\mocha\lib\mocha.js:576:10)
at Object.<anonymous> (C:\Users\Chris\Documents\GitHub\gamejam-challenge-nov18\node_modules\mocha\bin\_mocha:637:18)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
npm ERR! Test failed. See above for more details.
这是我(使用Mocha)编写的测试:
// appTest.js
var assert = require('chai').assert;
const isRowFreeFunc = require('../app').isRowFree;
describe('function', function(){
it('should be free', function(){
gameField = [
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1]
];
var result = isRowFreeFunc(6);
assert.equal(result, true);
})
});
这是我的nodejs配置:
// package.json
{
"name": "testing101",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "parcel index.html",
"test": "mocha"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"babel-preset-env": "^1.6.1",
"jest": "^22.4.4",
"parcel-bundler": "^1.7.0",
"phaser": "^3.15.1"
},
"jest": {
"verbose": true,
"testURL": "http://localhost:1234/"
},
"dependencies": {
"phaser": "^3.1.0",
"mocha": "^5.2.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kippenvogel/gamejam-challenge-nov18.git"
},
"bugs": {
"url": "https://github.com/kippenvogel/gamejam-challenge-nov18/issues"
},
"homepage": "https://github.com/kippenvogel/gamejam-challenge-nov18#readme"
}
这是我申请的一部分(精简版):
// app.js
import phaser from 'phaser';
var config = {
type: Phaser.AUTO,
width: 900,
height: 800,
scene: {
preload: preload,
create: create,
update: update
}
};
// IS ROW FREE OR FULL
/***
*
* @returns {boolean}
* true = free
* false = full
*/
module.exports = {
isRowFree: function(row) {
// ROW IS FULL
if (gameField[0][row] == 1 ||
gameField[0][row] == 2){
console.log(gameField);
console.log("ROW (" + row + ") is full!");
return false;
}
// ROW IS FREE
else {
var hh = getHeigthOfColumn(row);
console.log ("Free Stones: " + hh);
return true;
}
}
}
function getHeigthOfColumn(column){
var stones = 5;
for (stones; stones >= 0; stones = stones-1){
// FREE FOR MIN. 1 STONE
if (gameField[stones][column] == 0){
break;
}
if (gameField[stones][column] == 1 || gameField[stones][column] == 2 ){
}
}
console.log( "Free Stones in Column(" + column+"): " + stones);
return stones;
}