TypeScript: не удалось найти файл объявления для модуля в модульных тестах, только - PullRequest
0 голосов
/ 27 ноября 2018

Я использую TypeScript с кодом Visual Studio для Windows 10 для разработки модуля NPM.Я использую mocha / chai в сочетании с nyc (istanbul) для модульного тестирования и покрытия кода.

В некоторых моих тестах я хотел бы использовать байты байтов для более простого сравнения буферов.К сожалению, в модуле 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 Код предоставляет мне полную 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, но тогда я бы не увидел содержимое буферов в сообщении об ошибке, а только -1, 0 или 1.(И это не решило бы проблему.)

В настоящее время я застрял в этой точке, и любая помощь очень ценится.

1 Ответ

0 голосов
/ 30 ноября 2018

Хорошо, у меня это работает.

Это то, что я сделал:

tsconfig.json:

Добавить

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

в список compilerOptions.

Изменение заголовка файла BitArray-test.ts:

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

'use strict';

use(chaibytes);

Теперь все работает.

...