2017-06-09 6 views
1

分冊器、タイプスクリプト、ジャスミンをグローバルに、またローカルにインストールしました。 npm installを使用してテストを実行しているときに次のエラーが表示される場合は、設定に問題があればご案内ください。以下は

import {browser, element, by, By, $, $$, ExpectedConditions} from 'protractor'; 
^^^^^^ 
SyntaxError: Unexpected token import 
    at createScript (vm.js:56:10) 
    at Object.runInThisContext (vm.js:97:10) 
    at Module._compile (module.js:542:28) 
    at Object.Module._extensions..js (module.js:579:10) 
    at Module.load (module.js:487:32) 
    at tryModuleLoad (module.js:446:12) 
    at Function.Module._load (module.js:438:3) 
    at Module.require (module.js:497:17) 
    at require (internal/module.js:20:19) 
    at C:\MyFiles\NewTechonologies\Protractor\TypeScript\Test\node_modules\jasmine\lib\jasmine.js:93:5 
[13:14:56] E/launcher - Process exited with error code 100 
npm ERR! Test failed. See above for more details. 

私はVisual Studioのコードで

Conf.ts

import {Config} from 'protractor'; 

export let config: Config = { 

    framework: 'jasmine', 
    capabilities: { 
    browserName: 'chrome' 
    }, 
    specs: [ '../spec.ts' ], 
    onPrepare:() => { 
    let globals = require('protractor'); 
    let browser = globals.browser; 
    browser.manage().window().maximize(); 
    browser.manage().timeouts().implicitlyWait(5000); 
    // doing a browser.get will lead to a transpile error. 
    // undefined does not have a get method 
    }, 
    seleniumAddress: 'http://localhost:4444/wd/hub', 

    // You could set no globals to true to avoid jQuery '$' and protractor '$' 
    // collisions on the global namespace. 
    noGlobals: true 
}; 

spec.ts

import {browser, element, by, By, $, $$, ExpectedConditions} from 'protractor'; 
//import { ElementFinder, browser, by, element } from 'protractor'; 
import protractor = require('protractor'); 

describe('protractor with typescript typings',() => { 
    beforeEach(() => { 
    browser.get('http://www.angularjs.org'); 
    }); 

    it('should greet the named user', async() => { 
    setTimeout(function() { 
    // Whatever you want to do after the wait 
}, 4000); 
    element(by.model('yourName')).sendKeys('Julie'); 
    let greeting = element(by.binding('yourName')); 
    // expect(greeting.getText()).toEqual('Hello Julie!'); 
    expect<any>(greeting.getText()).toEqual('Hello Julie!'); 
    }); 

    it('should list todos', function() { 
    let todoList = element.all(by.repeater('todo in todoList.todos')); 
    expect<any>(todoList.count()).toEqual(2); 
    expect<any>(todoList.get(1).getText()).toEqual('build an angular app'); 
    }); 
}); 

を使用していたファイルの詳細については、

{ 
    "name": "example-typescript", 
    "version": "1.0.0", 
    "description": "a typescript example", 
    "author": "", 
    "license": "MIT", 
    "scripts": { 
    "tsc": "tsc", 
    "pretest": "npm run tsc", 
    "test": "protractor tmp/conf.js" 
    }, 
    "dependencies": { 
    "@types/jasmine": "^2.5.47", 
    "@types/jasminewd2": "^2.0.0", 
    "jasmine": "^2.4.1", 
    "protractor": "file:../", 
    "typescript": "~2.1.6" 
    }, 
    "devDependencies": { 
    "@types/jasmine": "^2.5.51", 
    "@types/jasminewd2": "^2.0.2", 
    "ts-node": "^3.0.2" 
    } 
+0

をうまく働いたあなたの 'tsconfig.json'の内容は何ですか? – Nemikolh

答えて

0

package.json使用すると、1つを作成し、tsconfig.jsonを持っていない場合。

npm run tsc -- --init 

編集しmoduletargetプロパティを、彼らはそれぞれcommonjses5あることを確認してください。基本的なものを取得するには、ご使用の構成を考えると、あなたが行うことができます。 ES6モジュールがまだノードによってサポートされていないという事実に起因するエラーです。したがって、importトークンを解析するときにノードに障害が発生しました。コメントの議論ごとに、私はをspec.jsする../spec.tsからconf.tsコンテンツを変えてきたように

{ 
    "compilerOptions": { 
     "module": "commonjs", 
     "target": "es5", 
     "moduleResolution": "node", 
     "sourceMap": true, 
     "lib": [ 
      "dom", 
      "es2015", 
     ] 
    }, 
    "exclude": [ 
     "node_modules" 
    ] 
} 
+0

"target": "es5"を試しましたが、エラーが発生しました。 node_modules/blocking-proxy/built/lib/angular_wait_barrier.d.ts(43,43):エラーTS2304: 'Promise'という名前が見つかりません。 "es6"では少なくともブラウザを開くことができましたが、テストは実行されません – sunpat

+0

これは普通のことですが、おそらく 'lib'フラグにはいくつか欠けている可能性があります。典型的な値は '[" dom "、" es2015 "]'です。可能な値の詳細については、こちらをご覧ください。[こちら](https://github.com/Microsoft/TypeScript-Handbook/blob/master/pages/Compiler%20Options.md) – Nemikolh

+0

私の回答を更新しました。未解決の "Promise"エラーを解決するtsconfigの例を示します。 – Nemikolh

0

:ここ

はあなたのために動作するはずtsconfig.jsonの一例ですそして

conf.ts

import {Config, browser} from 'protractor'; 
export let config: Config = { 

    framework: 'jasmine', 
    capabilities: { 
    browserName: 'chrome', 
    chromeOptions: { 
      'args': ['disable-infobars'] 
     } 
    }, 

    specs: [ 'spec.js' ], 
    onPrepare:() => { 
    let globals = require('protractor'); 
    let browser = globals.browser; 
    browser.ignoreSynchronization = true; 
    browser.manage().window().maximize(); 
    browser.manage().timeouts().implicitlyWait(5000); 
    }, 
    seleniumAddress: 'http://localhost:4444/wd/hub', 
    noGlobals: true 
}; 
関連する問題