【问题标题】:When trying to run protractor test - SyntaxError: Unexpected token import occurs尝试运行量角器测试时 - SyntaxError:发生意外的令牌导入
【发布时间】:2018-02-05 21:40:25
【问题描述】:

当我尝试通过 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 更新节点之后,仍然相同。 寻找其他类似的主题,但没有发生任何事情,即使在项目重新安装之后。

【问题讨论】:

  • 你需要一个支持import / export的转译器(看babel)。 nodejs 目前不支持。

标签: node.js jasmine protractor


【解决方案1】:

Nodejs 不能直接执行 typescript,必须通过在下面做更多的项目来将 typescript 转换为 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"
  ]
}

【讨论】:

  • @young:我已经尝试过您的解决方案。但同样的错误也存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-11
  • 2016-05-04
  • 2018-03-15
  • 2014-07-28
  • 2017-06-03
  • 2019-07-05
  • 1970-01-01
相关资源
最近更新 更多