我目前正在尝试为一个非常大的 Angular 应用程序进行 e2e 测试,但我在设置它们时遇到了麻烦。由于各种情况,我无法尝试将 Angular CLI 集成到项目中,所以我必须解决它。
我的项目结构
app
├── e2e
│ ├── tsconfig.e2e.json
│ ├── component.e2e-spec.ts
│ └── pages
│ └── component.page.ts
├── ....
├── protractor.conf.js
└── tsconfig.json
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"allowJs": true,
"noEmit": true,
"noEmitHelpers": true,
"importHelpers": true,
"strictNullChecks": false,
"lib": [
"dom",
"es6"
]
},
"typeRoots": [
"node_modules/@types"
],
"types": [
"jasmine",
"node",
"source-map",
"uglify-js",
"webpack"
],
"exclude": [
"node_modules",
"!node_modules/@types",
"dist",
"typescript"
],
"awesomeTypescriptLoaderOptions": {
"forkChecker": true,
"useWebpackText": true
},
"compileOnSave": false,
"buildOnSave": false,
"atom": {
"rewriteTsconfig": false
}
}
量角器.conf.ts
const {SpecReporter} = require('jasmine-spec-reporter');
exports.config = {
allScriptsTimeout: 11000,
specs: [
'./e2e/**/*.e2e-spec.ts'
],
capabilities: {
'browserName': 'chrome',
directConnect: true,
baseUrl: 'http://localhost:4200/',
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000,
print: function () {
}
},
onPrepare() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
jasmine.getEnv().addReporter(new SpecReporter({spec: {displayStacktrace: true}}));
}
}
}
tsconfig.e2e.json
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/e2e",
"module": "commonjs",
"target": "es5",
"types": [
"jasmine",
"jasminewd2",
"node"
]
}
}
我可以使用以下方法启动我的服务器:
npm run webpack-dev-server -- --config config/webpack.dev.js --open --progress --profile --watch --content-base src/
当我尝试使用以下命令开始 e2e 测试时:
npm-run-all -p -r server:prod:ci protractor
我收到以下错误:
Error: C:\Users\xxx\Desktop\app\e2e\component.e2e-spec.ts:1
(function (exports, require, module, __filename, __dirname) { import { ComponentPage} from './pages/component.page';
^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
我认为抛出该错误是因为 .ts 文件未转换为 .js ......所以我猜我的配置或我开始测试的方式是错误的。