Skip to content

Commit 9e2583d

Browse files
davidsekarMarkPieszak
authored andcommitted
(Unit testing) using Karma test runner (TrilonIO#399)
* Modified .csproject to emit the wwwroot/dist folder, if the folder is not created yet * Remove Jest and added Karma test runner for executing unit test cases
1 parent 359623e commit 9e2583d

File tree

7 files changed

+201
-36
lines changed

7 files changed

+201
-36
lines changed

Asp2017.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
</Target>
5555
<Target Name="CleanDist" AfterTargets="Clean">
5656
<ItemGroup>
57-
<FilesToDelete Include="Client\dist\**; wwwroot\dist\**" />
57+
<FilesToDelete Include="ClientApp\dist\**; wwwroot\dist\**" />
5858
</ItemGroup>
5959
<Delete Files="@(FilesToDelete)" />
6060
<RemoveDir Directories="Client\dist; wwwroot\dist" />

ClientApp/test/boot-tests.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Error.stackTraceLimit = Infinity;
2+
// Load required polyfills and testing libraries
3+
require('reflect-metadata');
4+
require('zone.js');
5+
require('zone.js/dist/long-stack-trace-zone');
6+
require('zone.js/dist/proxy.js');
7+
require('zone.js/dist/sync-test');
8+
require('zone.js/dist/jasmine-patch');
9+
require('zone.js/dist/async-test');
10+
require('zone.js/dist/fake-async-test');
11+
const testing = require('@angular/core/testing');
12+
const testingBrowser = require('@angular/platform-browser-dynamic/testing');
13+
14+
// Prevent Karma from running prematurely
15+
__karma__.loaded = function () {};
16+
17+
// First, initialize the Angular testing environment
18+
testing.getTestBed().initTestEnvironment(
19+
testingBrowser.BrowserDynamicTestingModule,
20+
testingBrowser.platformBrowserDynamicTesting()
21+
);
22+
23+
// Then we find all the tests
24+
const context = require.context('../', true, /\.spec\.ts$/);
25+
26+
// And load the modules
27+
context.keys().map(context);
28+
29+
// Finally, start Karma to run the tests
30+
__karma__.start();

ClientApp/test/jestGlobalMocks.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

ClientApp/test/karma.conf.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Karma configuration file, see link for more information
2+
// https://karma-runner.github.io/0.13/config/configuration-file.html
3+
4+
module.exports = function (config) {
5+
config.set({
6+
basePath: '.',
7+
frameworks: ['jasmine'],
8+
exclude: [],
9+
files: [
10+
'./boot-tests.js'
11+
],
12+
preprocessors: {
13+
'./boot-tests.js': ['coverage', 'webpack', 'sourcemap']
14+
},
15+
client: {
16+
captureConsole: false
17+
},
18+
coverageReporter: {
19+
type: 'in-memory'
20+
},
21+
remapCoverageReporter: {
22+
'text-summary': null,
23+
json: './coverage/coverage.json',
24+
html: './coverage/html'
25+
},
26+
reporters: ['mocha', 'coverage', 'remap-coverage'],
27+
port: 9876,
28+
colors: true,
29+
logLevel: config.LOG_WARN,
30+
autoWatch: false,
31+
browsers: ['Chrome'],
32+
mime: {
33+
'application/javascript': ['ts', 'tsx']
34+
},
35+
singleRun: true,
36+
webpack: require('./webpack.config.test.js')({
37+
env: 'test'
38+
}),
39+
webpackMiddleware: {
40+
noInfo: true,
41+
stats: {
42+
chunks: false
43+
}
44+
}
45+
});
46+
};

ClientApp/test/setupJest.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin');
2+
const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin');
3+
4+
var path = require('path');
5+
var rootPath = path.join.bind(path, path.resolve(__dirname, '../../'));
6+
7+
module.exports = function (options) {
8+
return {
9+
devtool: 'inline-source-map',
10+
resolve: {
11+
extensions: ['.ts', '.js'],
12+
modules: [rootPath('ClientApp'), 'node_modules']
13+
},
14+
module: {
15+
rules: [{
16+
enforce: 'pre',
17+
test: /\.js$/,
18+
loader: 'source-map-loader',
19+
exclude: [
20+
rootPath('node_modules/rxjs'),
21+
rootPath('node_modules/@angular')
22+
]
23+
},
24+
{
25+
test: /\.ts$/,
26+
use: [{
27+
loader: 'awesome-typescript-loader',
28+
query: {
29+
sourceMap: false,
30+
inlineSourceMap: true,
31+
compilerOptions: {
32+
removeComments: true
33+
}
34+
},
35+
},
36+
'angular2-template-loader'
37+
],
38+
exclude: [/\.e2e\.ts$/]
39+
},
40+
{
41+
test: /\.css$/,
42+
loader: ['to-string-loader', 'css-loader']
43+
},
44+
{
45+
test: /\.scss$/,
46+
loader: ['raw-loader', 'sass-loader']
47+
},
48+
{
49+
test: /\.html$/,
50+
loader: 'raw-loader'
51+
},
52+
{
53+
enforce: 'post',
54+
test: /\.(js|ts)$/,
55+
loader: 'istanbul-instrumenter-loader',
56+
options: {
57+
esModules: true
58+
},
59+
include: rootPath('ClientApp'),
60+
exclude: [
61+
/ClientApp\\test/,
62+
/\.(e2e|spec)\.ts$/,
63+
/node_modules/
64+
]
65+
}
66+
67+
]
68+
},
69+
plugins: [
70+
new ContextReplacementPlugin(
71+
/**
72+
* The (\\|\/) piece accounts for path separators in *nix and Windows
73+
*/
74+
/angular(\\|\/)core(\\|\/)@angular/,
75+
rootPath('ClientApp'), // location of your src
76+
{
77+
/**
78+
* your Angular Async Route paths relative to this root directory
79+
*/
80+
}
81+
),
82+
new LoaderOptionsPlugin({
83+
debug: false,
84+
options: {
85+
/**
86+
* legacy options go here
87+
*/
88+
}
89+
}),
90+
91+
],
92+
performance: {
93+
hints: false
94+
},
95+
96+
/**
97+
* Include polyfills or mocks for various node stuff
98+
* Description: Node configuration
99+
*
100+
* See: https://webpack.github.io/docs/configuration.html#node
101+
*/
102+
node: {
103+
global: true,
104+
process: false,
105+
crypto: 'empty',
106+
module: false,
107+
clearImmediate: false,
108+
setImmediate: false
109+
}
110+
111+
};
112+
}

package.json

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,15 @@
22
"name": "angular4-aspnetcore-universal",
33
"version": "1.0.0-rc3",
44
"scripts": {
5-
"test": "jest",
6-
"test:watch": "npm run test -- --watch",
5+
"test": "karma start ClientApp/test/karma.conf.js",
6+
"test:watch": "npm run test -- --auto-watch --no-single-run",
77
"test:ci": "npm run test -- --runInBand",
88
"test:coverage": "npm run test -- --coverage",
99
"build:dev": "npm run build:vendor && npm run build:webpack",
1010
"build:webpack": "webpack --progress --color",
1111
"build:prod": "npm run build:vendor -- --env.prod && npm run build:webpack -- --env.prod",
1212
"build:vendor": "webpack --config webpack.config.vendor.js --progress --color"
1313
},
14-
"jest": {
15-
"preset": "jest-preset-angular",
16-
"setupTestFrameworkScriptFile": "./ClientApp/test/setupJest.ts",
17-
"globals": {
18-
"__TS_CONFIG__": "ClientApp/tsconfig.spec.json",
19-
"__TRANSFORM_HTML__": true
20-
},
21-
"coveragePathIgnorePatterns": [
22-
"/node_modules/",
23-
"<rootDir>/ClientApp/test/.*.ts"
24-
],
25-
"coverageDirectory": "coverage"
26-
},
2714
"dependencies": {
2815
"@angular/animations": "^4.3.0",
2916
"@angular/common": "^4.3.0",
@@ -81,12 +68,19 @@
8168
"@ngtools/webpack": "^1.3.0",
8269
"@types/chai": "^3.4.34",
8370
"@types/jasmine": "^2.5.37",
84-
"@types/jest": "^19.2.3",
8571
"chai": "^3.5.0",
8672
"codelyzer": "^3.0.0",
73+
"istanbul-instrumenter-loader": "^3.0.0",
8774
"jasmine-core": "^2.5.2",
88-
"jest": "^20.0.0",
89-
"jest-preset-angular": "^2.0.1",
75+
"karma": "^1.7.1",
76+
"karma-chai": "^0.1.0",
77+
"karma-chrome-launcher": "^2.2.0",
78+
"karma-coverage": "^1.1.1",
79+
"karma-jasmine": "^1.1.0",
80+
"karma-mocha-reporter": "^2.2.4",
81+
"karma-remap-coverage": "^0.1.4",
82+
"karma-sourcemap-loader": "^0.3.7",
83+
"karma-webpack": "^2.0.3",
9084
"tslint": "^5.0.0"
9185
}
9286
}

0 commit comments

Comments
 (0)