无法使用 gulp.js 读取未定义的 属性 'start'

Cannot Read Property 'start' of undefined using gulp.js

因此,当我使用以下内容时,GULP 中出现 1 个错误:

node ./node_modules/gulp/bin/gulp.js test

这是我在 运行: node ./node_modules/gulp/bin/gulp.js test

时遇到的错误
PS C:\Users\myuser\repos\wwwroot\acqustn-aem-global-comps\ui.apps> node 
./node_modules/gulp/bin/gulp.js test
[09:41:14] Using gulpfile ~\repos\wwwroot\global-comps\ui.apps\gulpfile.js
[09:41:14] Starting 'test'...
[09:41:14] 'test' errored after 4.45 ms
[09:41:14] TypeError: Cannot read property 'start' of undefined
at C:\Users\myuser\repos\wwwroot\global-comps\ui.apps\gulpfile.js:12:11
at test (C:\Users\myuser\repos\wwwroot\global-comps\ui.apps\node_modules\undertaker\lib\set-task.js:13:15)     
at bound (domain.js:426:14)
at runBound (domain.js:439:12)
at asyncRunner (C:\Users\myuser\repos\wwwroot\global-comps\ui.apps\node_modules\async-done\index.js:55:18)     
at processTicksAndRejections (internal/process/task_queues.js:79:11)

这是我的 karma.conf.js 文件,其格式如下。我要在这里添加更多文字,因为我不能 post 除非我描述更多信息

module.exports = function (config) {
  const process = require('process');
  process.env.CHROME_BIN = require('puppeteer').executablePath();
  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-jquery', 'jasmine'],

    plugins: [
      require('jquery'),
      require('karma-junit-reporter'),
      require('karma-coverage'),
      require('karma-jasmine-jquery-2'),
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-phantomjs-launcher'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-sonarqube-unit-reporter')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, './coverage/global-comps'),
      reports: ['lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    // list of files / patterns to load in the browser
    files: [
      './src/main/content/jcr_root/etc/designs/global-components/footer/**/*.js',
      './src/main/content/jcr_root/etc/designs/global-components/header/**/*.js',
      './src/main/content/jcr_root/etc/designs/global-components/action/**/*.js'
    ],


    // list of files / patterns to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      './test/navigationClientLibs/js/header.spec.js': ['coverage']
    },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['junit', 'progress', 'kjhtml', 'coverage-istanbul', 'sonarqubeUnit', 'coverage'],
    sonarQubeUnitReporter: {
      sonarQubeVersion: 'LATEST',
      outputFile: 'reports/ut_report.xml',
      overrideTestDescription: true,
      testPaths: ['./test/navigationClientLibs/'],
      testFilePattern: '.js',
      useBrowserName: false
    },

    // 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: ['PhantomJS'],

    // 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,

    junitReporter: {
      outputFile: 'reports/junit/TESTS-xunit.xml'
    },

    coverageReporter: {
      type: 'lcov',
      dir: 'reports',
      subdir: 'coverage'
    }
  })
}

现在我的 GULP FILE 失败并显示 karma.start() 每当我 运行 以下节点 ./node_modules/gulp/bin/gulp.js 在 VS Code 中的命令提示符下测试航站楼

    var gulp = require('gulp');
    var karma = require('karma').server;
    var replace = require('gulp-replace');

    var postprocessLCOV = function() {
        return gulp.src('reports/coverage/lcov.info')
            .pipe(replace('SF:.', 'SF:frontend-project'))
            .pipe(gulp.dest('reports/coverage'));
    };

    gulp.task('test', function () {
        karma.start({
            configFile: __dirname + '/src/test/js/karma.conf.ci.js'
        }, postprocessLCOV);
    });

注意:我的 karma.conf.js 和 .ci.js 完全相同,所以我不必 post 两个文件都在这里。

最后,我的 package.json 显示了我正在使用的所有内容。每当我添加新库时,我都必须删除 package.lock.json,安装新库后,然后重新 运行 npm install.

{
  "name": "global-comps",
  "version": "1.0.0",
  "description": "This is for the Navigator",
  "main": "qunit.js",
  "scripts": {
    "lint": "eslint '**/*.js'",
    "test": "qunit",
    "compodoc": "npx compodoc -p tsconfig.doc.json",
    "ng": "ng",
    "clean": "rm -rf node*",
    "ng-cli": "npm install --no-optional @angular/cli",
    "start": "ng serve --proxy-config proxy.config.json",
    "build": "ng build",
    "prod": "node --max_old_space_size=8192 node_modules/@angular/cli/bin/ng build --prod --output-hashing none && npm run test && npm run clean",
    "e2e": "ng e2e",
    "sonar": "sonar-scanner"
  },
  "repository": {
    "type": "git",
    "url": "somerepo.git"
  },
  "author": "Some Author",
  "license": "MIT",
  "dependencies": {
    "@angular/common": "~9.0.2",
    "@angular/compiler": "~9.0.2",
    "@angular/core": "~9.0.2",
    "@angular/platform-browser": "~9.0.2",
    "@angular/platform-browser-dynamic": "~9.0.2",
    "@angular/router": "~9.0.2",
    "@types/mocha": "^9.0.0",
    "JSONPath": "^0.11.2",
    "browserify": "^17.0.0",
    "common-js": "^0.3.8",
    "eslint-plugin-qunit": "^6.1.1",
    "jquery": "^3.4.1",
    "jquery-browserify": "^1.8.1",
    "jsdom": "^16.5.3",
    "junit": "^1.4.9",
    "karma-junit-reporter": "^2.0.1",
    "ng-mocks": "^11.10.1",
    "node": "^15.12.0",
    "npm-check-updates": "^11.5.13",
    "phantomjs": "^2.1.7",
    "qunit-reporter-junit": "^1.1.1",
    "qunit-reporter-lcov": "^1.0.2",
    "require": "^2.4.20",
    "rxjs": "~6.5.4",
    "tslib": "^1.10.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^12.1.3",
    "@angular-devkit/build-webpack": "^0.1201.3",
    "@angular/compiler-cli": "~9.0.2",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/jsdom": "^16.2.13",
    "@types/node": "^12.11.1",
    "codelyzer": "^5.1.2",
    "grunt": "^1.3.0",
    "grunt-contrib-connect": "^3.0.0",
    "grunt-contrib-copy": "^1.0.0",
    "grunt-contrib-qunit": "^4.0.0",
    "grunt-git-authors": "^3.2.0",
    "grunt-search": "^0.1.8",
    "gulp": "^4.0.2",
    "gulp-replace": "^1.1.3",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "^6.3.2",
    "karma-chrome-launcher": "^3.1.0",
    "karma-coverage": "^2.0.3",
    "karma-coverage-istanbul-reporter": "^3.0.3",
    "karma-jasmine": "^4.0.1",
    "karma-jasmine-html-reporter": "^1.7.0",
    "karma-jasmine-jquery-2": "^0.1.1",
    "karma-mocha-reporter": "^2.2.5",
    "karma-phantomjs-launcher": "^1.0.4",
    "karma-qunit": "^4.1.2",
    "karma-sonarqube-unit-reporter": "0.0.23",
    "karma-webpack": "^5.0.0",
    "protractor": "~5.4.3",
    "puppeteer": "^1.20.0",
    "sonar-scanner": "^3.1.0",
    "sonarqube-scanner": "~2.7.0",
    "ts-node": "~8.3.0",
    "typescript": "~3.7.5"
  },
  "global": "window"
}

所以,这就是我所在的位置。

更新!

所以,我放弃了 GULP,让 Karma 开始工作。

但是在 SONARQUBE 中,我得到的是 0% 的覆盖率,因为 Karma 没有创建 lcov.info 文件,即使它正在创建覆盖目录和后续文件。

注意: 我可以 运行 在 QUNIT 的浏览器中进行测试,并查看它们在任何情况下的成功或失败。

但是我需要 KARMA 来创建 lcov.info 文件,其中包含我在 .js 文件中创建的测试。因为这不是 Angular 而只是纯粹的 JavaScript,这就是我被困的地方。

这是我的 SONARQUBE Coverage 的屏幕截图,向您展示我得到的结果。

我真的很想在这里得到一些帮助。

同样,这里有两个简单的测试文件,我 运行 正在使用 QUNIT 工作,但当我 运行 在 KARMA 中时它们不工作。

感谢大家一直以来的支持。

    describe('A Suite for a simple test', () => {

        var a;

        it('and so it is a spec', () => {
            a = true;
            expect(a).toBe(true);
        });

        it('2 plus 2 should be 4', () => {
            const result = add(2, 2);

            expect(result).toBe(4);

        });

    });

    describe('Test case 1', () => {
        var zipcodevalue = '';

        const test1 = sendToTest1;

        console.log('Test 1 is: ', test1);

        beforeEach(function () {
            zipToTestCase.call(this, {
                assert: assert,
                zipcodevalue: '98237',
                expected: "Success"
            });
        });

        function zipToTestCase() {

            if (isNaN(zipcodevalue === false)) {
                expect(zipcodevalue, '98237', 'string value and hard code value are NOT equal', function (callback) {
                    callback(zipcodevalue);
                });
            } else {
                expect(zipcodevalue, '98237', 'string value and hard code value are equal', function (callback) {
                    callback(zipcodevalue);
                });
            }

        }

        it('test for Valid ZipCode', () => {
            expect(zipcodevalue).toBeTruthy();
        })

        it('test for INValid ZipCode', () => {
            expect(zipcodevalue).toBeFalsy();
        })

    });

我在这篇文章中找到了答案!

https://sourcedcode.com/blog/aem/how-to-write-javascript-unit-tests-for-aem-client-libraries-with-jest

这正是我所需要的,结果非常完美!