diff --git a/karma-test-shim.js b/karma-test-shim.js new file mode 100644 index 0000000..bda7f4a --- /dev/null +++ b/karma-test-shim.js @@ -0,0 +1,70 @@ +// reference: https://github.com/ocombe/ng2-translate/blob/master/karma-test-shim.js + +// Turn on full stack traces in errors to help debugging +Error.stackTraceLimit = Infinity; + +jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; + +// Cancel Karma"s synchronous start, +// we will call `__karma__.start()` later, once all the specs are loaded. +__karma__.loaded = function () { }; + +System.config({ + baseURL: "/base/", + defaultJSExtensions: true, + paths: { + "n:*": "node_modules/*" + }, + map: { + "@angular": "n:@angular", + "@ssv": "n:@ssv", + "rxjs": "n:rxjs", + }, + packages: { + "@angular/common": { main: "index.js", defaultExtension: "js" }, + "@angular/compiler": { main: "index.js", defaultExtension: "js" }, + "@angular/core": { main: "index.js", defaultExtension: "js" }, + "@angular/platform-browser": { main: "index.js", defaultExtension: "js" }, + "@angular/platform-browser-dynamic": { main: "index.js", defaultExtension: "js" }, + + "@ssv/ng2-core": { main: "dist/amd/index.js", defaultExtension: "js" }, + } +}); + +System.import("@angular/platform-browser/src/browser/browser_adapter") + .then(function (browser_adapter) { + browser_adapter.BrowserDomAdapter.makeCurrent(); + }) + .then(function () { + return Promise.all([ + System.import("@angular/core/testing"), + System.import("@angular/platform-browser-dynamic/testing") + ]).then(function (providers) { + var testing = providers[0]; + var testingBrowser = providers[1]; + + testing.TestBed.initTestEnvironment( + testingBrowser.BrowserDynamicTestingModule, + testingBrowser.platformBrowserDynamicTesting() + ); + }); + }) + .then(function () { + return Promise.all(resolveTestFiles()); + }) + .then(function () { + __karma__.start(); + }, function (error) { + __karma__.error(error.stack || error); + }); + +function onlySpecFiles(path) { + return /[\.|_]spec\.js$/.test(path); +} +function resolveTestFiles() { + return Object.keys(window.__karma__.files) // All files served by Karma. + .filter(onlySpecFiles) + .map(function (moduleName) { + return System.import(moduleName); + }); +} \ No newline at end of file diff --git a/karma.conf.js b/karma.conf.js index 3393e43..d31cf8e 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -3,58 +3,49 @@ const conf = require("./build/config"); module.exports = function (config) { config.set({ basePath: "./", - frameworks: ["systemjs", "jasmine"], + frameworks: ["jasmine"], - systemjs: { - config: { - paths: { - "*": "*", - "src/*": "src/*", - "typescript": "node_modules/typescript/lib/typescript.js", - "systemjs": "node_modules/systemjs/dist/system.js", - "system-polyfills": "node_modules/systemjs/dist/system-polyfills.js", - "es6-module-loader": "node_modules/es6-module-loader/dist/es6-module-loader.js", - "n:*": "node_modules/*" - }, - map: { - "@angular": "n:@angular", - "rxjs": "n:rxjs" - }, - packages: { - "src": { - defaultExtension: "ts" - }, - "rxjs": { - defaultExtension: "js" - }, - "@angular/core": { - main: "index.js", - defaultExtension: "js" - } - }, - transpiler: "typescript" - }, - serveFiles: [ - conf.src.ts, - "node_modules/**/*.js" - ] - }, files: [ - // Polyfills. "node_modules/es6-shim/es6-shim.js", - "node_modules/reflect-metadata/Reflect.js", - - // Zone.js dependencies "node_modules/zone.js/dist/zone.js", - "node_modules/zone.js/dist/jasmine-patch.js", "node_modules/zone.js/dist/async-test.js", "node_modules/zone.js/dist/fake-async-test.js", + "node_modules/zone.js/dist/long-stack-trace-zone.js", + "node_modules/zone.js/dist/jasmine-patch.js", + "node_modules/systemjs/dist/system.src.js", + "node_modules/reflect-metadata/Reflect.js", - conf.src.testTs, - "src/*.spec.ts" + { pattern: "node_modules/reflect-metadata/**/*.js.map", included: false, watched: false, served: true }, + { pattern: "node_modules/systemjs/dist/system-polyfills.js", included: false, watched: false, served: true }, // PhantomJS2 (and possibly others) might require it + { pattern: "node_modules/@angular/**/*.js", included: false, watched: false, served: true }, + { pattern: "node_modules/@angular/**/*.js.map", included: false, watched: false, served: true }, + { pattern: "node_modules/rxjs/**/*.js", included: false, watched: false, served: true }, + { pattern: "node_modules/rxjs/**/*.js.map", included: false, watched: false, served: true }, + { pattern: "node_modules/@ssv/*/dist/**/*.js", included: false, watched: false, served: true }, + { pattern: "node_modules/@ssv/*/dist/**/*.js.map", included: false, watched: false, served: true }, + + { pattern: conf.src.ts, included: false, watched: true }, // source files + "karma-test-shim.js" + ], + exclude: [ + "node_modules/**/*_spec.js", + "node_modules/**/*.spec.js", ], - exclude: [], - preprocessors: {}, + preprocessors: { + // "src/**/*.html": ["ng-html2js"], + [conf.src.ts]: ["typescript"], + }, + typescriptPreprocessor: { + options: { + inlineSourceMap: true, + inlineSources: true, + emitDecoratorMetadata: true, + experimentalDecorators: true + } + }, + client: { + captureConsole: false // disables console logs + }, reporters: ["mocha"], // note: gulp using config from config.js instead port: 9876, colors: true, diff --git a/src/logging/logger.service.spec.ts b/src/logging/logger.service.spec.ts index df8fed5..1eaa36d 100644 --- a/src/logging/logger.service.spec.ts +++ b/src/logging/logger.service.spec.ts @@ -1,22 +1,30 @@ -import { - it, - inject, - describe, - expect, - beforeEach, - beforeEachProviders -} from "@angular/core/testing"; +import {inject, TestBed} from "@angular/core/testing"; + import {LoggerService} from "./logger.service"; describe("LoggerService", () => { - beforeEachProviders(() => [LoggerService]); + + let SUT: LoggerService; + beforeEach(() => { - spyOn(console, "log"); + + TestBed.configureTestingModule({ + providers: [LoggerService] + }); }); - it("should log successfully", inject([LoggerService], (logger: LoggerService) => { + beforeEach(inject([ + LoggerService + ], ( + logger: LoggerService + ) => { + SUT = logger; + spyOn(console, "log"); + })); + + it("should log successfully", () => { let message = "Hello"; - logger.log("log", message); + SUT.log("log", message); expect(console.log).toHaveBeenCalledWith(message); - })); + }); }); \ No newline at end of file