How can I get Karma + Webpack to find the module?

Gabriel Kunkel

I want to run my tests on a bunch of modules after webpack has combined them together through the Karma test runner, but whenever I run my tests Karma says,

"Error: Cannot find module "hello.js" at http://localhost:9877/base/src/hello.spec.js?d301966ffc1330826574d9d8fff5a644c3390c68:47"

I have a spec file:

var a = require('hello.js');

describe("a test test", function() {

  it("humperdink test", function() {
    expect(a).toEqual('humperdink');
  }); //end it

}); //end describe

hello.js is this:

var a = 'humperdink';

module.exports = a;

Both of these files are in the same folder.

My karma.conf.js is:

module.exports = function (config) {
  config.set({
    frameworks: ['jasmine'],
    files: [
      'src/**/*.js',
      'tests/**/*.spec.js'
    ],
    preprocessors: {
      'tests/**/*.spec.js': ['webpack'],
      'src/**/*.js' : ['webpack']
    },
    browsers: ['PhantomJS'],
    webpack: {
      entry: './src/hello.spec.js',
      output: {
        filename: 'bundle.js'
      }
    },
    webpackMiddleware: {
      noInfo: true
    }
  })
};

Currently my devDependencies installed are

"devDependencies": {
    "jasmine-core": "^2.3.4",
    "jshint": "^2.8.0",
    "karma": "^0.13.15",
    "karma-jasmine": "^0.3.6",
    "karma-jshint-preprocessor": "0.0.6",
    "karma-phantomjs-launcher": "^0.2.1",
    "karma-webpack": "^1.7.0",
    "phantomjs": "^1.9.19",
    "sinon": "^1.17.2",
    "webpack": "^1.12.9"

How do I get Karma to find the hello.js module?

I've tried changing the spec file's first line to things like

require('hello.js');

or

require('./hello.js');

or

require('hello');

on the advice of Karma Webpack - Error: Cannot find module "./test/utilities.js"

I don't think there's anything too complicated going on here like, Cannot find module error when using karma-webpack.

I have checked to make sure that the Karma test runner is working otherwise. If I run a really simple test in its own file it works just fine.

How do I solve this problem?

Aitor Aznar Álvarez

I have replicate your project and fix it. Following the https://github.com/webpack/karma-webpack

In the spec:

var a = require('../src/hello.js');

karma.conf.js:

module.exports = function (config) {
  config.set({
    frameworks: ['jasmine'],
    files: [
      //'src/**/*.js', <-------- Remove or comment this
      'tests/**/*.spec.js'
    ],
    preprocessors: {
      'tests/**/*.spec.js': ['webpack'],
      'src/**/*.js' : ['webpack']
    },
    browsers: ['PhantomJS'],
    webpack: {
      entry: './tests/hello.spec.js',
      output: {
        filename: 'bundle.js'
      }
    },
    webpackMiddleware: {
      noInfo: true
    }
  })
};

karma specs result in terminal

And additionally for npm test command: in package.json:

"scripts": {
    "test": "./node_modules/karma/bin/karma start"
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I injector Angular module in webpack?

From Dev

How can I injector Angular module in webpack?

From Dev

Angular and karma (jasmine) : How can I find an element by class

From Dev

Cannot find module error when using karma-webpack

From Dev

Cannot find module error when using karma-webpack

From Dev

Karma Webpack - Error: Cannot find module "./test/utilities.js"

From Dev

How can I find the function of a python module?

From Dev

ReferenceError: Can't find variable: require. [karma, webpack]

From Dev

karma + jasmine + webpack: module is not a function

From Dev

How can I get the path of a module dynamically?

From Dev

How can I get the path of a module dynamically?

From Dev

Webpack can't find Entry Module

From Dev

Karma is erroring with ReferenceError: Can't find variable: module on the karma config file itself

From Dev

How do I get 'npm test' to find my karma.conf.js file?

From Dev

How can I test a promise that is mocked with Karma?

From Dev

How can I install the module gateways and find the adress of my routeur

From Dev

How can I find out what package that a python module belongs to?

From Dev

How can I find what tab a module is associated with in Dojo?

From Dev

Ruby: how can I find all class method of module

From Dev

Karma can't load webpack

From Dev

How can I get source maps with the webpack less loader?

From Dev

How can I get webpack to emit its output to two locations?

From Dev

How can I get source maps with the webpack less loader?

From Dev

Karma cannot find module 'optimist'

From Dev

Karma cannot find module 'optimist'

From Dev

How can I get the stanford NLTK python module?

From Dev

How can I get a test resource from another module

From Dev

How can I get Gradle module name programmatically

From Dev

How can I send cookie using NodeJs request GET module?

Related Related

  1. 1

    How can I injector Angular module in webpack?

  2. 2

    How can I injector Angular module in webpack?

  3. 3

    Angular and karma (jasmine) : How can I find an element by class

  4. 4

    Cannot find module error when using karma-webpack

  5. 5

    Cannot find module error when using karma-webpack

  6. 6

    Karma Webpack - Error: Cannot find module "./test/utilities.js"

  7. 7

    How can I find the function of a python module?

  8. 8

    ReferenceError: Can't find variable: require. [karma, webpack]

  9. 9

    karma + jasmine + webpack: module is not a function

  10. 10

    How can I get the path of a module dynamically?

  11. 11

    How can I get the path of a module dynamically?

  12. 12

    Webpack can't find Entry Module

  13. 13

    Karma is erroring with ReferenceError: Can't find variable: module on the karma config file itself

  14. 14

    How do I get 'npm test' to find my karma.conf.js file?

  15. 15

    How can I test a promise that is mocked with Karma?

  16. 16

    How can I install the module gateways and find the adress of my routeur

  17. 17

    How can I find out what package that a python module belongs to?

  18. 18

    How can I find what tab a module is associated with in Dojo?

  19. 19

    Ruby: how can I find all class method of module

  20. 20

    Karma can't load webpack

  21. 21

    How can I get source maps with the webpack less loader?

  22. 22

    How can I get webpack to emit its output to two locations?

  23. 23

    How can I get source maps with the webpack less loader?

  24. 24

    Karma cannot find module 'optimist'

  25. 25

    Karma cannot find module 'optimist'

  26. 26

    How can I get the stanford NLTK python module?

  27. 27

    How can I get a test resource from another module

  28. 28

    How can I get Gradle module name programmatically

  29. 29

    How can I send cookie using NodeJs request GET module?

HotTag

Archive