Angular ci 测试在本地的结果与 DevOps 不同
我的管道有问题。我知道我当前的 Karma 测试有问题,但我在本地运行测试和在 DevOps 管道上运行测试时遇到了 不一致 的问题
karma.conf.ci.js
// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/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-jasmine-html-reporter'),
require('karma-coverage'),
require('@angular-devkit/build-angular/plugins/karma'),
require('karma-junit-reporter')
],
client: {
clearContext: false // leave Jasmine Spec Runner output visible in browser
},
coverageIstanbulReporter: {
dir: require('path').join(__dirname, './coverage/'),
reports: ['html', 'lcovonly', 'text-summary', 'cobertura'],
fixWebpackSourcePaths: true
},
reporters: ['progress', 'kjhtml', 'junit'],
junitReporter: {
outputDir: 'test-output'
},
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['ChromeHeadless'],
singleRun: true
});
};
命令是
npm run test-ci
(
"test-ci": "ng test --code-coverage --karmaConfig=karma.conf.ci.js"
)
修复一些测试并排除其他测试后,我让此命令在 WebStorm 本地以退出代码 0 运行
在 WebStorm 上,它报告了很多警告
√ Browser application bundle generation complete.
07 04 2021 11:55:27.299:INFO [Chrome Headless 89.0.4389.114 (Windows 10)]: Connected on socket a_CSFlZVK9QoDFJIAAAB with id 48577154
LOG: 'Wanted to read 'xxxxxx_authWellKnownEndPoints' but nothing was found'
Chrome Headless 89.0.4389.114 (Windows 10): Executed 0 of 18 SUCCESS (0 secs / 0 secs)
LOG: 'Wanted to read 'xxxxxx_authWellKnownEndPoints' but nothing was found'
Chrome Headless 89.0.4389.114 (Windows 10): Executed 0 of 18 SUCCESS (0 secs / 0 secs)
LOG: 'Did not find matching route for https://login.microsoftonline.com/xxxxxxx/v2.0/.well-known/openid-configuration'
Chrome Headless 89.0.4389.114 (Windows 10): Executed 0 of 18 SUCCESS (0 secs / 0 secs)
LOG: 'Wanted to read 'xxxxxx_authWellKnownEndPoints' but nothing was found'
Chrome Headless 89.0.4389.114 (Windows 10): Executed 5 of 18 SUCCESS (0 secs / 0.133 secs)
LOG: 'Wanted to read 'xxxxxx_authWellKnownEndPoints' but nothing was found'
Chrome Headless 89.0.4389.114 (Windows 10): Executed 5 of 18 SUCCESS (0 secs / 0.133 secs)
Chrome Headless 89.0.4389.114 (Windows 10): Executed 7 of 18 (skipped 1) SUCCESS (0 secs / 0.336 secs)
Chrome Headless 89.0.4389.114 (Windows 10): Executed 15 of 18 (skipped 3) SUCCESS (0.609 secs / 0.452 secs)
TOTAL: 15 SUCCESS
TOTAL: 15 SUCCESS
Process finished with exit code 0
在 Azure 上,结果不同
azure-pipelines.yml
- task: Npm@1
inputs:
command: 'custom'
customCommand: 'run test-ci'
displayName: 'Execute Unit Tests'
这是一个有趣的部分
Chrome Headless 89.0.4389.90 (Linux x86_64) FilterFormBuilderService should be created FAILED
HttpErrorResponse: Http failure response for https://localhost:5001/api/v1/secure/xxxxxxx: 0 Unknown Error
Chrome Headless 89.0.4389.90 (Linux x86_64): Executed 14 of 18 (1 FAILED) (skipped 3) (0 secs / 0.787 secs)
Chrome Headless 89.0.4389.90 (Linux x86_64) FilterFormBuilderService should be created FAILED
HttpErrorResponse: Http failure response for https://localhost:5001/api/v1/secure/xxxxxxx: 0 Unknown Error
Chrome Headless 89.0.4389.90 (Linux x86_64): Executed 15 of 18 (1 FAILED) (skipped 3) (0 secs / 0.854 secs)
Chrome Headless 89.0.4389.90 (Linux x86_64): Executed 15 of 18 (1 FAILED) (skipped 3) (1.406 secs / 0.854 secs)
- Generating browser application bundles...
✔ Browser application bundle generation complete.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] test-ci: `ng test --code-coverage --karmaConfig=karma.conf.ci.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] test-ci script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
这两次很有趣
-
FilterFormBuilderService
规范 不 依赖(三重检查)任何 Http 组件 - 在本地,端口 :5001 处的 API 服务器已关闭
问题
为什么测试在 CI 上失败但在本地成功?
为什么我的测试脚本由于一个简单的 http 无法访问主机错误而 出现错误 1 ,尽管所有单独的简单测试都成功了?如果测试依赖于 http, 该 测试应报告为失败,以便实施者可以修复或忽略测试。
看来您的项目依赖于构建环境。如果您在 Azure 管道中使用 Microsoft 托管代理 ,其环境与您的本地计算机不同,因此您会看到不同的构建结果。您可以 部署自托管代理 ,这可让您更好地控制安装构建和部署所需的依赖软件。然后在管道中使用它,这样它就会通过在本地机器上运行来构建您的项目。
顺便说一句,您可以在管道中将变量 system.debug 设置为 true 以获取 调试日志 ,这样您就可以查看它以获取详细的构建信息。
在多次尝试运行管道后,我通过 排除 找到了罪魁祸首测试。
我进行了一项测试,测试中的组件由于服务依赖关系而需要执行一些 AJAX 调用。是的,它违反了单元测试的 单元 原则,但我就是无法找到该测试并确定它是否因环境而导致失败。
我发帖是为了回答这个问题,但我真的很困惑,因为我还没有真正解决我的问题:理解测试为什么会和管道一起失败并出现错误代码 1。