Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TypeScript Declaration File with mocha tests?

I have a TypeScript project, where the test are written in TypeScript. I want to use mocha to directly test TS files. I'm using ts-node for that, as described in ts-node#mocha. In the project I'm using a JS library which doesn't have TS type definitions. So I created a d.ts file for that. Everything works well when compiling and running the projects. However mocha fails with:

% yarn mocha --require ts-node/register --require source-map-support/register ./test/*.ts 

src/client.ts:3:26 - error TS7016: Could not find a declaration file for module 'algosdk'. '/home/robert/projects/algorand/ts-mocha/node_modules/algosdk/index.js' implicitly has an 'any' type.
  Try `npm install @types/algosdk` if it exists or add a new declaration (.d.ts) file containing `declare module 'algosdk';`

3 import * as algosdk from "algosdk";

It seams that ts-node doesn't recognize the d.ts files.

The only solution which works is to use require instead of import statement, but this will not link the types to algosdk.

How to use mocha with TS files and type definition file?

This is the project structure (also in github):

├── build    <-- JS from compiled TS go here
├── node_modules
├── package.json
├── src
├── @types  <-- d.ts files 
├── test
├── tsconfig.json

The tsconfig.json:

{
  "compilerOptions": {
    "target": "es2017",
    "lib": ["esnext"],
    "module": "commonjs",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "outDir": "./build",

    "typeRoots" : ["./@types", "node_modules/@types"]
  },
  "exclude": ["**/node_modules"],
  "include": [
    "src",
    "test",
    "@types"
  ]
}

UPDATE

I confirm that the problem is related to ts-node, not to mocha. Running:

yarn ts-node src/client.ts

returns the same error.

References:

  • Mocha + ts-node
  • TypeScript: Could not find a declaration file for module in unit tests, only
like image 697
Robert Zaremba Avatar asked Jul 08 '20 12:07

Robert Zaremba


People also ask

How do I specify a test file in mocha?

The nice way to do this is to add a "test" npm script in package. json that calls mocha with the right arguments. This way your package. json also describes your test structure.

Can mocha run TypeScript?

TS-Mocha has one only dependency - ts-node, which is used as a TypeScript runtime to execute tests that can import and run imported TypeScript source files as well. It is a thin wrapper that run node process with mocha and set up ts-node environment to handle .

What Is A TypeScript declaration file?

Declaration files, if you're not familiar, are just files that describe the shape of an existing JavaScript codebase to TypeScript. By using declaration files (also called . d. ts files), you can avoid misusing libraries and get things like completions in your editor.

Could not find a declaration file for module mocha?

The error "Could not find declaration file for module" occurs when TypeScript cannot find the type declaration for a module. To solve the error, install the types for the module by running the command from the error message, e.g. npm install -D @types/module-name .


Video Answer


1 Answers

you can add the following configuration to tsconfig.json file.

  "ts-node": {
    "files": true
  },

it works well on my side.

like image 54
QC-cheetah Avatar answered Sep 25 '22 10:09

QC-cheetah