angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Import statements not supported in karma.conf.ts

Open rudym87 opened this issue 8 years ago • 2 comments

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Command (mark with an x)

- [ ] new
- [ ] build
- [ ] serve
- [x] test
- [ ] e2e
- [ ] generate
- [ ] add
- [ ] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Versions

Node v10.10.0

Yarn 1.9.4

Angular CLI: 6.2.2
Node: 10.10.0
OS: win32 x64
Angular: 6.1.7
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.8.2
@angular-devkit/build-angular     0.8.2
@angular-devkit/build-optimizer   0.8.2
@angular-devkit/build-webpack     0.8.2
@angular-devkit/core              0.8.2
@angular-devkit/schematics        0.8.2
@angular/cli                      6.2.2
@ngtools/webpack                  6.2.2
@schematics/angular               0.8.2
@schematics/update                0.8.2
rxjs                              6.2.2
typescript                        2.9.2
webpack                           4.19.0

Windows 10

Repro steps

  1. Generate a new app with ng new [appname].
  2. Rename src/karma.conf.js to src/karma.conf.ts.
  3. In this file, replace inline require statement with import statements:
[...]
plugins: [
    require('karma-jasmine'),
[...]

should become

import * as karmaJasmine from 'karma-jasmine';
[...]
plugins: [
    karmaJasmine ,
[...]
  1. Reflect the rename by editing angular.json at projects.[appname].targets.test.karmaConfig from src/karma.conf.js to src/karma.conf.ts.
  2. Run ng test

The log given by the failure

ERROR [config]: Invalid config file!
  [appPath]\src\karma.conf.ts:1
(function (exports, require, module, __filename, __dirname) { import * as karmaJasmine from 'karma-jasmine';
                                                                     ^

SyntaxError: Unexpected token *
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Module.m._compile ([appPath]\node_modules\ts-node\src\index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] ([appPath]\node_modules\ts-node\src\index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.parseConfig ([appPath]\node_modules\karma\lib\config.js:375:22)
    at new Server ([appPath]\node_modules\karma\lib\server.js:63:24)
    at Observable.rxjs_1.Observable.obs [as _subscribe] ([appPath]\node_modules\@angular-devkit\build-angular\src\karma\packages\angular_devkit\build_angular\src\karma\index.ts:95:29)
    at Observable._trySubscribe ([appPath]\node_modules\rxjs\src\internal\Observable.ts:223:19)

Desired functionality

Tests should run normally.

Mention any other details that might be useful

Run into this issue by upgrading an Angular v5 app to v6 which already had a working TypeScript Karma config.

rudym87 avatar Sep 18 '18 08:09 rudym87

The problem is that the root tsconfig the module is set to "es2015", ts-node uses this ts-config to transpile the ts configuration.

Please see https://github.com/karma-runner/karma/pull/3139

alan-agius4 avatar Sep 18 '18 10:09 alan-agius4

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.