javascript 运行测试时出错:TS6196已声明但从未使用

hrirmatl  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(399)

尝试按照https://medium.com/swlh/how-to-setting-up-unit-tests-with-typescript-871c0f4f1609指南编写一个简单的测试,但在运行测试时得到TS6196: 'HelloWorldServiceUnitTests' is declared but never used.错误。
./register.js

const tsNode = require('ts-node');
const testTSConfig = require('./test/tsconfig.json');

tsNode.register({
  files: true,
  transpileOnly: true,
  project: './test/tsconfig.json'
});

./mocharc.json

{
  "require": "./register.js",
  "reporter": "dot"
}

./nycrc.json

{
  "extends": "@istanbuljs/nyc-config-typescript",
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules/"
  ],
  "extension": [
    ".ts"
  ],
  "reporter": [
    "text-summary",
    "html"
  ],
  "report-dir": "./coverage"
}

./test/您好-世界-服务.单元.test.ts

import {suite, test} from '@testdeck/mocha';
import * as _chai from 'chai';

_chai.should();

@suite
class HelloWorldServiceUnitTests {
    // eslint-disable-next-line require-jsdoc
    @test 'should do something when call a method'() {
        const list = ['a', 'b', 'c'];
        console.log(list);
    }
}

运行测试时,在./test/tsconfig.json中设置noUnusedLocals: false仍会引发错误:

TSError: ⨯ Unable to compile TypeScript:
test/hello-world-service.unit.test.ts(7,7): error TS6196: 'HelloWorldServiceUnitTests' is declared but never used.
iecba09b

iecba09b1#

尝试在tsconfig.json中关闭noUnusedLocals选项
here的问题相同

相关问题