尝试运行量角器测试时 - SyntaxError:发生意外的令牌导入

时间:2018-02-05 11:51:29

标签: node.js jasmine protractor

当我尝试通过Protractor运行Jasmine测试时,它给我一个错误:

Debugger listening on ws://127.0.0.1:51610/4a264fb5-0c3d-4952-b511-309442659f88
For help see https://nodejs.org/en/docs/inspector
(node:6652) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
[13:41:16] I/launcher - Running 1 instances of WebDriver
[13:41:16] I/hosted - Using the selenium server at http://localhost:4444/wd/hub
[13:41:19] E/launcher - Error: F:\Testing\e2e\angularTest1.e2e-spec.ts:1
(function (exports, require, module, __filename, __dirname) { import {browser, by, element} from "protractor";
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at new Script (vm.js:51:7)
    at createScript (vm.js:138:10)
    at Object.runInThisContext (vm.js:199:10)
    at Module._compile (module.js:624:28)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
[13:41:19] E/launcher - Process exited with error code 100
Debugger attached.
Waiting for the debugger to disconnect...

Process finished with exit code 4
Empty test suite.

angularTest1.e2e-spec.ts:

import {browser, by, element} from "protractor";

describe('Protractor Demo App', function() {
  var firstNumber = element(by.model('first'));
  var secondNumber = element(by.model('second'));
  var goButton = element(by.id('gobutton'));
  var latestResult = element(by.binding('latest'));
  var history = element.all(by.repeater('result in memory'));

  function add(a, b) {
    firstNumber.sendKeys(a);
    secondNumber.sendKeys(b);
    goButton.click();
  }

  beforeEach(function() {
    browser.get('http://juliemr.github.io/protractor-demo/');
  });

  it('should have a history', function() {
    add(1, 2);
    add(3, 4);

    expect(history.last().getText()).toContain('1 + 2');
    expect(history.first().getText()).toContain('foo'); // This is wrong!
  });
});

protractor.conf.js:

exports.config = {
  allScriptsTimeout: 11000,
  specs: [
    './e2e/**/angularTest1.e2e-spec.ts'
  ],
  capabilities: {
    'browserName': 'chrome'
  },
  directConnect: false,
  baseUrl: 'https://angular.io/',
  seleniumAddress: 'http://localhost:4444/wd/hub',
  framework: 'jasmine',
  jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000,
    print: function() {}
  }
};

尝试从8.9.1更新节点 - > 8.9.4并得到同样的错误。 在8.9.4之后的更新节点之后 - > 9.5.0仍然相同。 寻找其他类似的主题,但即使在重新安装项目之后也没有发生任何事情。

1 个答案:

答案 0 :(得分:0)

Nodejs无法直接执行typescript,必须通过在下面执行更多项目来将typecript转换为javascript。

1)在软件包下面安装更多:

@types/node
@types/selenium-webdriver
ts-node
typescript

2)在项目文件夹中创建tsconfig.json,并在其中添加以下内容:

{
  "compileOnSave": false,
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "typeScript",
    "sourceMap": true,
    "allowJs": true,
    "target": "es6",
    "typeRoots": [
      "node_modules/@types"
    ],
    "types": ["node"]
  },
  "exclude": [
    "node_modules",
    "typescript"
  ]
}