Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

angular4 - SyntaxError: Unexpected token 'const' for testing.es5.js

I've switched to Angular4.0.0-rc.1 and it looks like ES5 testing bundles contain ES2015 code.

I'm getting this error:

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  SyntaxError: Unexpected token 'const'
    at webpack:///~/@angular/core/@angular/core/testing.es5.js:10:0 <- src/test.ts:16345

Also found related issue on Angular repo.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I just created a basic Angular project and had the same (or similar) issue with PhantomJS integration.

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  SyntaxError: Unexpected token 'const'
  at webpack:///src/app/app.component.ts:1:0 <- src/test.ts:63184

Our team got it to work and here's what we tossed in our documentation:

PhantomJS is a headless (aka, no GUI) browser that can be used for automated testing. Currently, PhantomJS has a limitation in that it only supports the ES5 standard.

  1. npm install -g phantomjs-prebuilt
  2. npm install --save-dev karma-phantomjs-launcher
    • in karma.conf.js
      1. add require('karma-phantomjs-launcher') to the plugins section
      2. add PhantomJS to the browsers
  3. npm install --save intl
    • in src/polyfill.ts
      1. add import 'intl'; (uncomment at the bottom)
      2. add import "core-js/client/shim"; to the Evergreen requirements section
  4. In src/tsconfig.spec.json (or whatever typescript config is used for tests)
    • Set the target to es5.

Assuming all goes well, testing should work.

Some examples on how to trigger a test run:

  • ng test --browsers PhantomJS --singleRun true
  • ./node_modules/karma/bin/karma start ./karma.conf.js --browsers PhantomJS --singleRun true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...