Javascript 角度单元测试-模块不是功能
我使用的是Javascript 角度单元测试-模块不是功能,javascript,angularjs,webpack,ecmascript-6,karma-runner,Javascript,Angularjs,Webpack,Ecmascript 6,Karma Runner,我使用的是ES6,angular 1.5.8,webpack(babel loader) 我对angular和karma是新手,我试着做一些单元测试。因果报应抛出一个错误: Chrome 55.0.2883(Windows 10 0.0.0)日志:“警告:试图加载 角度不止一次。'Chrome 55.0.2883(Windows 10 0.0.0)Youtube api服务应从长youtube链接获取ID失败 TypeError:(0,_youtube2.default)不是函数 反对。(C:/
ES6,angular 1.5.8,webpack(babel loader)
我对angular和karma是新手,我试着做一些单元测试。因果报应抛出一个错误:
Chrome 55.0.2883(Windows 10 0.0.0)日志:“警告:试图加载
角度不止一次。'Chrome 55.0.2883(Windows 10 0.0.0)Youtube
api服务应从长youtube链接获取ID失败
TypeError:(0,_youtube2.default)不是函数
反对。(C:/projects/angular videos library/app/src/library/api/api.spec.js:60:40)
TypeError:无法读取未定义的属性“getId”
反对。(C:/projects/angular videos library/app/src/library/api/api.spec.js:66:24)
Chrome 55.0.2883(Windows 10 0.0.0):执行1/1(1失败)错误
(0.007秒/0.003秒)22 01 2017 05:13:37.949:INFO[Firefox 50.0.0
(Windows 10 0.0.0)]:连接在带有id的插座SCXEU8MPU8MCH8QJ AAAB上
21147日志:“警告:尝试多次加载。”Firefox
50.0.0(Windows 10 0.0.0)Youtube api服务应从长Youtube链接获取ID失败
TypeError:_youtube2.default不是C:/projects/angular videos library/app/src/library/api/api.spec.js中的函数
(第60行)
[0]/
下面是api.spec.js
:
import YoutubeService from './youtube.service';
describe('Youtube api service', () => {
let YtService;
beforeEach(() => {
YtService = YoutubeService();
});
it('should get an ID from long youtube link', () => {
let link = 'https://www.youtube.com/watch?v=TFeSNOdNtyo';
let id = YtService.getId(link);
expect(id).toBe('TFeSNOdNtyo');
});
});
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'node_modules/angular/angular.min.js',
'app/src/app.module.js',
'app/src/**/*.spec.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'app/src/**/*.js': ["webpack"],
'app/src/**/*.spec.js': ["webpack"]
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome', 'Firefox'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
webpack: {
module: {
loaders: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loaders: ['ng-annotate', 'babel?presets[]=es2015'] // 'babel-loader' is also a valid name to reference
}]
}
}
})
}
karma.conf.js
:
import YoutubeService from './youtube.service';
describe('Youtube api service', () => {
let YtService;
beforeEach(() => {
YtService = YoutubeService();
});
it('should get an ID from long youtube link', () => {
let link = 'https://www.youtube.com/watch?v=TFeSNOdNtyo';
let id = YtService.getId(link);
expect(id).toBe('TFeSNOdNtyo');
});
});
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: './',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
// list of files / patterns to load in the browser
files: [
'node_modules/angular/angular.min.js',
'app/src/app.module.js',
'app/src/**/*.spec.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'app/src/**/*.js': ["webpack"],
'app/src/**/*.spec.js': ["webpack"]
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome', 'Firefox'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
webpack: {
module: {
loaders: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loaders: ['ng-annotate', 'babel?presets[]=es2015'] // 'babel-loader' is also a valid name to reference
}]
}
}
})
}
我怎样才能修好它
编辑1
在Alexander提出更改后,我得到如下错误:
ERROR in ./app/src/library/api/apis.spec.js
Module not found: Error: a dependency to an entry point is not allowed
@ ./app/src/library/api/apis.spec.js 3:15-43
webpack: bundle is now VALID.
22 01 2017 08:14:50.407:ERROR [karma]: { [Error: no such file or directory]
code: 'ENOENT',
errno: 34,
message: 'no such file or directory',
path: '/_karma_webpack_/app/src/library/api/vimeo.service.js' }
Error: no such file or directory
at MemoryFileSystem.readFileSync (C:\projects\angular-videos-library\node_modules\karma-webpack\node_modules\webpack-dev-middleware\node_modules\memory-fs\li
b\MemoryFileSystem.js:114:10)
at MemoryFileSystem.(anonymous function) [as readFile] (C:\projects\angular-videos-library\node_modules\karma-webpack\node_modules\webpack-dev-middleware\nod
e_modules\memory-fs\lib\MemoryFileSystem.js:300:34)
at doRead (C:\projects\angular-videos-library\node_modules\karma-webpack\lib\karma-webpack.js:203:29)
at Plugin.readFile (C:\projects\angular-videos-library\node_modules\karma-webpack\lib\karma-webpack.js:207:5)
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)
在测试中,您需要指定应用程序正在使用的角度模块/应用程序名称。例如每个模块(模块('myApp'))之前的
代码>。您还需要在项目和karma配置中包含角度模拟。好的,在每个之前添加更好(angular.module('app))代码>(这是(模块('app')的一个错误)
模块不是一个函数
。现在我有其他错误。根据错误,它看起来无法找到特定的文件,vimeo.service.js
。您可能需要特别将其包含在karma.conf.js
文件
数组中。请记住,您将无法在数组中进行实际的$http调用您的测试没有重要的配置。相反,您将使用$httpBackend以及模拟服务/响应来测试各种功能。我强烈建议您在Angular中对模拟依赖项以及测试服务进行更多的研究。