単体テストについて考えずにアプリケーションのコーディングを開始しました。しかし、今私はそれらを書く必要があります。私はジャスミンを使用してテストを書こうとしましたが、コンポーネントのいずれかをインポートしてウェブブラウザでジャスミンを起動するとエラーが発生します。「システムは定義されていません」。カルマとジャスミンを既存のプロジェクトに追加することは可能ですか?私はいくつかのチュートリアルを読んだが、それらのすべてが新しいプロジェクトのためであり、正直言って私はいくつかのことを理解していない(私はかなり新しい)。ですから、誰かが少し言葉で記述できる場合、どのようにangular2アプリのための基本的なテスト環境を設定するには、私は感謝します。あなたの答えに事前に感謝します。角2テスト開始方法
0
A
答えて
0
は、次の設定を追加する必要があります、package.json
ファイルにカルマの依存関係を
- をし、それを実行するためのコマンド
- カルマ構成用
karma.conf.js
ファイル - 活字体のテストをロードする
karma-test-shim.js
ファイルSystemJSを使用して
package.json
ファイルの内容:ここで
{
"name": "test-karma",
"version": "1.0.0",
"scripts": {
"build": "rm -rf dist && tsc",
"pretest": "npm run build",
"test": "karma start karma.conf.js",
"start": "concurrent \"npm run tsc:w\" \"npm run lite\" ",
"tsc": "tsc",
"tsc:w": "tsc -w",
"lite": "lite-server",
"postinstall": "typings install",
"typings": "typings"
},
"license": "ISC",
"dependencies": {
"@angular/common": "2.0.0-rc.1",
"@angular/compiler": "2.0.0-rc.1",
"@angular/core": "2.0.0-rc.1",
"@angular/http": "2.0.0-rc.1",
"@angular/platform-browser": "2.0.0-rc.1",
"@angular/platform-browser-dynamic": "2.0.0-rc.1",
"@angular/router": "2.0.0-rc.1",
"@angular/router-deprecated": "2.0.0-rc.1",
"@angular/upgrade": "2.0.0-rc.1",
"systemjs": "0.19.6",
"es6-shim": "^0.35.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.12",
"angular2-in-memory-web-api": "0.0.7",
"bootstrap": "^3.3.6"
},
"devDependencies": {
"concurrently": "^2.0.0",
"jasmine-core": "^2.4.1",
"karma": "^0.13.22",
"karma-chrome-launcher": "^1.0.1",
"karma-coverage": "^1.0.0",
"karma-firefox-launcher": "^1.0.0",
"karma-jasmine": "^1.0.2",
"karma-phantomjs-launcher": "^1.0.0",
"lite-server": "^2.2.0",
"phantomjs": "^2.1.7",
"phantomjs-prebuilt": "^2.1.7",
"typescript": "^1.8.10",
"typings": "^0.8.1",
"uglify": "^0.1.5"
}
}
はカルマ構成(karma.conf.js
)です:
'use strict';
module.exports = function(config) {
config.set({
basePath: '.',
frameworks: ['jasmine'],
files: [
// Paths loaded by Karma
{pattern: 'node_modules/es6-shim/es6-shim.min.js', included: true, watched: true},
{pattern: 'node_modules/reflect-metadata/Reflect.js', included: true, watched: true},
{pattern: 'node_modules/zone.js/dist/zone.js', included: true, watched: true},
{pattern: 'node_modules/zone.js/dist/async-test.js', included: true, watched: true},
{pattern: 'node_modules/systemjs/dist/system-polyfills.js', included: true, watched: true},
{pattern: 'node_modules/systemjs/dist/system.src.js', included: true, watched: true},
{pattern: 'node_modules/rxjs/**/*.js', included: false, watched: false},
{pattern: 'node_modules/@angular/**/*.js', included: false, watched: false},
{pattern: 'karma-test-shim.js', included: true, watched: true},
// Paths loaded via module imports
{pattern: 'app/**/*.js', included: false, watched: true},
// Paths to support debugging with source maps in dev tools
{pattern: 'app/**/*.ts', included: false, watched: true},
{pattern: 'app/**/*.js.map', included: false, watched: false}
],
// Proxied base paths
proxies: {
// Required for component assests fetched by Angular's compiler
'/app/': '/base/app/'
},
port: 9876,
logLevel: config.LOG_INFO,
colors: true,
autoWatch: true,
browsers: ['PhantomJS', 'Chrome', 'Firefox'],
// Karma plugins loaded
plugins: [
'karma-jasmine',
'karma-coverage',
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-phantomjs-launcher'
],
// Coverage reporter generates the coverage
reporters: ['progress', 'dots', 'coverage'],
// Source files that you wanna generate coverage for.
// Do not include tests or libraries (these files will be instrumented by Istanbul)
preprocessors: {
'dist/**/!(*spec).js': ['coverage']
},
coverageReporter: {
reporters: [
{type: 'json', subdir: '.', file: 'coverage-final.json'}
]
},
singleRun: true
});
};
とkarma-test-shim.js
ファイル:
'use strict';
// Turn on full stack traces in errors to help debugging
Error.stackTraceLimit = Infinity;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000;
// Cancel Karma's synchronous start,
// we will call `__karma__.start()` later, once all the specs are loaded.
__karma__.loaded = function() {};
var map = {
'app': 'base/app',
'rxjs': 'base/node_modules/rxjs',
'@angular': 'base/node_modules/@angular'
};
var packages = {
'app': { main: 'main.js', defaultExtension: 'js' },
'rxjs': { defaultExtension: 'js' }
};
var packageNames = [
'@angular/common',
'@angular/compiler',
'@angular/core',
'@angular/http',
'@angular/platform-browser',
'@angular/platform-browser-dynamic',
'@angular/router',
'@angular/router-deprecated',
'@angular/testing',
'@angular/upgrade',
];
packageNames.forEach(function(pkgName) {
packages[pkgName] = { main: 'index.js', defaultExtension: 'js' };
});
packages['base/app'] = {
defaultExtension: 'js',
format: 'cjs',
map: Object.keys(window.__karma__.files).filter(onlyAppFiles).reduce(createPathRecords, {})
};
var config = {
map: map,
packages: packages
};
System.config(config);
System.import('@angular/platform-browser/src/browser/browser_adapter')
.then(function(browser_adapter) { browser_adapter.BrowserDomAdapter.makeCurrent(); })
.then(function() { return Promise.all(resolveTestFiles()); })
.then(function() { __karma__.start(); }, function(error) { __karma__.error(error.stack || error); });
function createPathRecords(pathsMapping, appPath) {
var pathParts = appPath.split('/');
var moduleName = './' + pathParts.slice(Math.max(pathParts.length - 2, 1)).join('/');
moduleName = moduleName.replace(/\.js$/, '');
pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath];
return pathsMapping;
}
function onlyAppFiles(filePath) {
return /\/base\/app\/(?!.*\.spec\.js$).*\.js$/.test(filePath);
}
function onlySpecFiles(path) {
return /\.spec\.js$/.test(path);
}
function resolveTestFiles() {
return Object.keys(window.__karma__.files) // All files served by Karma.
.filter(onlySpecFiles)
.map(function(moduleName) {
// loads all spec files via their global module names (e.g.
// 'base/dist/vg-player/vg-player.spec')
return System.import(moduleName);
});
}
関連する問題
- 1. 角度2 NPM開始エラー
- 2. 角2クイックスタートnpm開始エラー
- 3. 新しいJavaScriptプロジェクト(テスト、開発、ビルド)の開始方法は?
- 4. 角度2を開始する前に関数を実行する方法
- 5. 角度2で開始タイマーを開始する機能はありますか?
- 6. jXchange - 開始方法
- 7. N2Cms開始方法
- 8. 模擬エンドポイントを使ったCamelテストのルートの開始方法
- 9. 角度サービス(投稿、取得、削除)を開始する方法
- 10. 角度 - 開始時にクリックイベントを設定する方法
- 11. 角2 /角形:Webサーバーに展開する方法
- 12. SVGダッシュオフセット開始角度
- 13. 角2テストkendo-autocomplete
- 14. 角度2テスト・セットアップ
- 15. 角2のテスト - process.env
- 16. 角度2 e2eテスト
- 17. 角度2のテスト
- 18. ビジュアルスタジオユニットテスト「デバッグ開始」はすべてのテストを開始
- 19. DBusサービスの開始方法
- 20. Amazon Webサービス、開始方法
- 21. アクティビティの開始方法ダイアログ
- 22. 自動メールプロセス - 開始方法
- 23. Silverlightストーリーボード:開始方法?
- 24. 角度2のライブサイトがnpm開始サイトと異なる
- 25. 角2:設定を保存するための開始ダイアログ
- 26. 角2 - ガードする前のセッションサービスを開始する
- 27. 角2はAppComponentから開始ページに移動しません
- 28. 角2フォームの検証開始日> =終了日
- 29. Zurb's Foundation 'Joyride':開始方法/プログラム方法
- 30. 2フェーズコミット - テスト方法
バージョンのAngular2を使用しますか? ? –
@ThierryTemplier 2.0.0-rc.1 – Agr