Skip to content

Commit

Permalink
add karma test
Browse files Browse the repository at this point in the history
  • Loading branch information
wangpei07 committed Apr 3, 2018
1 parent 8b36445 commit d4bd819
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# utils

> 2018-03-31
- 文档:https://searchfe.github.io/utils/
87 changes: 87 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Karma configuration
// Generated on Wed Apr 04 2018 00:03:37 GMT+0800 (CST)

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', 'requirejs', 'chai-as-promised', 'chai-sinon'],


// list of files / patterns to load in the browser
files: [
'test-main.js',
{pattern: 'src/**/*.js', included: false},
{pattern: 'amd_modules/**/*.js', included: false},
{pattern: 'test/**/*.js', included: false}
],


// list of files to exclude
exclude: [
'**/*.swp'
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
// source files, that you wanna generate coverage for
// do not include tests or libraries
// (these files will be instrumented by Istanbul)
'src/**/*.js': ['coverage']
},


// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['mocha'],
coverageReporter: {
reporters: [
{
type: 'text-summary'
}, {
type: 'lcov',
dir: 'coverage/'
}
]
},


// 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: ['ChromeHeadless'],


// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
24 changes: 23 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,27 @@
"bugs": {
"url": "https://github.com/searchfe/utils/issues"
},
"homepage": "https://github.com/searchfe/utils#readme"
"homepage": "https://github.com/searchfe/utils#readme",
"devDependencies": {
"chai": "^3.5.0",
"chai-as-promised": "^5.3.0",
"karma": "^2.0.0",
"karma-chai": "^0.1.0",
"karma-chai-as-promised": "^0.1.2",
"karma-chai-sinon": "^0.1.5",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.1",
"karma-coveralls": "^1.1.2",
"karma-firefox-launcher": "^1.1.0",
"karma-html-reporter": "^0.2.7",
"karma-mocha": "^1.1.1",
"karma-mocha-reporter": "^2.0.4",
"karma-opera-launcher": "^1.0.0",
"karma-requirejs": "^1.1.0",
"karma-safari-launcher": "^1.0.0",
"mocha": "^3.2.0",
"requirejs": "^2.3.5",
"sinon": "^2.1.0",
"sinon-chai": "^2.8.0"
}
}
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
define(function() {

function Browser () {
this._name = '';
};

Browser.prototype._getBrowserName = function (ua) {
var ua = ua || navigator.userAgent;
if (ua.indexOf('QQBrowser') > -1) {
this._name = 'qq';
}
}

return Browser;

});
33 changes: 33 additions & 0 deletions test-main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @file testMain test entry for karma
* @author harttle<[email protected]>
*/
var allTestFiles = [];
var TEST_REGEXP = /test\/.*\.js$/i;

// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function (file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
// If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
// then do not normalize the paths
var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
allTestFiles.push(normalizedTestModule);
}
});

require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base',

// dynamically load all test files
deps: allTestFiles,

// external AMD modules
paths: {
'@searchfe/assert': 'amd_modules/@searchfe/assert'
},

// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
define(['src/index'], function (Browser) {
describe('Browser', function () {
describe('new', function () {
it('should get qq browser name', function () {
var ua = 'Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; HUAWEI C8818 Build/HuaweiC8818) AppleWebKit/537.36 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.8 Mobile Safari/537.36';
var browser = new Browser();
browser._getBrowserName(ua);
expect(browser._name).to.equal('qq');
});
});
});
});

0 comments on commit d4bd819

Please sign in to comment.