Karma / PhantomJS cannot run unittests written in typescript

I am trying to get my unittests to work in phantomJS using Karma and Jasmine. Unittests are written in Typescript, as well as in source files. Tests work fine in Chrome, Safari, and Firefox , but don't work in PhantomJS. Error:

PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
  Error: (SystemJS) eval@[native code]
        tryCatchReject@/poc/node_modules/systemjs/dist/system-polyfills.src.js:1188:34
        runContinuation1@/poc/node_modules/systemjs/dist/system-polyfills.src.js:1147:18
        when@/poc/node_modules/systemjs/dist/system-polyfills.src.js:935:20
        run@/poc/node_modules/systemjs/dist/system-polyfills.src.js:826:17
        _drain@/poc/node_modules/systemjs/dist/system-polyfills.src.js:102:22
        drain@/poc/node_modules/systemjs/dist/system-polyfills.src.js:67:15
        Evaluating /poc/src/components/button/button.js
        Error loading /poc/test/components/button/button.spec.ts

I tried connecting the phantomjs debugger, but it does not cause any errors, so I am a bit stuck here.

Karma configuration:

module.exports = function (config) {
  config.set({
    customLaunchers: {
      'PhantomJS_custom': {
        base: 'PhantomJS',
        options: {
          windowName: 'my-window',
          settings: {
            webSecurityEnabled: false
          }
        },
        flags: ['--load-images=true'],
        debug: true
      }
    },

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '../',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['systemjs', 'jasmine'],

    plugins: [
      'karma-systemjs',
      'karma-jasmine',
      'karma-coverage',
      'karma-remap-istanbul',
      'karma-typescript-preprocessor',
      'karma-safari-launcher',
      'karma-chrome-launcher',
      'karma-firefox-launcher',
      'karma-phantomjs-launcher'
    ],

    preprocessors: {
      'src/**/*.ts': ['typescript', 'coverage']
    },

    // list of files / patterns to load in the browser
    files: [
      'node_modules/phantomjs-polyfill/bind-polyfill.js',
      'test/**/button.spec.ts',
      'testutils/**/*.ts'
    ],

    systemjs: {
      configFile: 'config/system.conf.js',
      config: {
        packages: {
          'src': {
            defaultExtension: 'js'
          },
          'test': {
            defaultExtension: 'ts'
          },
          'testutils': {
            defaultExtension: 'ts'
          }
        },

        transpiler: "typescript"
      },

      serveFiles: [
        'src/**/*.ts',
        'test/**/button.spec.ts',
        'testutils/**/*.ts'
      ]
    },

    // list of files to exclude
    exclude: [],

    coverageReporter: {
      reporters: [
        {
          type: 'json',
          dir: 'coverage',
          subdir: '.',
          file: 'report.json'
        }
      ]
    },

    remapIstanbulReporter: {
      src: 'coverage/report.json',
      reports: {
        lcovonly: 'coverage/lcov.info',
        html: 'coverage/html/report'
      }
    },

    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: [
      'progress',
      'coverage',
      'karma-remap-istanbul'
    ],
    typescriptPreprocessor: {
      // options passed to the typescript compiler
      options: {
        inlineSourceMap: true,
        inlineSources: true,
        "outDir": ".tmp/",
        sourceMap: false,
        noImplicitUseStrict: true,
        target: 'ES5', // (optional) Specify ECMAScript target version: 'ES3' (default), or 'ES5'
        module: 'commonjs', // (optional) Specify module code generation: 'commonjs' or 'amd'
        concatenateOutput: false // (optional) Concatenate and emit output to single file. By default true if module option is omitted, otherwise false.
      }
    },

    // web server port
    port: 9876,

    // enable / disable colors in the output (reporters and logs)
    colors: true,

    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_DEBUG,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['PhantomJS'],//, 'Safari', 'Chrome', 'Firefox'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: false,

    // Concurrency level
    // how many browser should be started simultaneous
    concurrency: Infinity,

    captureTimeout: 60000,
    browserDisconnectTimeout: 20000000000,
    browserDisconnectTolerance: 0,
    browserNoActivityTimeout: 1000000000
  })
};
+4
source share

Source: https://habr.com/ru/post/1648362/


All Articles