Skip to content

Commit

Permalink
chore: add basic unit-testing infrastructure with Karma
Browse files Browse the repository at this point in the history
  • Loading branch information
pkozlowski-opensource committed Sep 17, 2015
1 parent 127d11b commit 780714d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
npm-debug.log
node_modules
dist
7 changes: 5 additions & 2 deletions TODO.md
@@ -1,2 +1,5 @@
- testing infrastructure
-
* testing infrastructure
* finish first version of the pre-processor
* calculate module path name
* push options to the karma config configuration
* how can I start all JS tests from System.register modules
37 changes: 37 additions & 0 deletions build/karma-typescript-preprocessor/index.js
@@ -0,0 +1,37 @@
var ts = require('typescript');
var path = require('path');

var createTypeScriptPreprocessor = function (args, config, logger) {

var log = logger.create('preprocessor.typescript');
var projectBasePath = path.resolve(__dirname, '../..'); //TODO: OS-specific, path specific

return function (content, file, done) {

var baseRelativePath = path.relative(projectBasePath, file.originalPath);
baseRelativePath = path.join('base', baseRelativePath);

log.debug('Processing "%s".', file.originalPath);

//TODO: pass those options from "outside"
var compilerOptions = {
module: ts.ModuleKind.System,
experimentalDecorators: true
};

var compileResult = ts.transpileModule(content, {
compilerOptions: compilerOptions,
moduleName: baseRelativePath.slice(0, -path.extname(baseRelativePath).length)
});

//TODO: what happens if there is transpilation error?
done(compileResult.outputText); //keys: [ 'outputText', 'diagnostics', 'sourceMapText' ]
};
};

// PUBLISH DI MODULE
module.exports = {
'preprocessor:TypeScript': ['factory', createTypeScriptPreprocessor]
};

//TODO: source-map support
20 changes: 20 additions & 0 deletions build/tests-bootstrap.js
@@ -0,0 +1,20 @@
// Cancel Karma's synchronous start,
// we will call `__karma__.start()` later, once all the specs are loaded.
__karma__.loaded = function() {};

// Import all the specs, execute their `main()` method and kick off Karma (Jasmine).
Promise
.all(Object.keys(window.__karma__.files) // All files served by Karma.
.filter(onlySpecFiles)
.map(function(path) {
return System.import(path.slice(0, -3)); //TODO: hard-coded extension length
}))
.then(function() {
__karma__.start();
}, function(error) {
__karma__.error(error.stack || error);
});

function onlySpecFiles(path) {
return /spec\.ts$/.test(path); //TODO: hard-coded extension
}
25 changes: 22 additions & 3 deletions gulpfile.js
Expand Up @@ -3,12 +3,13 @@ var gulp = require('gulp');
var PATHS = {
src: {
js: 'src/**/*.ts',
test: 'test/**/*.spec.ts',
html: 'src/**/*.html'
},
lib: [
'node_modules/angular2/node_modules/traceur/bin/traceur-runtime.js',
'node_modules/angular2/bundles/angular2.js',
'node_modules/systemjs/dist/system-csp-production.js'
'node_modules/systemjs/dist/system-csp-production.js',
'node_modules/angular2/bundles/angular2.js'
],
typings: 'node_modules/angular2/bundles/typings/angular2/angular2.d.ts'
};
Expand Down Expand Up @@ -40,6 +41,25 @@ gulp.task('libs', function () {
return gulp.src(PATHS.lib).pipe(gulp.dest('dist/lib'));
});

gulp.task('test', function (done) {
var Server = require('karma').Server;
new Server({
browsers: ['Chrome'],
frameworks: ['jasmine'],
files: [
PATHS.lib[0], PATHS.lib[1], PATHS.lib[2], //TODO: do it better
PATHS.src.js,
PATHS.src.test,
'build/tests-bootstrap.js'
],
preprocessors: {
'**/*.ts': ['TypeScript']
},
plugins: ['karma-jasmine', 'karma-chrome-launcher', require('./build/karma-typescript-preprocessor')],
singleRun: true
}, done).start();
});

gulp.task('play', ['libs', 'html', 'js'], function () {
var httpPlay = require('http-play');

Expand All @@ -48,4 +68,3 @@ gulp.task('play', ['libs', 'html', 'js'], function () {

httpPlay({dist: __dirname + '/dist', port: 9000});
});

7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -14,7 +14,12 @@
"del": "^1.2.0",
"gulp": "^3.9.0",
"gulp-typescript": "^2.8.0",
"http-play": "^0.1.0"
"http-play": "^0.1.0",
"jasmine-core": "^2.3.4",
"karma": "^0.13.9",
"karma-chrome-launcher": "^0.2.0",
"karma-jasmine": "^0.3.6",
"typescript": "^1.6.0-dev.20150915"
},
"dependencies": {
"angular2": "2.0.0-alpha.37",
Expand Down
14 changes: 14 additions & 0 deletions test/progress/progress.spec.ts
@@ -0,0 +1,14 @@
import {BsProgress} from '../../src/progress/progress';

describe('progress', () => {
var progress;

beforeEach(() => {
progress = new BsProgress();
});

it('should calculate progress percent for default min = 0 / max = 100', () => {
progress.value = 60;
expect(progress.getPercentValue()).toBe(60);
});
});

0 comments on commit 780714d

Please sign in to comment.