问题描述
我正在将TypeScript与Windows 10上的Visual Studio Code一起使用来开发NPM模块.我将mocha/chai与nyc(istanbul)结合使用进行单元测试和代码覆盖.
I'm using TypeScript with Visual Studio Code on Windows 10 to develop an NPM module. I use mocha/chai combined with nyc (istanbul) for unit testing and code coverage.
对于我的某些测试,我想使用chai-bytes来更轻松地比较缓冲区.不幸的是,在chai-bytes模块中没有类型定义文件,在@ types/chai-bytes处也没有定义.
For some of my tests I would like to use chai-bytes to compare buffers more easily. Unfortunately, there is no type definition file in the chai-bytes module and there is no definition at @types/chai-bytes available.
因此,我为chai-bytes插件编写了自己的类型定义文件(这很简单),但是在执行npm test
时,出现以下错误:
Therefore, I have written my own type definition file for the chai-bytes plugin (which is very simple), but during execution of npm test
I get the following error:
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找到的.
VS Code provides me with full Intellisense, so I think my type definition file works and is found at least by 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都找不到了.
I have tried to move the type definition file to the source tree (several places), but with no effect, besides, that sometimes it got even worse, so that even VS Code haven't found it anymore.
这些是我的配置文件:
package.json:
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:
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:
mocha.opts:
--require ts-node/register
--require source-map-support/register
--recursive
--full-trace
--bail
test/**/*.ts
types \ chai-bytes \ index.d.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(仅相关测试):
BitArray-test.ts (only the relevant test):
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
. (而且它不能解决问题.)
I could use Buffer.compare
as work-around instead, but then I wouldn't see the buffers' content in the error message but only a -1
, 0
or 1
. (And it wouldn't solve the problem.)
目前,我被困在这一点上,任何帮助都将不胜感激.
Currently, I'm stuck at that point and any help is much appreciated.
推荐答案
好的,我可以使用它.
这就是我所做的:
tsconfig.json:
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);
现在,它可以工作了.
这篇关于TypeScript:仅在单元测试中找不到模块的声明文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!