[英]Getting Experimental support for decorator error when using tsyringe @injectable
I'm using tsyringe for dependency injection and trying to run unit tests.我正在使用 tsyringe 进行依赖注入并尝试运行单元测试。 The class is in ts and the test file is in js.
class在ts中,测试文件在js中。 When I try to run my tests by executing
TS_NODE_PROJECT=\"tsconfig.testing.json\" mocha -r ts-node/register src/**/*.test.js
I get the following compilation error:当我尝试通过执行
TS_NODE_PROJECT=\"tsconfig.testing.json\" mocha -r ts-node/register src/**/*.test.js
来运行我的测试时,我收到以下编译错误:
repo.ts:27:14 - error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option in your 'tsconfig' or 'jsconfig' to remove this warning.
Here is my code.这是我的代码。
// repo.ts
@injectable()
export class Repo {
testAdd = (a, b) => {
return a + b;
};
}
// repo.test.js
const { Repo } = require("../repo");
const expect = require("chai").expect;
describe("testing the add function", () => {
it("addition worked correctly", (done) => {
const r = new Repo();
const res = r.testAdd(4, 5);
expect(res).to.equal(9);
done();
});
});
// tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "commonjs",
"noImplicitReturns": false,
"noUnusedLocals": false,
"outDir": "lib",
"sourceMap": true,
"strict": false,
"target": "es2017"
},
"compileOnSave": true,
"include": ["src"]
}
// tsconfig.testing.json
{
"compilerOptions": {
"module": "commonjs",
"target": "es6"
},
"include": ["**/*.spec.ts"]
}
If I get rid of the injectable()
decorator then the tests work.如果我摆脱了
injectable()
装饰器,那么测试就可以工作了。 If I change the test ext from js to ts, then it works.如果我将测试 ext 从 js 更改为 ts,那么它可以工作。 I tried creating a jsconfig.json and adding in
我尝试创建一个 jsconfig.json 并添加
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
but it didn't help.但这没有帮助。
What am I doing wrong?我究竟做错了什么?
Update, I think the issue is that I needed to add the更新,我认为问题是我需要添加
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
in the tsconfig.testing.json file as well.在 tsconfig.testing.json 文件中也是如此。 So far looks to be working with the.js testing files.
到目前为止看起来正在使用 .js 测试文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.