简体   繁体   English

使用 tsyringe @injectable 时获得对装饰器错误的实验性支持

[英]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.

相关问题 使用 tsyringe 将依赖项注入控制器时出错 - Error injecting dependency into controller with tsyringe 使用 tsyringe 的 Typescript 动态依赖注入 - Dynamic Dependency injection with Typescript using tsyringe 使用 --experimental-modules 后出现“ESM 模块加载器是实验性的”的 NodeJs 错误 - NodeJs error of 'The ESM module loader is experimental' after using --experimental-modules 使用experimental-json-modules时,ESLint不适用于json导入 - ESLint not working for json import when using experimental-json-modules 使用babel装饰器时出现意外令牌@ - Unexpected Token @ when using babel decorator 使用 TS 装饰器时未定义 PropertyDescriptor - PropertyDescriptor undefined when using TS decorator 在 npm package 中添加对实验语法“classProperties”的支持 - Add support for experimental syntax 'classProperties' in an npm package 导入 json 值时缺少 @Injectable 注释 - Missing @Injectable annotation when importing json value 在 nodejs 后端使用 msal 时出错 - Getting an error when using msal in a nodejs backend 使用“--experimental-modules”编译时如何修复“ReferenceError: require is not defined”错误? - How to fix "ReferenceError: require is not defined" error when compiling with "--experimental-modules"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM