在 Ionic 3 的 .spec.ts 文件中找不到模块 [英] Cannot find module in .spec.ts file in Ionic 3

查看:25
本文介绍了在 Ionic 3 的 .spec.ts 文件中找不到模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Ionic 3(Angular 类型)应用程序,为了实现 unit testing,我正在使用 JasmineKarma.Ionic 3(Angular 类型)的项目结构不提供页面/组件/提供者的 .spec.ts 文件.因此,我手动为页面创建了 .spec 文件,但它显示找不到模块 '@angular/core' 和 '找不到模块 '@angular/core/testing' 错误.

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.

我尝试了以下方法:

  • 删除 node_modules 并重新安装(npm install).
  • 运行命令 npm i @angular/core,带和不带 --save-dev.
  • 在新安装后也重新启动了 sublime 和终端.

但这些并没有为问题带来任何解决方案.

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

那么,有什么办法可以解决这个问题吗?还是我错误地实施了 Jasmine-Karma 单元测试?请提出建议.

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

karma.conf.js:

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);
};

推荐答案

在 Ionic 3 项目结构的 tsconfig.json 中,"exclude" 包含 "src/**/*.spec.ts";如下:

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

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

因此,我将src/**/*.spec.ts"这一行从exclude"移至include",并使用相同的 tsconfig.json 文件.现在,找不到节点模块"的错误不再显示.这对我有用.

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"]

这篇关于在 Ionic 3 的 .spec.ts 文件中找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