Skip to content

Commit

Permalink
The usage sample added to the module.
Browse files Browse the repository at this point in the history
  • Loading branch information
devrafalko committed Aug 2, 2018
0 parents commit c17b78d
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
sample/node_modules
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
sample
.gitignore
46 changes: 46 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "webpack-karma-jasmine",
"version": "2.0.1",
"description": "Configuration for karma jasmine",
"keywords": [
"webpack",
"babel",
"karma",
"jasmine",
"unit",
"test",
"config"
],
"author": "Pawel Rafalko <dev.rafalko@gmail.com>",
"license": "MIT",
"dependencies": {
"jasmine-core": "^3.1.0",
"karma": "^2.0.5",
"karma-chrome-launcher": "^2.2.0",
"karma-edge-launcher": "^0.4.2",
"karma-firefox-launcher": "^1.1.0",
"karma-ie-launcher": "^1.0.0",
"karma-jasmine": "^1.1.2",
"karma-jasmine-html-reporter": "^1.2.0",
"karma-mocha-reporter": "^2.2.5",
"karma-opera-launcher": "^1.0.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-safari-launcher": "^1.0.0",
"karma-spec-reporter": "0.0.32",
"karma-webpack": "^3.0.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0"
},
"scripts": {
"postinstall": "npm install karma-cli -g"
},
"repository": {
"type": "git",
"url": "git+https://github.com/devrafalko/webpack-karma-jasmine.git"
},
"bugs": {
"url": "https://github.com/devrafalko/webpack-karma-jasmine/issues"
},
"homepage": "https://github.com/devrafalko/webpack-karma-jasmine#readme"
}
207 changes: 207 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
### Installation

This boilerplate package will let you use `karma` in your projects.
Use `npm install webpack-karma-jasmine` instead of installing all dependencies and loaders separately.

