TypeScript:在单元测试中找不到模块的声明文件,仅

TypeScript: Could not find a declaration file for module in unit tests, only

我在 Windows 10 上使用带有 Visual Studio 代码的 TypeScript 来开发 NPM 模块。我使用 mocha/chai 结合 nyc (istanbul) 进行单元测试和代码覆盖。

对于我的一些测试,我想使用 chai-bytes 来更轻松地比较缓冲区。不幸的是,chai-bytes 模块中没有类型定义文件,@types/chai-bytes 处也没有可用的定义。

因此,我为 chai-bytes 插件编写了自己的类型定义文件(非常简单),但是在执行 npm test 期间出现以下错误:

TSError: ⨯ Unable to compile TypeScript:
test/utls/BitArray-test.ts(3,23): error TS7016: Could not find a declaration file for module 'chai-bytes'. 'C:/Users/<user>/Source/Repos/velux-api/node_modules/chai-bytes/index.js' implicitly has an 'any' type.
  Try `npm install @types/chai-bytes` if it exists or add a new declaration (.d.ts) file containing `declare module 'chai-bytes';`
test/utls/BitArray-test.ts(48,38): error TS2339: Property 'equalBytes' does not exist on type 'Assertion'.

VS Code 为我提供了完整的 Intellisense,因此我认为我的类型定义文件可以工作并且至少可以被 VS Code 找到。

这是我的目录结构:

dist\              <-- My compiled code goes here
  utils\
    BitArray.d.ts
    BitArray.js
    BitArray.js.map
  index.d.ts
  index.js
  index.js.map
  ...
src\
  utils\
    BitArray.ts
  index.ts
  ...
test\
  utils\
    BitArray-test.ts
  ... (other test files)
  mocha.opts
types\
  chai-bytes\
    index.d.ts       <-- Type definition file for 'chai-bytes'

我曾尝试将类型定义文件移动到源代码树(几个地方),但没有任何效果,此外,有时它变得更糟,以至于甚至 VS Code 都找不到它了。

这些是我的配置文件:

package.json:

{
      "name": "klf-200-api",
      "version": "3.0.0",
      "description": "This module provides a wrapper to the socket API of a Velux KLF-200 interface. You will need at least firmware 0.2.0.0.71 on your KLF interface for this library to work.",
      "main": "dist/index.js",
      "types": "dist/index.d.ts",
      "author": {
        "name": "Michael Schroeder"
      },
      "dependencies": {
        "@types/promise-timeout": "^1.3.0",
        "promise-timeout": "^1.3.0"
      },
      "devDependencies": {
        "@types/chai": "^4.1.6",
        "@types/chai-as-promised": "^7.1.0",
        "@types/mitm": "^1.3.2",
        "@types/mocha": "^5.2.5",
        "@types/node": "^10.11.7",
        "@types/sinon": "^5.0.7",
        "@types/sleep": "0.0.7",
        "babel-eslint": "^8.0.0",
        "chai": "^4.1.0",
        "chai-as-promised": "^7.1.1",
        "chai-bytes": "^0.1.1",
        "chai-sinon": "^2.8.1",
        "cross-env": "^5.2.0",
        "eslint": "^4.7.1",
        "eslint-config-defaults": "^9.0.0",
        "eslint-plugin-react": "^7.3.0",
        "gulp": "^4.0.0",
        "gulp-release-it": "^2.0.14",
        "gulp-typescript": "^5.0.0-alpha.3",
        "gulp-uglify": "^3.0.1",
        "istanbul": "^0.4.5",
        "mitm": "^1.4.0",
        "mocha": "^3.4.2",
        "nock": "^9.0.14",
        "nyc": "^13.1.0",
        "sinon": "^7.1.1",
        "sleep": "^5.2.3",
        "source-map-support": "^0.5.9",
        "ts-mocha": "^2.0.0",
        "ts-node": "^7.0.1",
        "typescript": "^3.1.2",
        "uglify-es": "^3.3.9"
      },
      "scripts": {
        "test": "cross-env TS_NODE_FILES=true nyc mocha",
        "document": "jsdoc src -r -c ./.jsdoc.json -d docs"
      },
      "nyc": {
        "include": [
          "src/**/*.ts",
          "src/**/*.tsx"
        ],
        "extension": [
          ".ts",
          ".tsx"
        ],
        "exclude": [
          "**/*.d.ts"
        ],
        "reporter": [
          "text-summary",
          "html"
        ],
        "all": true
      },
      "repository": {
        "type": "git",
        "url": "https://github.com/MiSchroe/klf-200-api"
      },
      "keywords": [
        "klf-200",
        "IoT"
      ],
      "license": "MIT",
      "bugs": {
        "url": "https://github.com/MiSchroe/klf-200-api/issues"
      },
      "homepage": "https://mischroe.github.io/klf-200-api/"
    }

tsconfig.json:

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es6",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    "sourceMap": true,                     /* Generates corresponding '.map' file. */
    "outDir": "./dist",                        /* Redirect output structure to the directory. */

    /* Strict Type-Checking Options */
    "strict": true,                           /* Enable all strict type-checking options. */
    "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */

    /* Module Resolution Options */
    "esModuleInterop": true                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
  },
  "include": [
    "./src/**/*"
  ]
}

mocha.opts:

--require ts-node/register
--require source-map-support/register
--recursive
--full-trace
--bail
test/**/*.ts

types\chai-bytes\index.d.ts:

/// <reference types="chai" />

declare module "chai-bytes" {
    function chaiBytes(chai: any, utils: any): void;

    export = chaiBytes;
}

declare namespace Chai {

    // For BDD API
    interface Assertion extends LanguageChains, NumericComparison, TypeComparison {
        equalBytes(expected: string | Array<number> | ArrayLike<number>): void;
    }

}

BitArray-test.ts(仅相关测试):

import { expect } from "chai";
import { bitArrayToArray, arrayToBitArray } from "../../src/utils/BitArray";
import chaibytes from "chai-bytes";

'use strict';

chai.use(chaibytes);

describe("...", function() {
            it("should return an the correctly filled buffer", function() {
                const nums: number[] = [0, 2, 4, 6, 8, 10, 12, 14];
                const result = arrayToBitArray(nums, 2);

                expect(result).to.be.an.instanceof(Buffer);
                expect(result).to.be.equalBytes([0x55, 0x55]);
            });
});

npm --version: 3.10.10

node --version: v6.11.1

我可以使用 Buffer.compare 作为变通方法,但是我不会在错误消息中看到缓冲区的内容,而只会看到 -101。 (而且它不会解决问题。)

目前,我陷入了困境,非常感谢任何帮助。

tsconfig.json 添加:

"typeRoots": [
  "./node_modules/@types",
  "./types"
]                       /* List of folders to include type definitions from. */

进入compilerOptions列表。

更改 BitArray-test.ts 文件的 header:

import { bitArrayToArray, arrayToBitArray } from "../../src/utils/BitArray";
import chaibytes from "chai-bytes";
import { expect, use } from "chai";

'use strict';

use(chaibytes);