2015-11-26 6 views
5

Voglio correre i miei test su un mucchio di moduli dopo webpack li ha combinati insieme attraverso la prova corridore Karma, ma ogni volta che ho eseguito i miei test Karma diceCome posso ottenere Karma + Webpack per trovare il modulo?

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

Ho un file spec:

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

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

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

}); //end describe 

hello.js è questo:

var a = 'humperdink'; 

module.exports = a; 

Entrambi questi file sono nella stessa cartella.

miei karma.conf.js è:

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 
    } 
    }) 
}; 

Attualmente il mio devDependencies installati sono

"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" 

Come raggiungo Karma per trovare il modulo hello.js?

Ho provato a cambiare la prima linea del file spec a cose come

require('hello.js'); 

o

require('./hello.js'); 

o

require('hello'); 

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

Non penso che ci sia qualcosa di troppo complicato da fare qui, Cannot find module error when using karma-webpack.

Ho controllato per accertarmi che il test runner di Karma funzioni diversamente. Se eseguo un test davvero semplice nel suo file funziona perfettamente.

Come posso risolvere questo problema?

+0

Hai provato 'require ('../ src/hello');'? –

+0

L'ho fatto, prima di spostare entrambi i file nella stessa directory. –

risposta

9

Ho replicato il progetto e l'ho risolto. A seguito del https://github.com/webpack/karma-webpack

Nella specifica:

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

E in aggiunta, per prova NPM comando: in package.json :

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