From 780714d959b82e80782292ae51364f7f84ad6185 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Mon, 14 Sep 2015 15:59:16 +0200 Subject: [PATCH] chore: add basic unit-testing infrastructure with Karma --- .gitignore | 1 + TODO.md | 7 ++-- build/karma-typescript-preprocessor/index.js | 37 ++++++++++++++++++++ build/tests-bootstrap.js | 20 +++++++++++ gulpfile.js | 25 +++++++++++-- package.json | 7 +++- test/progress/progress.spec.ts | 14 ++++++++ 7 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 build/karma-typescript-preprocessor/index.js create mode 100644 build/tests-bootstrap.js create mode 100644 test/progress/progress.spec.ts diff --git a/.gitignore b/.gitignore index 76add87..311e9e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +npm-debug.log node_modules dist \ No newline at end of file diff --git a/TODO.md b/TODO.md index ce17e3b..b43e271 100644 --- a/TODO.md +++ b/TODO.md @@ -1,2 +1,5 @@ -- testing infrastructure -- \ No newline at end of file +* 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 \ No newline at end of file diff --git a/build/karma-typescript-preprocessor/index.js b/build/karma-typescript-preprocessor/index.js new file mode 100644 index 0000000..ea5c60e --- /dev/null +++ b/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 \ No newline at end of file diff --git a/build/tests-bootstrap.js b/build/tests-bootstrap.js new file mode 100644 index 0000000..f9a2add --- /dev/null +++ b/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 +} diff --git a/gulpfile.js b/gulpfile.js index 8584708..e465e58 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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' }; @@ -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'); @@ -48,4 +68,3 @@ gulp.task('play', ['libs', 'html', 'js'], function () { httpPlay({dist: __dirname + '/dist', port: 9000}); }); - diff --git a/package.json b/package.json index 93b03b5..f9d6a5a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/progress/progress.spec.ts b/test/progress/progress.spec.ts new file mode 100644 index 0000000..e342afb --- /dev/null +++ b/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); + }); +});