D
D
Dimionus2018-09-04 20:15:20
Unit testing
Dimionus, 2018-09-04 20:15:20

How to test classes in phantomJS using karma/mocha?

There is a class:

class ResourceLoader {
  constructor(url) {
    this.address = url;
  }
  ...
  RawToText(argv) {
    return argv;
  });
  ...
}

export default {
  ResourceLoader,
};

There is a test:
import ResourceLoader from './ResourceLoader';

describe('Helpers::ResourceLoader', () => {
  /* Default setup */
  const domain = 'host.com';
  const instanceResourceLoader = new ResourceLoader(`https://${domain}`);
  ...
  /* Tasks */
  it('Replace links from address', () => {
    expect(instanceResourceLoader.RawToText('test str'))
      .to.equal('test str');
  });
});

Error in console output:
PhantomJS 2.1.1 (Windows 7 0.0.0) ERROR
  {
    "message": "TypeError: undefined is not a constructor (evaluating 'new _ResourceLoader.ResourceLoader('https://' + domain)')\nat webpack:///src/Helpers/ResourceLoader.spec.js:26:2 <- index.js:13550",
    "str": "TypeError: undefined is not a constructor (evaluating 'new _ResourceLoader.ResourceLoader('https://' + domain)')\nat webpack:///src/Helpers/ResourceLoader.spec.js:26:2 <- index.js:13550"
  }

karma.conf.js
// This is a karma config file. For more details see
//   http://karma-runner.github.io/0.13/config/configuration-file.html
// we are also using it with karma-webpack
//   https://github.com/webpack/karma-webpack

const webpackConfig = require('../../../build/webpack.test.conf');

module.exports = function karmaConfig(config) {
  config.set({
    // to run in additional browsers:
    // 1. install corresponding karma launcher
    //    http://karma-runner.github.io/0.13/config/browsers.html
    // 2. add it to the `browsers` array below.
    browsers: ['PhantomJS'],
    frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim'],
    reporters: ['spec', 'coverage'],
    files: [
      '../../../node_modules/babel-polyfill/dist/polyfill.js',
      '../../../node_modules/whatwg-fetch/fetch.js',
      './index.js'
    ],
    preprocessors: {
      './index.js': ['webpack', 'sourcemap'],
    },
    webpack: webpackConfig,
    webpackMiddleware: {
      noInfo: true,
    },
    coverageReporter: {
      dir: '../../../test/coverage',
      reporters: [
        { type: 'lcov', subdir: '.' },
        { type: 'text-summary' },
      ],
    },
  });
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question