1

Trying to write a simple test following this guide https://medium.com/swlh/how-to-setting-up-unit-tests-with-typescript-871c0f4f1609 but I get TS6196: 'HelloWorldServiceUnitTests' is declared but never used. error when I run the test.

./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/hello-world-service.unit.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);
    }
}

enter image description here

Setting noUnusedLocals: false in ./test/tsconfig.json still throws an error when running the test:

TSError: ⨯ Unable to compile TypeScript:
test/hello-world-service.unit.test.ts(7,7): error TS6196: 'HelloWorldServiceUnitTests' is declared but never used.
vovahost
  • 34,185
  • 17
  • 113
  • 116

1 Answers1

1

Try setting your noUnusedLocals option off in your tsconfig.json

Same issue as here

Daxelarne
  • 172
  • 11
  • Is it possible to configure `noUnusedLocals` rule so it only applies to Classes which have `@suite` decorator? – vovahost Jan 27 '23 at 09:08
  • you can configure the `compileroptions` with `include` and `exclude` by selecting the file, but cant include by anotations `{ "compilerOptions": { "noUnusedLocals": true, }, "include": ["*"], "exclude": ["myFolder/myFile"] } ` – Daxelarne Jan 27 '23 at 09:15
  • Setting `noUnusedLocals: false` in `./test/tsconfig.json` turns off the IDE warning but the test still fails when I run it. See updated question. – vovahost Jan 27 '23 at 09:18
  • From the tutorial, did you copy the `tsconfig.json` config ? and your `HelloWorldServiceUnitTests` is not the same. Try copying the same content than the tutorial. – Daxelarne Jan 27 '23 at 09:23
  • Yes. Is there anyway to configure that `./test/tsconfig.json` when running the test? Looks to me that it's how the test is launched. – vovahost Jan 27 '23 at 10:00
  • The tests files location is defined in the `package.json`. The `tsconfig.json` is used to specifies the root files and the compiler options required to compile the project – Daxelarne Jan 27 '23 at 10:04