升级到Angular 6后,我在项目中遇到e2e测试问题。以前的测试工作正常,现在存在一个问题,即.ts文件未编译:错误:TSError:⨯无法来编译TypeScript 。迁移到angular.json文件后,我没有任何变化。我试图找到配置中的确切属性负责进行翻译,但是找不到任何具体答案。当时我以为是
beforeLaunch: function() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
},
有人知道什么可能是错的吗?
v8.11.4
5.4.0
6.1.6
6.1.5
Chrome
Windows 10.0.17134
protractor.conf.js
const { SpecReporter } = require('jasmine-spec-reporter');
const jasmineReporters = require('jasmine-reporters');
const protractorImageComparison = require('protractor-image-comparison');
require('ts-node/register');
require('tsconfig-paths/register');
const { LoginWindow } = require('./e2e/loginWindow.po.js');
exports.config = {
allScriptsTimeout: 30000,
specs: [
'./e2e/**/*.e2e-spec.ts'
],
capabilities: {
'browserName': 'chrome',
chromeOptions: {
args: [ "--no-sandbox", "--disable-gpu", "--window-size=1920x1080" ]
}
},
directConnect: true,
baseUrl: 'http://localhost:4300/',
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
print: function() {}
},
beforeLaunch: function() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
},
onPrepare() {
jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } }));
jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
consolidateAll: true,
savePath: './',
filePrefix: '...'
}));
browser.protractorImageComparison = new protractorImageComparison(
{
actualFolder: '...',
baselineFolder: '...',
diffFolder: '...',
screenshotPath: '...',
tempFullScreenFolder: '...',
autoSaveBaseline: true,
ignoreAntialiasing: true,
ignoreTransparentPixel: true,
}
);
loginPage = new LoginWindow();
browser.waitForAngularEnabled(false).then(() => {
return browser.get('...').then(() => {
loginPage.loginWindowInternal().click().then(() => {
browser.sleep(5000);
}).then(() => {
loginPage.loginWindowUserName().isPresent().then((result) => {
if (result) {
...
browser.sleep(5000);
}
});
});
});
});
}
};
tsconfig.e2e.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/e2e",
"module": "commonjs",
"target": "es5",
"types":[
"jasmine",
"node"
]
}
}
tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"baseUrl": "src",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2016",
"dom"
],
"paths": {
...
},
"module": "es2015"
}
}
angular.json
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
...
"...-e2e": {
"root": "e2e",
"sourceRoot": "e2e",
"projectType": "application",
"architect": {
"e2e": {
"builder": "@angular-devkit/build-angular:protractor",
"options": {
"protractorConfig": "./protractor.conf.js"
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"e2e/tsconfig.e2e.json"
],
"exclude": []
}
}
}
}
},
"defaultProject": "...",
"schematics": {
"@schematics/angular:component": {
"prefix": "app",
"styleext": "scss"
},
"@schematics/angular:directive": {
"prefix": "app"
}
}
}
运行测试的输出
[11:44:42] E/launcher - ### Error: TSError: ⨯ Unable to compile TypeScript:
e2e/....e2e-spec.ts(27,111): error TS2322: Type '0' is not assignable to type '{ label: number; sent: () => any; trys: any[]; ops: any[]; }'.
at createTSError (...\node_modules\ts-node\src\index.ts:261:12)
at getOutput (...\node_modules\ts-node\src\index.ts:367:40)
at Object.compile (...\node_modules\ts-node\src\index.ts:558:11)
at Module.m._compile (...\node_modules\ts-node\src\index.ts:439:43)
at Module.m._compile (...\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (module.js:663:10)
at require.extensions.(anonymous function) (...\node_modules\ts-node\src\index.ts:442:12)
at Object.require.extensions.(anonymous function) [as .ts] (...\node_modules\ts-node\src\index.ts:442:12)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
[11:44:42] E/launcher - Process exited with error code 100
An unexpected error occurred: undefined
注意: 我很确定这是不补偿打字稿文件的问题,因为当我将LoginWindow文件更改为打字稿类并将其导入protractor.conf.js
时,const { LoginWindow } = require('./e2e/loginWindow.po');
我遇到一个错误:
[11:16:01] E/configParser - ...\e2e\loginWindow.po.ts:1
(function (exports, require, module, __filename, __dirname) { import { browser, element, by } from 'protractor';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Module.m._compile (...\node_modules\ts-node\src\index.ts:439:23)
at Module._extensions..js (module.js:663:10)
at Object.require.extensions.(anonymous function) [as .ts] (...\node_modules\ts-node\src\index.ts:442:12)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
An unexpected error occurred: undefined
答案 0 :(得分:3)
我自己找到了解决方案。对不起,@ all之前开始了这个悬赏,我再也无法阻止它了。
我发现我的 protractor.conf.js 缺少以下2个条目:
// put this directly after 'allScriptsTimeout: xxxxx'
// the given path starts inside your e2e-folder!
specs: [
'./test-files/*.e2e.ts'
],
// and then extend the onPrepare-function
onPrepare: function() {
require('ts-node').register({
project: './e2e/tsconfig.e2e.json'
});
...
}
此外,我还必须在 tsconfig.e2e.ts 中切换 include 和 compilerOptions 的位置。如果 compilerOptions 在 include 之前出现,您会遇到错误。
{
"extends": "../tsconfig.json",
"include": [
"**/*.e2e.ts"
],
"compilerOptions": {
...
}
答案 1 :(得分:1)
我想,如果conf文件中具有以下选项,则无需包含在tsconfig.e2e.ts中
onPrepare() {
require('ts-node').register({
project: require('path').join(__dirname, './tsconfig.e2e.json')
});
...
...
}
在上面的代码中,您实际上包含的是“ tsconfig.e2e.json”文件,那么我觉得您不必担心顺序。