From 1c2de1e483210f93ebe8163a36fd1468403b725d Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 2 Feb 2017 23:31:22 +0000 Subject: [PATCH] Refactor karma config - rename test:multi npm target to test-multi, for consistency with react-sdk - base karma webpack config on the distribution one - include Olm if we have it - don't use the karma source loader - it's pointless given we webpack everything. - turn off module listing in the webpack stats to shorten the console output --- karma.conf.js | 105 ++++++++++++++++++++++++-------------------------- package.json | 3 +- 2 files changed, 51 insertions(+), 57 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index 901832c78c..b0a48c92ba 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -2,13 +2,14 @@ var path = require('path'); var webpack = require('webpack'); +var webpack_config = require('./webpack.config'); /* * We use webpack to build our tests. It's a pain to have to wait for webpack * to build everything; however it's the easiest way to load our dependencies * from node_modules. * - * If you run karma in multi-run mode (with `npm run test:multi`), it will watch + * If you run karma in multi-run mode (with `npm run test-multi`), it will watch * the tests for changes, and webpack will rebuild using a cache. This is much quicker * than a clean rebuild. */ @@ -19,8 +20,41 @@ var testFile = process.env.KARMA_TEST_FILE || 'test/all-tests.js'; process.env.PHANTOMJS_BIN = 'node_modules/.bin/phantomjs'; process.env.Q_DEBUG = 1; +/* the webpack config is based on the real one, to (a) try to simulate the + * deployed environment as closely as possible, and (b) to avoid a shedload of + * cut-and-paste. + */ + +// find out if we're shipping olm, and where it is, if so. +const olm_entry = webpack_config.entry['olm']; + +// remove the default entries - karma provides its own (via the 'files' and +// 'preprocessors' config below) +delete webpack_config['entry']; + +// add ./test as a search path for js +webpack_config.module.loaders.unshift({ + test: /\.js$/, loader: "babel", + include: [path.resolve('./src'), path.resolve('./test')], +}); + +// disable parsing for sinon, because it +// tries to do voodoo with 'require' which upsets +// webpack (https://github.com/webpack/webpack/issues/304) +webpack_config.module.noParse.push(/sinon\/pkg\/sinon\.js$/); + +// ? +webpack_config.resolve.alias['sinon'] = 'sinon/pkg/sinon.js'; + +webpack_config.resolve.root = [ + path.resolve('./src'), + path.resolve('./test'), +]; + +webpack_config.devtool = 'inline-source-map'; + module.exports = function (config) { - config.set({ + const myconfig = { // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter frameworks: ['mocha'], @@ -51,7 +85,7 @@ module.exports = function (config) { // available preprocessors: // https://npmjs.org/browse/keyword/karma-preprocessor preprocessors: { - 'test/**/*.js': ['webpack', 'sourcemap'] + '{src,test}/**/*.js': ['webpack'], }, // test results reporter to use @@ -94,59 +128,20 @@ module.exports = function (config) { outputDir: 'karma-reports', }, - webpack: { - module: { - preLoaders: [ - // use the source-map-loader for javascript. This means - // that we have a better chance of seeing line numbers from - // the pre-babeled source. - { test: /\.js$/, loader: "source-map-loader" }, - ], - loaders: [ - { test: /\.json$/, loader: "json" }, - { - test: /\.js$/, loader: "babel", - include: [path.resolve('./src'), - path.resolve('./test'), - ] - }, - ], - noParse: [ - // don't parse the languages within highlight.js. They - // cause stack overflows - // (https://github.com/webpack/webpack/issues/1721), and - // there is no need for webpack to parse them - they can - // just be included as-is. - /highlight\.js\/lib\/languages/, + webpack: webpack_config, - // also disable parsing for sinon, because it - // tries to do voodoo with 'require' which upsets - // webpack (https://github.com/webpack/webpack/issues/304) - /sinon\/pkg\/sinon\.js$/, - ], + webpackMiddleware: { + stats: { + // don't fill the console up with a mahoosive list of modules + chunks: false, }, - resolve: { - alias: { - // alias any requires to the react module to the one in our path, otherwise - // we tend to get the react source included twice when using npm link. - react: path.resolve('./node_modules/react'), - - // same goes for js-sdk - "matrix-js-sdk": path.resolve('./node_modules/matrix-js-sdk'), - - sinon: 'sinon/pkg/sinon.js', - }, - root: [ - path.resolve('./src'), - path.resolve('./test'), - ], - }, - plugins: [ - // olm may not be installed, so avoid webpack warnings by - // ignoring it. - new webpack.IgnorePlugin(/^olm$/), - ], - devtool: 'inline-source-map', }, - }); + }; + + // include the olm loader if we have it. + if (olm_entry) { + myconfig.files.unshift(olm_entry); + } + + config.set(myconfig); }; diff --git a/package.json b/package.json index 915056e636..d48eaffe62 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "clean": "rimraf lib webapp electron/dist", "prepublish": "npm run build:compile", "test": "karma start --single-run=true --autoWatch=false --browsers PhantomJS --colors=false", - "test:multi": "karma start" + "test-multi": "karma start" }, "dependencies": { "babel-polyfill": "^6.5.0", @@ -111,7 +111,6 @@ "karma-junit-reporter": "^0.4.1", "karma-mocha": "^0.2.2", "karma-phantomjs-launcher": "^1.0.0", - "karma-sourcemap-loader": "^0.3.7", "karma-webpack": "^1.7.0", "minimist": "^1.2.0", "mkdirp": "^0.5.1",