9

I'm creating typescript base npm module, but i stuck with jest tests. I'm trying to include jsonld module from https://github.com/types/jsonld and everything seems work fine, but when i run command yarn test i have error message 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; });
    }
}

I have prediction that jsonld module is not in root directory but in js/ directory. But when i changed import to jsonld/js issues still the same.

user6039980
  • 3,108
  • 8
  • 31
  • 57
Vytautas Saulis
  • 141
  • 2
  • 6

3 Answers3

9

If someone has a problem with a package not resolved by 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)

Then you should check the package.json main field inside the installed package folder (with that example, it's ./node_modules/@private-registry/private-package/package.json. If there's no main field or the main field points to a wrong (nonexistent) file, then that's the problem. It was a problem in my case at least.


If that doesn't help, you can also try to walk with a console.log through node_modules/jest-resolve/build/index.js and node_modules/jest-resolve/build/defaultResolver.js like I did.

Crysknight
  • 291
  • 2
  • 8
  • How did you actually fix the error though? – twiz Feb 27 '23 at 12:03
  • @twiz try checking my answer https://stackoverflow.com/a/75595580/4288447 – Laura Mar 03 '23 at 11:52
  • @Laura That certainly would work as a temporary fix, but you would need the original package changed or create your own fork if you want it to persist in your codebase. In my case, I am installing a package directly from Github which means it is missing the files that are built when published on NPM, so I don't think there is a reasonable way to fix the original package without the maintainers committing the built files to git. – twiz Mar 03 '23 at 15:01
  • Yes, sure. That's exactly what I had to do, I opened a PR for the lib maintainers. – Laura Mar 04 '23 at 23:20
1

Following @Crysknight answer, I came up with a solution for my project:

Only adding main entry in package.json didn't worked. So I added a console.log to show me the error message in node_modules/jest-resolve/build/resolver.js. The message was Error: No known conditions for "." entry in "foo" package.

Opening the library package.json I see:

...
"exports": {
  ".": {
    "import": "./dist/foo.core.mjs",
    "types": "./dist/foo.d.ts"
  },
  "./*": "./*"
},
...

The problem was, the entry require was missing, so I added the following:

...
"exports": {
  ".": {
    "require": "./dist/foo.esm.mjs",
    "import": "./dist/foo.core.mjs",
    "types": "./dist/foo.d.ts"
  },
  "./*": "./*"
},
...

And now it's working :D hope this answer can help someone.

Laura
  • 265
  • 2
  • 11
0

I had a similar problem, the error was:

Cannot find module '@reach/router' from 'node_modules/my_custom_module/dist/blah.js' 

At Resolver.resolveModule (node_modules/jest-runtime/node_modules/jest-resolve/build/index.js:306:11)
at Object.<anonymous> (node_modules/my_custom_module/dist/blah.js:1:392)

Needed to go to my_custom_module/package.json file and spot @reach/router there. @reach/router was listed as a peer dependency, i.e. it was inside peerDependecies block (not dependencies or devDependencies), and peer dependencies are not installed automatically with npm install (read here). To fix it, needed to include @reach/router as a dependency into the package.json file within my project (not my_custom_module).

magic_turtle
  • 1,243
  • 3
  • 17
  • 37