我有一个名为Polynomial的typescript类,我想用它创建一个npm模块。为此,我试着用mocha测试它,用这个脚本:
//Polynomial.test.js
const Polynomial = require('./Polynomial.ts');
const assert = require('assert').strict;
describe("First test", function() {
it("test", function() {
let p = new Polynomial();
assert(p.equals(new Polynomial("0")));
});
});
当我执行npm test
时,我在类的第一行得到错误Unexpected identifier
,看起来像这样:
// Polynomial.ts
class Polynomial {
private coefMap: Map<string, string> = new Map(); // <--- Error
// ...
}
module.exports = Polynomial;
这就是package.json
:
{
"name": "multivariate-polynomial",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "mocha Polynomial.test.js"
}
}
而tsconfig.json
:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true
},
}
有什么问题吗?
1条答案
按热度按时间8iwquhpp1#
更改
lib
和target
并没有解决这个问题,但是我按照下面描述的步骤创建了一个新项目:Setup a new TypeScript project with Mocha support
现在它起作用了。