Skip to content

Commit

Permalink
test(logger): fixed test for logger to use rc5 features + updated kar…
Browse files Browse the repository at this point in the history
…ma.conf/shim
  • Loading branch information
stephenlautier committed Aug 9, 2016
1 parent f335cc2 commit 837e9c4
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 57 deletions.
70 changes: 70 additions & 0 deletions karma-test-shim.js
Original file line number Diff line number Diff line change
@@ -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);
});
}
79 changes: 35 additions & 44 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
34 changes: 21 additions & 13 deletions src/logging/logger.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
}));
});
});

0 comments on commit 837e9c4

Please sign in to comment.