在Ionic 3的.spec.ts文件中找不到模块 [英] Cannot find module in .spec.ts file in Ionic 3
问题描述
我正在研究Ionic 3(角度类型)应用程序,并且要实现unit testing
,我正在使用 Jasmine 和 Karma . Ionic 3(角型)的项目结构不为页面/组件/提供者提供.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"]
因此,我使用相同的tsconfig.json文件将"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"]
这篇关于在Ionic 3的.spec.ts文件中找不到模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!