From 224ea0b0b6b42e2e250ec97e0b11f1e5ef5c9fab Mon Sep 17 00:00:00 2001 From: David Gnanasekaran Date: Sat, 26 Aug 2017 14:24:56 +0530 Subject: [PATCH 1/2] Modified .csproject to emit the wwwroot/dist folder, if the folder is not created yet --- Asp2017.csproj | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Asp2017.csproj b/Asp2017.csproj index db3c39eb..1bebcdf2 100644 --- a/Asp2017.csproj +++ b/Asp2017.csproj @@ -23,6 +23,20 @@ + + + + + + + + + + + + + From 3ade01ea392de5fd2d5aeb5e0a7b8803fab99779 Mon Sep 17 00:00:00 2001 From: davidsekar Date: Mon, 4 Sep 2017 19:54:43 +0530 Subject: [PATCH 2/2] Remove Jest and added Karma test runner for executing unit test cases --- Asp2017.csproj | 2 +- ClientApp/test/boot-tests.js | 30 +++++++ ClientApp/test/jestGlobalMocks.ts | 15 ---- ClientApp/test/karma.conf.js | 46 +++++++++++ ClientApp/test/setupJest.ts | 2 - ClientApp/test/webpack.config.test.js | 112 ++++++++++++++++++++++++++ package.json | 30 +++---- 7 files changed, 201 insertions(+), 36 deletions(-) create mode 100644 ClientApp/test/boot-tests.js delete mode 100644 ClientApp/test/jestGlobalMocks.ts create mode 100644 ClientApp/test/karma.conf.js delete mode 100644 ClientApp/test/setupJest.ts create mode 100644 ClientApp/test/webpack.config.test.js diff --git a/Asp2017.csproj b/Asp2017.csproj index 1bebcdf2..d944c48d 100644 --- a/Asp2017.csproj +++ b/Asp2017.csproj @@ -54,7 +54,7 @@ - + diff --git a/ClientApp/test/boot-tests.js b/ClientApp/test/boot-tests.js new file mode 100644 index 00000000..eba839d2 --- /dev/null +++ b/ClientApp/test/boot-tests.js @@ -0,0 +1,30 @@ +Error.stackTraceLimit = Infinity; +// Load required polyfills and testing libraries +require('reflect-metadata'); +require('zone.js'); +require('zone.js/dist/long-stack-trace-zone'); +require('zone.js/dist/proxy.js'); +require('zone.js/dist/sync-test'); +require('zone.js/dist/jasmine-patch'); +require('zone.js/dist/async-test'); +require('zone.js/dist/fake-async-test'); +const testing = require('@angular/core/testing'); +const testingBrowser = require('@angular/platform-browser-dynamic/testing'); + +// Prevent Karma from running prematurely +__karma__.loaded = function () {}; + +// First, initialize the Angular testing environment +testing.getTestBed().initTestEnvironment( + testingBrowser.BrowserDynamicTestingModule, + testingBrowser.platformBrowserDynamicTesting() +); + +// Then we find all the tests +const context = require.context('../', true, /\.spec\.ts$/); + +// And load the modules +context.keys().map(context); + +// Finally, start Karma to run the tests +__karma__.start(); diff --git a/ClientApp/test/jestGlobalMocks.ts b/ClientApp/test/jestGlobalMocks.ts deleted file mode 100644 index e5e90ebc..00000000 --- a/ClientApp/test/jestGlobalMocks.ts +++ /dev/null @@ -1,15 +0,0 @@ -const mock = () => { - let storage = {}; - return { - getItem: key => key in storage ? storage[key] : null, - setItem: (key, value) => storage[key] = value || '', - removeItem: key => delete storage[key], - clear: () => storage = {} - }; -}; - -Object.defineProperty(window, 'localStorage', {value: mock()}); -Object.defineProperty(window, 'sessionStorage', {value: mock()}); -Object.defineProperty(window, 'getComputedStyle', { - value: () => ['-webkit-appearance'] -}); diff --git a/ClientApp/test/karma.conf.js b/ClientApp/test/karma.conf.js new file mode 100644 index 00000000..0ae99871 --- /dev/null +++ b/ClientApp/test/karma.conf.js @@ -0,0 +1,46 @@ +// Karma configuration file, see link for more information +// https://karma-runner.github.io/0.13/config/configuration-file.html + +module.exports = function (config) { + config.set({ + basePath: '.', + frameworks: ['jasmine'], + exclude: [], + files: [ + './boot-tests.js' + ], + preprocessors: { + './boot-tests.js': ['coverage', 'webpack', 'sourcemap'] + }, + client: { + captureConsole: false + }, + coverageReporter: { + type: 'in-memory' + }, + remapCoverageReporter: { + 'text-summary': null, + json: './coverage/coverage.json', + html: './coverage/html' + }, + reporters: ['mocha', 'coverage', 'remap-coverage'], + port: 9876, + colors: true, + logLevel: config.LOG_WARN, + autoWatch: false, + browsers: ['Chrome'], + mime: { + 'application/javascript': ['ts', 'tsx'] + }, + singleRun: true, + webpack: require('./webpack.config.test.js')({ + env: 'test' + }), + webpackMiddleware: { + noInfo: true, + stats: { + chunks: false + } + } + }); +}; diff --git a/ClientApp/test/setupJest.ts b/ClientApp/test/setupJest.ts deleted file mode 100644 index 1d3bd024..00000000 --- a/ClientApp/test/setupJest.ts +++ /dev/null @@ -1,2 +0,0 @@ -import 'jest-preset-angular'; -import './jestGlobalMocks'; diff --git a/ClientApp/test/webpack.config.test.js b/ClientApp/test/webpack.config.test.js new file mode 100644 index 00000000..6818b46c --- /dev/null +++ b/ClientApp/test/webpack.config.test.js @@ -0,0 +1,112 @@ +const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin'); +const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin'); + +var path = require('path'); +var rootPath = path.join.bind(path, path.resolve(__dirname, '../../')); + +module.exports = function (options) { + return { + devtool: 'inline-source-map', + resolve: { + extensions: ['.ts', '.js'], + modules: [rootPath('ClientApp'), 'node_modules'] + }, + module: { + rules: [{ + enforce: 'pre', + test: /\.js$/, + loader: 'source-map-loader', + exclude: [ + rootPath('node_modules/rxjs'), + rootPath('node_modules/@angular') + ] + }, + { + test: /\.ts$/, + use: [{ + loader: 'awesome-typescript-loader', + query: { + sourceMap: false, + inlineSourceMap: true, + compilerOptions: { + removeComments: true + } + }, + }, + 'angular2-template-loader' + ], + exclude: [/\.e2e\.ts$/] + }, + { + test: /\.css$/, + loader: ['to-string-loader', 'css-loader'] + }, + { + test: /\.scss$/, + loader: ['raw-loader', 'sass-loader'] + }, + { + test: /\.html$/, + loader: 'raw-loader' + }, + { + enforce: 'post', + test: /\.(js|ts)$/, + loader: 'istanbul-instrumenter-loader', + options: { + esModules: true + }, + include: rootPath('ClientApp'), + exclude: [ + /ClientApp\\test/, + /\.(e2e|spec)\.ts$/, + /node_modules/ + ] + } + + ] + }, + plugins: [ + new ContextReplacementPlugin( + /** + * The (\\|\/) piece accounts for path separators in *nix and Windows + */ + /angular(\\|\/)core(\\|\/)@angular/, + rootPath('ClientApp'), // location of your src + { + /** + * your Angular Async Route paths relative to this root directory + */ + } + ), + new LoaderOptionsPlugin({ + debug: false, + options: { + /** + * legacy options go here + */ + } + }), + + ], + performance: { + hints: false + }, + + /** + * Include polyfills or mocks for various node stuff + * Description: Node configuration + * + * See: https://webpack.github.io/docs/configuration.html#node + */ + node: { + global: true, + process: false, + crypto: 'empty', + module: false, + clearImmediate: false, + setImmediate: false + } + + }; +} diff --git a/package.json b/package.json index 66b937c3..6072e16c 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,8 @@ "name": "angular4-aspnetcore-universal", "version": "1.0.0-rc3", "scripts": { - "test": "jest", - "test:watch": "npm run test -- --watch", + "test": "karma start ClientApp/test/karma.conf.js", + "test:watch": "npm run test -- --auto-watch --no-single-run", "test:ci": "npm run test -- --runInBand", "test:coverage": "npm run test -- --coverage", "build:dev": "npm run build:vendor && npm run build:webpack", @@ -11,19 +11,6 @@ "build:prod": "npm run build:vendor -- --env.prod && npm run build:webpack -- --env.prod", "build:vendor": "webpack --config webpack.config.vendor.js --progress --color" }, - "jest": { - "preset": "jest-preset-angular", - "setupTestFrameworkScriptFile": "./ClientApp/test/setupJest.ts", - "globals": { - "__TS_CONFIG__": "ClientApp/tsconfig.spec.json", - "__TRANSFORM_HTML__": true - }, - "coveragePathIgnorePatterns": [ - "/node_modules/", - "/ClientApp/test/.*.ts" - ], - "coverageDirectory": "coverage" - }, "dependencies": { "@angular/animations": "^4.3.0", "@angular/common": "^4.3.0", @@ -81,12 +68,19 @@ "@ngtools/webpack": "^1.3.0", "@types/chai": "^3.4.34", "@types/jasmine": "^2.5.37", - "@types/jest": "^19.2.3", "chai": "^3.5.0", "codelyzer": "^3.0.0", + "istanbul-instrumenter-loader": "^3.0.0", "jasmine-core": "^2.5.2", - "jest": "^20.0.0", - "jest-preset-angular": "^2.0.1", + "karma": "^1.7.1", + "karma-chai": "^0.1.0", + "karma-chrome-launcher": "^2.2.0", + "karma-coverage": "^1.1.1", + "karma-jasmine": "^1.1.0", + "karma-mocha-reporter": "^2.2.4", + "karma-remap-coverage": "^0.1.4", + "karma-sourcemap-loader": "^0.3.7", + "karma-webpack": "^2.0.3", "tslint": "^5.0.0" } }