|
1 | | -/*global jasmine, __karma__, window*/ |
2 | | -(function () { |
3 | | - |
4 | | -// Error.stackTraceLimit = Infinity; |
5 | | - |
| 1 | +// /*global jasmine, __karma__, window*/ |
| 2 | +Error.stackTraceLimit = Infinity; |
6 | 3 | jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000; |
7 | 4 |
|
8 | | -// Cancel Karma's synchronous start, |
9 | | -// we call `__karma__.start()` later, once all the specs are loaded. |
10 | | -__karma__.loaded = function () { }; |
11 | | - |
12 | | -// SET THE RUNTIME APPLICATION ROOT HERE |
13 | | -var appRoot ='app'; // no trailing slash! |
14 | | - |
15 | | -// RegExp for client application base path within karma (which always starts 'base\') |
16 | | -var karmaBase = '^\/base\/'; // RegEx string for base of karma folders |
17 | | -var appPackage = 'base/' + appRoot; //e.g., base/app |
18 | | -var appRootRe = new RegExp(karmaBase + appRoot + '\/'); |
19 | | -var onlyAppFilesRe = new RegExp(karmaBase + appRoot + '\/(?!.*\.spec\.js$)([a-z0-9-_\.\/]+)\.js$'); |
20 | | - |
21 | | -var moduleNames = []; |
22 | | - |
23 | | -// Configure systemjs packages to use the .js extension for imports from the app folder |
24 | | -var packages = {}; |
25 | | -packages[appPackage] = { |
26 | | - defaultExtension: false, |
27 | | - format: 'register', |
28 | | - map: Object.keys(window.__karma__.files) |
29 | | - .filter(onlyAppFiles) |
30 | | - // Create local module name mapping to karma file path for app files |
31 | | - // with karma's fingerprint in query string, e.g.: |
32 | | - // './hero.service': '/base/app/hero.service.js?f4523daf879cfb7310ef6242682ccf10b2041b3e' |
33 | | - .reduce(function (pathsMapping, appPath) { |
34 | | - var moduleName = appPath.replace(appRootRe, './').replace(/\.js$/, ''); |
35 | | - pathsMapping[moduleName] = appPath + '?' + window.__karma__.files[appPath]; |
36 | | - return pathsMapping; |
37 | | - }, {}) |
38 | | - } |
39 | | - |
40 | | -System.config({ packages: packages }); |
41 | | - |
42 | | -// Configure Angular for the browser and |
43 | | -// with test versions of the platform providers |
44 | | - Promise.all([ |
45 | | - System.import('angular2/testing'), |
46 | | - System.import('angular2/platform/testing/browser') |
47 | | - ]) |
48 | | - .then(function (results) { |
49 | | - var testing = results[0]; |
50 | | - var browser = results[1]; |
51 | | - testing.setBaseTestProviders( |
52 | | - browser.TEST_BROWSER_PLATFORM_PROVIDERS, |
53 | | - browser.TEST_BROWSER_APPLICATION_PROVIDERS); |
54 | | - |
55 | | - // Load all spec files |
56 | | - // (e.g. 'base/app/hero.service.spec.js') |
57 | | - return Promise.all( |
58 | | - Object.keys(window.__karma__.files) |
59 | | - .filter(onlySpecFiles) |
60 | | - .map(function (moduleName) { |
61 | | - moduleNames.push(moduleName); |
62 | | - return System.import(moduleName); |
63 | | - })); |
64 | | - }) |
65 | | - |
66 | | - .then(success, fail); |
67 | | - |
68 | | -////// Helpers ////// |
69 | | - |
70 | | -function onlyAppFiles(filePath) { |
71 | | - return onlyAppFilesRe.test(filePath); |
72 | | -} |
| 5 | +__karma__.loaded = function () { |
| 6 | +}; |
73 | 7 |
|
74 | | -function onlySpecFiles(filePath) { |
75 | | - return /\.spec\.js$/.test(filePath); |
| 8 | +function isJsFile(path) { |
| 9 | + return path.slice(-3) == '.js'; |
76 | 10 | } |
77 | 11 |
|
78 | | -function success () { |
79 | | - console.log( |
80 | | - 'Spec files loaded:\n ' + |
81 | | - moduleNames.join('\n ') + |
82 | | - '\nStarting Jasmine testrunner'); |
83 | | - __karma__.start(); |
| 12 | +function isSpecFile(path) { |
| 13 | + return /\.spec\.js$/.test(path); |
84 | 14 | } |
85 | 15 |
|
86 | | -function fail(error) { |
87 | | - __karma__.error(error.stack || error); |
| 16 | +function isBuiltFile(path) { |
| 17 | + var builtPath = '/base/app/'; |
| 18 | + return isJsFile(path) && (path.substr(0, builtPath.length) == builtPath); |
88 | 19 | } |
89 | 20 |
|
90 | | -})(); |
| 21 | +var allSpecFiles = Object.keys(window.__karma__.files) |
| 22 | + .filter(isSpecFile) |
| 23 | + .filter(isBuiltFile); |
| 24 | + |
| 25 | +// Load our SystemJS configuration. |
| 26 | + |
| 27 | +var packages ={ |
| 28 | + 'app': { main: 'main.js', defaultExtension: 'js' }, |
| 29 | + 'rxjs': { defaultExtension: 'js' }, |
| 30 | +}; |
| 31 | + |
| 32 | +// Add angular packages to SystemJS config |
| 33 | +[ |
| 34 | + '@angular/common', |
| 35 | + '@angular/compiler', |
| 36 | + '@angular/core', |
| 37 | + '@angular/http', |
| 38 | + '@angular/platform-browser', |
| 39 | + '@angular/platform-browser-dynamic', |
| 40 | + '@angular/router', |
| 41 | + '@angular/router-deprecated', |
| 42 | + '@angular/upgrade' |
| 43 | +].forEach(function (name) { packages[name] = {main: 'index.js', defaultExtension: 'js'};}); |
| 44 | + |
| 45 | +System.config({ |
| 46 | + baseURL: '/base', |
| 47 | + map: { |
| 48 | + 'rxjs': 'node_modules/rxjs', |
| 49 | + '@angular': 'node_modules/@angular', |
| 50 | + 'app': 'app' |
| 51 | + }, |
| 52 | + packages: packages |
| 53 | +}); |
| 54 | + |
| 55 | +Promise.all([ |
| 56 | + System.import('@angular/core/testing'), |
| 57 | + System.import('@angular/platform-browser-dynamic/testing') |
| 58 | +]).then(function (providers) { |
| 59 | + var testing = providers[0]; |
| 60 | + var testingBrowser = providers[1]; |
| 61 | + |
| 62 | + testing.setBaseTestProviders( |
| 63 | + testingBrowser.TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, |
| 64 | + testingBrowser.TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS); |
| 65 | + |
| 66 | +}).then(function() { |
| 67 | + // Finally, load all spec files. |
| 68 | + // This will run the tests directly. |
| 69 | + return Promise.all( |
| 70 | + allSpecFiles.map(function (moduleName) { |
| 71 | + return System.import(moduleName); |
| 72 | + })); |
| 73 | +}).then(__karma__.start, __karma__.error); |
0 commit comments