├── .gitignore ├── LICENSE ├── README.md ├── karma.conf.js ├── package.json ├── src └── Foo.js └── test ├── .setup.js └── Foo-test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | node_modules 28 | 29 | # Optional npm cache directory 30 | .npm 31 | 32 | # Optional REPL history 33 | .node_repl_history 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Leland Richardson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # enzyme-example-karma-webpack 2 | Example project with React + Enzyme + Karma + Webpack 3 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | 3 | module.exports = function(config) { 4 | config.set({ 5 | basePath: '', 6 | frameworks: ['jasmine'], 7 | files: [ 8 | 'test/**/*.js' 9 | ], 10 | 11 | preprocessors: { 12 | // add webpack as preprocessor 13 | 'src/**/*.js': ['webpack', 'sourcemap'], 14 | 'test/**/*.js': ['webpack', 'sourcemap'] 15 | }, 16 | 17 | webpack: { //kind of a copy of your webpack config 18 | devtool: 'inline-source-map', //just do inline source maps instead of the default 19 | module: { 20 | loaders: [ 21 | { 22 | test: /\.js$/, 23 | loader: 'babel', 24 | exclude: path.resolve(__dirname, 'node_modules'), 25 | query: { 26 | presets: ['airbnb'] 27 | } 28 | }, 29 | { 30 | test: /\.json$/, 31 | loader: 'json', 32 | }, 33 | ] 34 | }, 35 | externals: { 36 | 'react/lib/ExecutionEnvironment': true, 37 | 'react/lib/ReactContext': true 38 | } 39 | }, 40 | 41 | webpackServer: { 42 | noInfo: true //please don't spam the console when running in karma! 43 | }, 44 | 45 | plugins: [ 46 | 'karma-webpack', 47 | 'karma-jasmine', 48 | 'karma-sourcemap-loader', 49 | 'karma-chrome-launcher', 50 | 'karma-phantomjs-launcher' 51 | ], 52 | 53 | 54 | babelPreprocessor: { 55 | options: { 56 | presets: ['airbnb'] 57 | } 58 | }, 59 | reporters: ['progress'], 60 | port: 9876, 61 | colors: true, 62 | logLevel: config.LOG_INFO, 63 | autoWatch: true, 64 | browsers: ['Chrome'], 65 | singleRun: false, 66 | }) 67 | }; 68 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "enzyme-example-karma-webpack", 3 | "version": "0.1.4", 4 | "description": "", 5 | "main": "build/index.js", 6 | "dependencies": { 7 | "react": "^0.14.6", 8 | "react-addons-test-utils": "^0.14.6", 9 | "react-dom": "^0.14.6" 10 | }, 11 | "devDependencies": { 12 | "babel": "^6.3.26", 13 | "babel-core": "^6.4.5", 14 | "babel-loader": "^6.2.1", 15 | "babel-preset-airbnb": "^1.0.1", 16 | "babelify": "^7.2.0", 17 | "browserify": "^13.0.0", 18 | "chai": "^3.5.0", 19 | "enzyme": "^2.0.0", 20 | "jasmine-core": "^2.4.1", 21 | "json-loader": "^0.5.4", 22 | "karma": "^0.13.19", 23 | "karma-babel-preprocessor": "^6.0.1", 24 | "karma-browserify": "^5.0.0", 25 | "karma-chrome-launcher": "^0.2.2", 26 | "karma-jasmine": "^0.3.7", 27 | "karma-mocha": "^0.2.1", 28 | "karma-phantomjs-launcher": "^1.0.0", 29 | "karma-sourcemap-loader": "^0.3.7", 30 | "karma-webpack": "^1.7.0", 31 | "mocha": "^2.4.5", 32 | "phantomjs-prebuilt": "^2.1.4", 33 | "watchify": "^3.7.0", 34 | "webpack": "^1.12.12" 35 | }, 36 | "scripts": { 37 | "test": "./node_modules/karma/bin/karma start --single-run --browsers PhantomJS", 38 | "test:watch": "./node_modules/karma/bin/karma start" 39 | }, 40 | "repository": { 41 | "type": "git", 42 | "url": "git+https://github.com/lelandrichardson/enzyme-example-karma-webpack.git" 43 | }, 44 | "author": "Leland Richardson ", 45 | "license": "MIT", 46 | "bugs": { 47 | "url": "https://github.com/lelandrichardson/enzyme-example-karma-webpack/issues" 48 | }, 49 | "homepage": "https://github.com/lelandrichardson/enzyme-example-karma-webpack#readme" 50 | } 51 | -------------------------------------------------------------------------------- /src/Foo.js: -------------------------------------------------------------------------------- 1 | import React, { PropTypes } from 'react'; 2 | 3 | const propTypes = {}; 4 | 5 | const defaultProps = {}; 6 | 7 | class Foo extends React.Component { 8 | constructor(props) { 9 | super(props); 10 | } 11 | 12 | render() { 13 | return ( 14 |
15 | ); 16 | } 17 | } 18 | 19 | Foo.propTypes = propTypes; 20 | Foo.defaultProps = defaultProps; 21 | 22 | export default Foo; 23 | -------------------------------------------------------------------------------- /test/.setup.js: -------------------------------------------------------------------------------- 1 | require('babel-register')(); 2 | 3 | var jsdom = require('jsdom').jsdom; 4 | 5 | var exposedProperties = ['window', 'navigator', 'document']; 6 | 7 | global.document = jsdom(''); 8 | global.window = document.defaultView; 9 | Object.keys(document.defaultView).forEach((property) => { 10 | if (typeof global[property] === 'undefined') { 11 | exposedProperties.push(property); 12 | global[property] = document.defaultView[property]; 13 | } 14 | }); 15 | 16 | global.navigator = { 17 | userAgent: 'node.js' 18 | }; 19 | 20 | documentRef = document; 21 | -------------------------------------------------------------------------------- /test/Foo-test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { shallow, mount, render } from 'enzyme'; 3 | import Foo from '../src/Foo'; 4 | 5 | describe("A suite", function() { 6 | it("contains spec with an expectation", function() { 7 | expect(shallow().contains(
)).toBe(true); 8 | }); 9 | 10 | it("contains spec with an expectation", function() { 11 | expect(shallow().is('.foo')).toBe(true); 12 | }); 13 | 14 | it("contains spec with an expectation", function() { 15 | expect(mount().find('.foo').length).toBe(1); 16 | }); 17 | 18 | it("can run an expectation with render", function() { 19 | expect(render().find('.foo').length).toBe(1); 20 | }); 21 | }); 22 | --------------------------------------------------------------------------------