JHipster新应用程序测试在ReferenceError上失败:XMLHttpRequest

时间:2018-08-07 15:13:36

标签: jhipster

我已经创建了一个新的应用程序JHipster,并且没有任何修改,我无法打包它。

这是我的.yo-rc.json

 {
 "generator-jhipster": {
    "promptValues": {
    "packageName": "testbug",
    "nativeLanguage": "en"
    },
    "jhipsterVersion": "5.1.0",
    "applicationType": "monolith",
    "baseName": "testBugJhipster",
    "packageName": "testbug",
    "packageFolder": "testbug",
    "serverPort": "8080",
    "authenticationType": "jwt",
    "cacheProvider": "ehcache",
    "enableHibernateCache": true,
    "websocket": "spring-websocket",
    "databaseType": "sql",
    "devDatabaseType": "h2Memory",
    "prodDatabaseType": "mysql",
    "searchEngine": "elasticsearch",
    "messageBroker": "kafka",
    "serviceDiscoveryType": "eureka",
    "buildTool": "maven",
    "enableSwaggerCodegen": true,
    "jwtSecretKey": "a1643dc27d4bad4c20782e29fe5491fc3ecdf5e5",
    "clientFramework": "angularX",
    "useSass": true,
    "clientPackageManager": "yarn",
    "testFrameworks": [
    "gatling",
    "cucumber",
    "protractor"
    ],
    "jhiPrefix": "jhi",
    "enableTranslation": true,
    "nativeLanguage": "en",
    "languages": [
    "en",
    "fr"
    ]
}
}

要打包,我使用

./mvnw -Pprod package

起初,我遇到此错误:

[INFO] Running 'yarn run webpack:test' in /home/korrident/testBugJhipster
[INFO] yarn run v1.6.0
[INFO] $ yarn run test
[INFO] $ yarn run lint && jest --coverage --logHeapUsage -w=2 --config src/test/javascript/jest.conf.js
[INFO] $ tslint --project tsconfig.json -e 'node_modules/**'
[ERROR] No valid rules have been specified
[ERROR] FAIL src/test/javascript/spec/app/shared/login/login.component.spec.ts
[ERROR]   ● Test suite failed to run
[ERROR] 
[ERROR]     SecurityError: localStorage is not available for opaque origins
[ERROR]       
[ERROR]       at Window.get localStorage [as localStorage] (node_modules/jsdom/lib/jsdom/browser/Window.js:257:15)
[ERROR]           at Array.forEach (<anonymous>)
[ERROR] 

基于此SO: client side tests fail using jhipster 5.1.0

我在src / test / javascript / jest.conf.js的末尾添加了

testEnvironment: "node",
testURL: "http://localhost/"

但是现在,我遇到了这个错误

[INFO] $ yarn run test
[INFO] $ yarn run lint && jest --coverage --logHeapUsage -w=2 --config src/test/javascript/jest.conf.js
[INFO] $ tslint --project tsconfig.json -e 'node_modules/**'
[ERROR] No valid rules have been specified
[ERROR] FAIL src/test/javascript/spec/app/admin/health/health.component.spec.ts
[ERROR]   ● Test suite failed to run
[ERROR] 
[ERROR]     ReferenceError: XMLHttpRequest is not defined
[ERROR]       
[ERROR]       at patchXHR (node_modules/zone.js/dist/zone.js:2926:39)
[ERROR]       at node_modules/zone.js/dist/zone.js:2919:5
[ERROR]       at Function.Object.<anonymous>.Zone.__load_patch (node_modules/zone.js/dist/zone.js:84:33)
[ERROR]       at node_modules/zone.js/dist/zone.js:2917:6
[ERROR]       at Object.<anonymous>.FUNCTION (node_modules/zone.js/dist/zone.js:9:65)
[ERROR]       at Object.<anonymous> (node_modules/zone.js/dist/zone.js:12:2)
[ERROR]       at Object.<anonymous> (node_modules/jest-preset-angular/setupJest.js:5:1)
[ERROR] 

除了jest.conf.js,git diff都没有显示来自初始提交的其他更改。

我尝试安装xmlhttprequest@^1.8.0,但玩笑的结果没有变化。

这个问题并不紧急,因为我可以禁用测试,但是我不能允许它太长时间。

1 个答案:

答案 0 :(得分:0)

感谢@ jared-smith解决它。

在src / test / javascript / jest.conf.js中正确配置

module.exports = {
    preset: 'jest-preset-angular',
    setupTestFrameworkScriptFile: '<rootDir>/src/test/javascript/jest.ts',
    coverageDirectory: '<rootDir>/target/test-results/',
    globals: {
        'ts-jest': {
            tsConfigFile: 'tsconfig.json'
        },
        __TRANSFORM_HTML__: true
    },
    moduleNameMapper: {
        'app/(.*)': '<rootDir>/src/main/webapp/app/$1'
    },
    reporters: [
        'default',
        [ 'jest-junit', { output: './target/test-results/jest/TESTS-results.xml' } ]
    ],
    testResultsProcessor: 'jest-sonar-reporter',
    transformIgnorePatterns: ['node_modules/(?!@angular/common/locales)'],
    testMatch: ['<rootDir>/src/test/javascript/spec/**/+(*.)+(spec.ts)'],
    rootDir: '../../../',

    //Path generated code
    //To correct SecurityError: localStorage
    testEnvironment: "jsdom",
    testURL: "http://localhost/"
};

包装现在可以正常工作了。 (是否应该在jhipster中提交此配置?)