【问题标题】:JHipster new application test fail on ReferenceError: XMLHttpRequestJHipster 新应用程序测试在 ReferenceError: XMLHttpRequest 上失败
【发布时间】:2018-08-09 03:19:40
【问题描述】:

我创建了一个新的应用程序 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 但开玩笑的结果没有变化。

这个问题并不紧急,因为我可以禁用测试,但我不能让它太久。

【问题讨论】:

  • 您将环境设置为节点,但 XMLHttpRequest 不是节点的一部分。如果您正在编写浏览器代码,请对其进行测试。
  • 将其更改为testEnvironment: 'jsdom'。添加testURL: 'http://localhost' 应该足以修复第一个错误。这都与bug in Jest 发现的minor release update of jsdom 有关。

标签: jhipster


【解决方案1】:

感谢@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 中提交吗?)

【讨论】:

    猜你喜欢
    • 2021-02-09
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 2017-08-31
    • 2021-12-17
    • 2019-01-09
    • 2018-05-05
    • 1970-01-01
    相关资源
    最近更新 更多