Cannot find module in .spec.ts file in Ionic 3

坚强是说给别人听的谎言 提交于 2020-02-25 02:21:25

问题


I am working on Ionic 3 (Angular type) application, and to implement unit testing ,I am using Jasmine and Karma. Project structure of Ionic 3 (Angular type) doesnot provide .spec.ts file for pages/components/providers. So, I, manually, created .spec file for pages, but it is showing 'Cannot find module '@angular/core' and 'Cannot find module '@angular/core/testing' error.

As these file are not able to fetch modules or not able to get the path to reach to those modules.

I have tried following ways :

  • Removed node_modules and reinstalled it (npm install).
  • Ran command npm i @angular/core, with and without --save-dev.
  • Restarted sublime and terminal as well, after new installation.

But these didn't bring any solution to the issue.

So, is there any way to resolve this issue ? Or am I wrongly implementing Jasmine-Karma unit testing ? Please suggest.

karma.conf.js :

var webpackConfig = require('./webpack.test.js');
module.exports = function(config) {
    var _config = {
        basePath: '../',
        frameworks: ['jasmine'],

        files: [{pattern: 'src/pages/**/*.spec.ts', watched: true},
        { pattern: './src/pages/**/*',
        watched: false,
        included: false,
        served: true,
        nocache: false
    }],

    proxies: {'/assets/': '/base/src/pages/'},

    preprocessors: {'**/*.spec.ts' : ["webpack"]},

    webpack: webpackConfig,

    webpackMiddleware: {stats: 'errors-only'},

    webpackServer: {noInfo: true},

    browserConsoleLogOptions: {
        level: 'log',
        format: '%b %T: %m',
        terminal: true
    },

    coverageIstanbulReporter: {
        reports: [ 'html', 'lcovonly' ],
        fixWebpackSourcePaths: true
    },

    reporters: config.coverage ? ['kjhtml', 'dots', 'coverage-istanbul'] : ['kjhtml', 'dots'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
};

config.set(_config);
};

回答1:


In Ionic 3 project structure's tsconfig.json, "exclude" was including "src/**/*.spec.ts"; as follows :

"exclude": [
     "node_modules",
     "src/**/*.spec.ts"]

So, I moved this line "src/**/*.spec.ts" from "exclude" to "include" with same tsconfig.json file. Now, the error of "Cannot find node modules" isn't showing again. And this worked for me.

"include": [
     "src/**/*.ts",
     "src/**/*.spec.ts"]


来源:https://stackoverflow.com/questions/60150417/cannot-find-module-in-spec-ts-file-in-ionic-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!