monounity / karma-typescript

Simplifying running unit tests with coverage for Typescript projects.
314 stars 109 forks source link

An error occurs when importing a declaration file. #315

Open daybrush opened 5 years ago

daybrush commented 5 years ago

An error occurs when importing a declaration file.

error 1

import { ObjectInterface } from "@daybrush/utils/declaration/consts";
16 01 2019 20:12:10.435:ERROR [karma]: Error: Unable to resolve module [@daybrush/utils/declaration/consts] from [/Users/user/Desktop/scenejs/src/Scene.js]

error 2

import { ObjectInterface } from "@daybrush/utils/declaration/consts.d";
16 01 2019 20:20:17.846:ERROR [source-reader.karma-typescript]: Error parsing code: Unexpected token (1:7)
in /Users/user/Desktop/scenejs/node_modules/@daybrush/utils/declaration/consts.d.ts
at line 1, column 7:

... interface ObjectInterface<T> {
    [name: string]: ...
var onFilenameResolved = function () {
            _this.lookupNameCache[bundleItem.lookupName] = bundleItem.filename;
            if (_this.isInFilenameCache(bundleItem) || bundleItem.isTypingsFile() || bundleItem.isTypescriptFile()) {
                process.nextTick(function () {
                    onModuleResolved(bundleItem);
                });
            }
erikbarke commented 5 years ago

What version of karma-typescript are you running?

daybrush commented 5 years ago

The currently applied karma-typescript version is 3.0.13.

daybrush commented 5 years ago

The versions of modules related to karma are as follows.

"karma": "^1.7.1",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
"karma-typescript": "^3.0.13",
daybrush commented 5 years ago

Error2, Added bundleItem.isTypingFile() in the dist/...api/bundler/resolve/resolver.js file.

ssivov commented 5 years ago

I'm ran into the similar issue. This happens because @daybrush/utils/declaration/consts.d is interpreted as a npmModule. In resolver.ts, there is the following snippet, where moduleName is your file path

if (bundleItem.isTypescriptFile() && !bundleItem.isNpmModule()) {
    process.nextTick(() => {
         onModuleResolved(bundleItem);
     });
     return;
}
public isNpmModule(): boolean {
    return this.moduleName.charAt(0) !== "." && this.moduleName.charAt(0) !== "/";
}

Since file path doesn't start with "." or "/" it is considered an npm module and can't be resolved and throws the error down the line.

@erikbarke , can the isNpmModule condition be improved to handle tsconfig paths correctly? I could make a PR to check if filename is present, whether filename contains "node_modules", but it's prone to false positives if some other folder on the path or filename itself contain 'node_modules' substring.

Workaround with the existing version is to set

karmaTypescriptConfig {
    bundlerOptions: {
        exclude: [
            "@daybrush/utils/declaration/consts.d"
        ]
    }
}
erikbarke commented 5 years ago

@ssivov, PR:s are always welcome!

Contributing guide lines

thw0rted commented 3 years ago

@erikbarke Is this maybe a duplicate of #261?