> If you want to test your `.html` files with **DOM** see [karma-html](https://www.npmjs.com/package/karma-html) package.
This package is the suggestion of karma configuretion. It uses:
* `karma-webpack` and `babel-loader` to compile ES2015 javascript features and bundle **specs** and **tests** `.js` files together
* `karma-jasmine` *(to use another framework, install it manually)*
* `karma-mocha-reporter` bash reporter
* `karma-jasmine-html-reporter` browser reporter

### Sample
`git clone https://github.com/devrafalko/webpack-karma-jasmine.git`
`cd webpack-karma-jasmine/sample`
`npm install`
`npm test`

### Configuration
This is the full description of the sample above.
#### 1. create `karma.conf.js` file
The suggestion of `karma.conf.js` configuration

```javascript
module.exports = function(config) {
config.set({
//root path location to resolve paths defined in files and exclude
basePath: '',
//files/patterns to exclude from loaded files
exclude: [],
//files/patterns to load in the browser
files: [
{pattern: 'src/*.js', watched:true, served:false, included:false, nocache:false},
{pattern: 'tests/*.js',watched:true,served:true,included:true}
/*parameters*/
//watched: if autoWatch is true all files that have set watched to true will be watched for changes
//served: should the files be served by Karma's webserver?
//included: should the files be included in the browser using <script> tag?
//nocache: should the files be served from disk on each request by Karma's webserver?
/*assets*/
//{pattern: '*.html', watched:true, served:true, included:false}
//{pattern: 'images/*', watched:false, served:true, included:false}
],

//executes the tests whenever one of watched files changes
autoWatch: true,
//if true, Karma will run tests and then exit browser
singleRun:false,
//if true, Karma fails on running empty test-suites
failOnEmptyTestSuite:false,
//reduce the kind of information passed to the bash
logLevel: config.LOG_WARN, //config.LOG_DISABLE, config.LOG_ERROR, config.LOG_INFO, config.LOG_DEBUG

//list of frameworks you want to use, only jasmine is installed automatically
frameworks: ['jasmine'],
//list of browsers to launch and capture
browsers: ['Chrome'/*,'PhantomJS','Firefox','Edge','ChromeCanary','Opera','IE','Safari'*/],
//list of reporters to use
reporters: ['mocha','kjhtml'/*,'dots','progress','spec'*/],

//address that the server will listen on, '0.0.0.0' is default
listenAddress: '0.0.0.0',
//hostname to be used when capturing browsers, 'localhost' is default
hostname: 'localhost',
//the port where the web server will be listening, 9876 is default
port: 9876,
//when a browser crashes, karma will try to relaunch, 2 is default
retryLimit:0,
//how long does Karma wait for a browser to reconnect, 2000 is default
browserDisconnectTimeout: 5000,
//how long will Karma wait for a message from a browser before disconnecting from it, 10000 is default
browserNoActivityTimeout: 10000,
//timeout for capturing a browser, 60000 is default
captureTimeout: 60000,

client: {
//capture all console output and pipe it to the terminal, true is default
captureConsole:false,
//if true, Karma clears the context window upon the completion of running the tests, true is default
clearContext:false,
//run the tests on the same window as the client, without using iframe or a new window, false is default
runInParent: false,
//true: runs the tests inside an iFrame; false: runs the tests in a new window, true is default
useIframe:true,
jasmine:{
//tells jasmine to run specs in semi random order, false is default
random: false
}
},

/*karma-webpack config*/
preprocessors: {
//use webpack to support require() in test-suits .js files
//use babel-loader from webpack to compile es2015 features in .js files
//add webpack as preprocessor
'./tests/*.js': ['webpack']
},
webpackMiddleware: {
//turn off webpack bash output when run the tests
noInfo: true,
stats: 'errors-only'
},

/*karma-mocha-reporter config*/
mochaReporter: {
output: 'noFailures' //full, autowatch, minimal
}
});
};
```

#### 2. adjust the folders structure to your needs
* Adjust `basePath` and `excludes` property, `files` `pattern` properties, and `preprocessors` properties to your need.
* The configuration assumes that the following folder structure is arranged:
```
┌ karma.conf.js
├ package.json
├ webpack.config.js
├ src
│ ├ index.js
│ └ another_module.js
└ tests
├ spec_a.js
└ spec_b.js
```

#### 3. add `babel-loader` to your `webpack.config.js` file

```javascript
const path = require('path');

module.exports = {
entry: {
index:'./src/index.js'
},
output: {
filename: 'bundled.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.js$/i,
exclude:/(node_modules)/,
loader:'babel-loader',
options:{
presets:['env']
}
}
]
}
};
```

#### 4. Add some specs to your test files

##### `spec_a.js`
```javascript
const myModule = require('./../src/index.js');
describe("Module should return", function () {
it("some number", function () {
expect(myModule()).toEqual(10);
});
});
```

##### `index.js`
```javascript
module.exports = ()=> 10;
```

#### 5. Run tests:
* you can call `karma start` in the terminal
* or add `"scripts": { "test": "karma start" }` to your `package.json` and call tests with `npm test`

### Links
* [karma configuration file docs](http://karma-runner.github.io/1.0/config/configuration-file.html)
* [karma files docs](http://karma-runner.github.io/1.0/config/files.html)
* [webpack-babel-installer](https://www.npmjs.com/package/webpack-babel-installer)
* [babel presets docs](https://babeljs.io/docs/plugins/)
* [babel-loader](https://www.npmjs.com/package/babel-loader)
* [karma-webpack (webpack docs)](https://github.com/webpack/docs/wiki/usage-with-karma)
* [karma-webpack (github docs)](https://github.com/webpack-contrib/karma-webpack)
* [jasmine docs](https://jasmine.github.io/api/edge/global)
* [karma-jasmine docs](https://github.com/karma-runner/karma-jasmine)

###### Launchers
* [karma-phantomjs-launcher](https://www.npmjs.com/package/karma-phantomjs-launcher)
* [karma-chrome-launcher](https://www.npmjs.com/package/karma-chrome-launcher)
* [karma-firefox-launcher](https://www.npmjs.com/package/karma-firefox-launcher)
* [karma-edge-launcher](https://www.npmjs.com/package/karma-edge-launcher)
* [karma-ie-launcher](https://www.npmjs.com/package/karma-ie-launcher)
* [karma-opera-launcher](https://www.npmjs.com/package/karma-opera-launcher)
* [karma-safari-launcher](https://www.npmjs.com/package/karma-safari-launcher)

###### Reporters
* [karma-mocha-reporter](https://www.npmjs.com/package/karma-mocha-reporter)
* [karma-spec-reporter](https://www.npmjs.com/package/karma-spec-reporter)
* [karma-jasmine-html-reporter](https://www.npmjs.com/package/karma-jasmine-html-reporter)

### See also
* [karma-html](https://www.npmjs.com/package/karma-html) *(test your `.html` files in the browser)*
* [jasmine-dom-custom-matchers](https://www.npmjs.com/package/jasmine-dom-custom-matchers)
* [webpack-jquery-ui](https://www.npmjs.com/package/webpack-jquery-ui)
* [webpack-icons-installer](https://www.npmjs.com/package/webpack-icons-installer)
* [styles-loader](https://www.npmjs.com/package/styles-loader)
85 changes: 85 additions & 0 deletions sample/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
module.exports = function(config) {
config.set({
//root path location to resolve paths defined in files and exclude
basePath: '',
//files/patterns to exclude from loaded files
exclude: [],
//files/patterns to load in the browser
files: [
{pattern: 'src/*.js', watched:true, served:false, included:false, nocache:false},
{pattern: 'tests/*.js',watched:true,served:true,included:true}
/*parameters*/
//watched: if autoWatch is true all files that have set watched to true will be watched for changes
//served: should the files be served by Karma's webserver?
//included: should the files be included in the browser using <script> tag?
//nocache: should the files be served from disk on each request by Karma's webserver?
/*assets*/
//{pattern: '*.html', watched:true, served:true, included:false}
//{pattern: 'images/*', watched:false, served:true, included:false}
],

//executes the tests whenever one of watched files changes
autoWatch: true,
//if true, Karma will run tests and then exit browser
singleRun:false,
//if true, Karma fails on running empty test-suites
failOnEmptyTestSuite:false,
//reduce the kind of information passed to the bash
logLevel: config.LOG_WARN, //config.LOG_DISABLE, config.LOG_ERROR, config.LOG_INFO, config.LOG_DEBUG

//list of frameworks you want to use, only jasmine is installed automatically
frameworks: ['jasmine'],
//list of browsers to launch and capture
browsers: ['Chrome'/*,'PhantomJS','Firefox','Edge','ChromeCanary','Opera','IE','Safari'*/],
//list of reporters to use
reporters: ['mocha','kjhtml'/*,'dots','progress','spec'*/],

//address that the server will listen on, '0.0.0.0' is default
listenAddress: '0.0.0.0',
//hostname to be used when capturing browsers, 'localhost' is default
hostname: 'localhost',
//the port where the web server will be listening, 9876 is default
port: 9876,
//when a browser crashes, karma will try to relaunch, 2 is default
retryLimit:0,
//how long does Karma wait for a browser to reconnect, 2000 is default
browserDisconnectTimeout: 5000,
//how long will Karma wait for a message from a browser before disconnecting from it, 10000 is default
browserNoActivityTimeout: 10000,
//timeout for capturing a browser, 60000 is default
captureTimeout: 60000,

client: {
//capture all console output and pipe it to the terminal, true is default
captureConsole:false,
//if true, Karma clears the context window upon the completion of running the tests, true is default
clearContext:false,
//run the tests on the same window as the client, without using iframe or a new window, false is default
runInParent: false,
//true: runs the tests inside an iFrame; false: runs the tests in a new window, true is default
useIframe:true,
jasmine:{
//tells jasmine to run specs in semi random order, false is default
random: false
}
},

/*karma-webpack config*/
preprocessors: {
//use webpack to support require() in test-suits .js files
//use babel-loader from webpack to compile es2015 features in .js files
//add webpack as preprocessor
'./tests/*.js': ['webpack']
},
webpackMiddleware: {
//turn off webpack bash output when run the tests
noInfo: true,
stats: 'errors-only'
},

/*karma-mocha-reporter config*/
mochaReporter: {
output: 'noFailures' //full, autowatch, minimal
}
});
};
17 changes: 17 additions & 0 deletions sample/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "karma-sample",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"scripts": {
"test": "karma start",
"start": "webpack --config webpack.config.js"
},
"author": "",
"license": "MIT",
"devDependencies": {
"webpack": "^4.16.4",
"webpack-cli": "^3.1.0",
"webpack-karma-jasmine": "^2.0.1"
}
}
1 change: 1 addition & 0 deletions sample/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = ()=> 10;
6 changes: 6 additions & 0 deletions sample/tests/spec_a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const myModule = require('./../src/index.js');
describe("Module should return", function () {
it("some number", function () {
expect(myModule()).toEqual(10);
});
});
Loading

0 comments on commit c17b78d

Please sign in to comment.