我正在尝试将Angular 5项目更新为Angular 6,all of the sources are available online(Commit #1,Commit #2),但我会尝试在此处突出显示相关的代码段。使用Angular 5一切正常,并且最初为迁移所做的更改根本没有触及测试设置。
我没有设法将我的问题转变为最小的复制案例,但如果有人愿意尝试在自己的机器上运行我的确切代码,我已在此问题的底部包含说明。您还可以看到确切的输出in the bitbucket-Pipeline log。
我遇到的基本症状是以下"Executed 0 of 0"
- 运行ng test
时出错:
marcus@marcus-pc:~/p/s/client >>> node_modules/.bin/ng test
10% building modules 1/1 modules 0 active(node:6192) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.hooks` instead
10% building modules 3/3 modules 0 active10 05 2018 10:55:59.897:INFO [karma]: Karma v1.7.1 server started at http://0.0.0.0:9876/
10 05 2018 10:55:59.898:INFO [launcher]: Launching browsers ChromeHeadless, FirefoxHeadless with unlimited concurrency
10% building modules 3/4 modules 1 active …ratch-sql/client/src sync /\.spec\.ts$/10 05 2018 10:55:59.902:INFO [launcher]: Starting browser Chrome
10 05 2018 10:55:59.912:INFO [launcher]: Starting browser Firefox
10 05 2018 10:56:06.064:INFO [HeadlessChrome 0.0.0 (Linux 0.0.0)]: Connected on socket inKwlXEJ84kIUYzUAAAA with id 55152332
10 05 2018 10:56:06.082:INFO [Firefox 59.0.0 (Linux 0.0.0)]: Connected on socket LmNu7uNvlTgbKPKoAAAB with id 83729562
HeadlessChrome 0.0.0 (Linux 0.0.0): Executed 0 of 0 ERROR (0.017 secs / 0 secs)
Firefox 59.0.0 (Linux 0.0.0): Executed 0 of 0 ERROR (0.004 secs / 0 secs)
我最初怀疑这是一个路径问题,但是当查看karma调试运行器中的已加载源时,我的所有.spec.ts
文件似乎都已正确编译并存在:
所以我的一般配置可能有问题。因此,我检查了我的angular.json
以查看哪些文件似乎涉及测试目标:
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"karmaConfig": "./karma.conf.js",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.test.json",
}
},
我的src/test.ts
文件似乎有点过时(at least the angular documentation page uses a different file),看起来像这样。使用aio-project中提到的文件会产生相同的结果:
import './polyfills.ts';
import 'zone.js/dist/long-stack-trace-zone';
import 'zone.js/dist/proxy.js';
import 'zone.js/dist/sync-test';
import 'zone.js/dist/jasmine-patch';
import 'zone.js/dist/async-test';
import 'zone.js/dist/fake-async-test';
import { getTestBed } from '@angular/core/testing';
import {
BrowserDynamicTestingModule,
platformBrowserDynamicTesting
} from '@angular/platform-browser-dynamic/testing';
// Unfortunately there's no typing for the `__karma__` variable. Just declare it as any.
declare var __karma__: any;
declare var require: any;
// Prevent Karma from running prematurely.
__karma__.loaded = function() { };
// First, initialize the Angular testing environment.
getTestBed().initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
);
// Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
// And load the modules.
context.keys().map(context);
// Finally, start Karma to run the tests.
__karma__.start();
最后,我的karma.conf
与我的"aio reference file"略有不同(我的包括一位JUnit记者),但再次使用"原创"文件没有改变发现" 0规格"错误。
// Karma configuration file, see link for more information
// https://karma-runner.github.io/0.13/config/configuration-file.html
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['jasmine', '@angular-devkit/build-angular'],
plugins: [
require('karma-jasmine'),
require('karma-chrome-launcher'),
require('karma-firefox-launcher'),
require('karma-jasmine-html-reporter'),
require('karma-coverage-istanbul-reporter'),
require('karma-junit-reporter'),
require('@angular-devkit/build-angular/plugins/karma'),
],
client:{
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, 'coverage'),
reports: [ 'html', 'lcovonly' ],
fixWebpackSourcePaths: true
},
junitReporter: {
outputDir: 'test-results'
},
angularCli: {
environment: 'dev'
},
reporters: ['junit', 'progress', 'kjhtml'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
customLaunchers: {
ChromeHeadless: {
base: 'Chrome',
flags: [
'--headless',
'--disable-gpu',
// Without a remote debugging port, Google Chrome exits immediately.
'--remote-debugging-port=9222',
'--no-sandbox'
],
},
FirefoxHeadless: {
base: 'Firefox',
flags: [
'--headless'
]
}
},
browsers: ['ChromeHeadless', 'FirefoxHeadless'],
singleRun: true
});
};
如果您仍在阅读本文并愿意在您的计算机上运行我的代码,您可以执行以下操作:
angular-6
分支(git clone "https://bitbucket.org/marcusriemer/esqulino.git" -b angular-6
)。client
文件夹(cd client
)Makefile
快捷方式(make install-deps client-test
)或"正常" npm
和ng
组合(npm install && node_modules/.bin/ng test
)。