Skip to content

Instantly share code, notes, and snippets.

@perry-mitchell
Created December 6, 2016 19:01
Show Gist options
  • Save perry-mitchell/f41b01c87f25f483ecb3c2c7c2fcc74d to your computer and use it in GitHub Desktop.
Save perry-mitchell/f41b01c87f25f483ecb3c2c7c2fcc74d to your computer and use it in GitHub Desktop.
Webpack2 + inject-loader2.0.1 (blog post)
{
"env": {
"testing": {
"presets": [
"es2015"
]
}
},
"presets": [
["es2015", { "modules": false }]
]
}
import { getSpecialValue } from "filea";
export function getValue() {
return getSpecialValue() * 2;
}
describe("entry", function() {
beforeEach(function() {
let fileInjector = require("inject-loader!entry");
this.filea = {
getSpecialValue: function() {
return 1;
}
};
this.entry = fileInjector({
"filea": this.filea
});
sinon.spy(this.filea, "getSpecialValue");
});
it("works without override", function() {
expect(require("entry.js").getValue()).to.equal(20);
});
it("overrides", function() {
expect(this.entry.getValue()).to.equal(2);
});
it("gets a special value from filea", function() {
this.entry.getValue();
expect(this.filea.getSpecialValue.calledOnce).to.be.true;
});
});
export function getSpecialValue() {
return 10;
}
import { getSpecialValue } from "filea";
describe("filea", function() {
describe("getSpecialValue", function() {
it("returns a special value", function() {
expect(getSpecialValue()).to.equal(10);
});
});
});
module.exports = function(config) {
config.set({
// 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: ['mocha', 'chai', 'sinon'],
// list of files / patterns to load in the browser
files: [
'./*.spec.js'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
"./*.spec.js": ["webpack"]
},
// webpack configuration
webpack: require("./webpack.config.js"),
// middleware for webpack
webpackMiddleware: {
stats: "errors-only"
},
reporters: ['progress'],
// 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_INFO,
// 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: ['ChromeWithoutSecurity'],
customLaunchers: {
ChromeWithoutSecurity: {
base: 'Chrome',
flags: ['--disable-web-security']
}
},
// 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
})
}
{
"name": "my-app",
"version": "1.0.0",
"main": "entry.js",
"scripts": {
"build": "webpack --progress",
"test": "NODE_ENV=testing karma start --single-run",
"test:watch": "NODE_ENV=testing karma start"
},
"dependencies": {
"babel-core": "^6.18.2",
"babel-loader": "^6.2.8",
"babel-preset-es2015": "^6.18.0",
"inject-loader": "2.0.1",
"webpack": "2.1.0-beta.27"
},
"devDependencies": {
"chai": "^3.5.0",
"karma": "^1.3.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.0.0",
"karma-mocha": "^1.3.0",
"karma-sinon": "^1.0.5",
"karma-webpack": "^1.8.0",
"mocha": "^3.2.0",
"sinon": "^1.17.6"
}
}
const path = require("path");
const webpack = require("webpack");
module.exports = {
entry: {
script: path.resolve(__dirname, "./entry.js")
},
module: {
rules: [
{
test: /\.js$/,
use: "babel-loader",
exclude: /(\/node_modules\/|test\.js|\.spec\.js$)/
}
]
},
output: {
path: "./dist",
filename: "script.js",
pathinfo: true
},
resolve: {
extensions: [".js"],
modules: [
__dirname,
path.resolve(__dirname, "./node_modules")
]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment