我正在创建打字稿基础的npm模块,但是我坚持使用开玩笑的测试。我正在尝试从https://github.com/types/jsonld包括jsonld模块,并且一切似乎都正常,但是当我运行命令yarn test
时出现错误消息Cannot find module 'jsonld' from 'DidDocument.ts'
package.json
{
"name": "defined-id",
"version": "1.0.0",
"description": "Defined ID",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "jest --config jestconfig.json",
"build": "tsc",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "tslint -p tsconfig.json",
"prepare": "yarn run build",
"prepublishOnly": "yarn test && yarn run lint",
"preversion": "yarn run lint",
"version": "yarn run format && git add -A src",
"postversion": "git push && git push --tags"
},
"repository": {
"type": "git",
"url": "git+https://github.com/VSaulis/defined-id.git"
},
"keywords": [
"DID",
"DID document"
],
"author": "Vytautas Saulis",
"license": "ISC",
"bugs": {
"url": "https://github.com/VSaulis/defined-id/issues"
},
"homepage": "https://github.com/VSaulis/defined-id#readme",
"devDependencies": {
"@types/jest": "^23.3.9",
"jest": "^23.6.0",
"prettier": "^1.15.2",
"ts-jest": "^23.10.5",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.16.0",
"typescript": "^3.1.6"
},
"dependencies": {
"@types/jsonld": "github:types/jsonld",
"@types/uuid": "^3.4.4"
}
}
jestconfig.json
{
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
"moduleDirectories": ["node_modules", "src"]
}
DidDocument.ts
import * as jsonld from 'jsonld';
import { Did } from './Did';
import { PublicKey } from './PublicKey';
export class DidDocument {
private context: string[] = ["https://w3id.org/did/v1", "https://w3id.org/security/v1"];
private did: Did;
private publicKeys: PublicKey[];
constructor(did: Did, publicKeys: PublicKey[]) {
this.did = did;
this.publicKeys = publicKeys;
}
public format(): void {
const doc = {
"http://schema.org/id": this.did.format(),
"publicKey": this.publicKeys.map((publicKey: PublicKey) => JSON.stringify(publicKey.format()))
};
return jsonld.compact(doc, JSON.stringify(this.context), () => { return; });
}
}
我预测jsonld模块不在根目录中,而是在js /目录中。但是当我将导入更改为jsonld/js
时,问题仍然相同。
答案 0 :(得分:0)
如果有人遇到了一个没有通过 jest 解决的包问题,e. g.
Cannot find module '@private-registry/private-package' from 'user_block.vue'
13 | >
14 | <template v-slot:activator="{ on }">
> 15 | <v-avatar
| ^
16 | :color="white ? '#f2f2f2' : color"
17 | :class="[
18 | 'mp-user-avatar',
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at src/components/lib/components/user_block.vue:15:20
at Object.<anonymous> (src/components/lib/components/index.ts:75:42)
然后您应该检查已安装包文件夹中的 package.json main
字段(在该示例中,它是 ./node_modules/@private-registry/private-package/package.json
。如果没有主字段或主字段指向错误(不存在)文件,那么这就是问题所在。至少在我的情况下这是一个问题。
如果这没有帮助,您也可以像我一样尝试用 console.log
穿过 node_modules/jest-resolve/build/index.js
和 node_modules/jest-resolve/build/defaultResolver.js
。