Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@angular-devkit/build-angular): support karma version 6.x #19784

Merged
merged 3 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"jest-worker": "26.6.2",
"jquery": "^3.3.1",
"jsonc-parser": "3.0.0",
"karma": "~5.2.0",
"karma": "~6.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/angular_devkit/build_angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"peerDependencies": {
"@angular/compiler-cli": "^11.0.0 || ^11.1.0-next",
"@angular/localize": "^11.0.0 || ^11.1.0-next",
"karma": "^5.2.0",
"karma": "^5.2.0 || ^6.0.0",
"ng-packagr": "^11.0.0 || ^11.1.0-next",
"protractor": "^7.0.0",
"tslint": "^6.1.0",
Expand Down
51 changes: 21 additions & 30 deletions packages/angular_devkit/build_angular/src/webpack/plugins/karma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function addKarmaFiles(files: any[], newFiles: any[], prepend = false) {
}
}

const init: any = (config: any, emitter: any, customFileHandlers: any) => {
const init: any = (config: any, emitter: any) => {
if (!config.buildWebpack) {
throw new Error(`The '@angular-devkit/build-angular/plugins/karma' karma plugin is meant to` +
` be used from within Angular CLI and will not work correctly outside of it.`
Expand Down Expand Up @@ -219,39 +219,13 @@ const init: any = (config: any, emitter: any, customFileHandlers: any) => {

webpackMiddleware = new webpackDevMiddleware(compiler, webpackMiddlewareConfig);

// Forward requests to webpack server.
customFileHandlers.push({
urlRegex: new RegExp(`\\/${KARMA_APPLICATION_PATH}\\/.*`),
handler: function handler(req: any, res: any) {
webpackMiddleware(req, res, function () {
// Ensure script and style bundles are served.
// They are mentioned in the custom karma context page and we don't want them to 404.
const alwaysServe = [
`/${KARMA_APPLICATION_PATH}/runtime.js`,
`/${KARMA_APPLICATION_PATH}/polyfills.js`,
`/${KARMA_APPLICATION_PATH}/polyfills-es5.js`,
`/${KARMA_APPLICATION_PATH}/scripts.js`,
`/${KARMA_APPLICATION_PATH}/styles.js`,
`/${KARMA_APPLICATION_PATH}/vendor.js`,
];
if (alwaysServe.indexOf(req.url) != -1) {
res.statusCode = 200;
res.end();
} else {
res.statusCode = 404;
res.end('Not found');
}
});
}
});

emitter.on('exit', (done: any) => {
webpackMiddleware.close();
done();
});
};

init.$inject = ['config', 'emitter', 'customFileHandlers'];
init.$inject = ['config', 'emitter'];

// Block requests until the Webpack compilation is done.
function requestBlocker() {
Expand Down Expand Up @@ -327,8 +301,25 @@ sourceMapReporter.$inject = ['baseReporterDecorator', 'config'];
function fallbackMiddleware() {
return function (request: http.IncomingMessage, response: http.ServerResponse, next: () => void) {
if (webpackMiddleware) {
request.url = '/' + KARMA_APPLICATION_PATH + request.url;
webpackMiddleware(request, response, next);
if (request.url && !new RegExp(`\\/${KARMA_APPLICATION_PATH}\\/.*`).test(request.url)) {
request.url = '/' + KARMA_APPLICATION_PATH + request.url;
}
webpackMiddleware(request, response, () => {
const alwaysServe = [
`/${KARMA_APPLICATION_PATH}/runtime.js`,
`/${KARMA_APPLICATION_PATH}/polyfills.js`,
`/${KARMA_APPLICATION_PATH}/polyfills-es5.js`,
`/${KARMA_APPLICATION_PATH}/scripts.js`,
`/${KARMA_APPLICATION_PATH}/styles.js`,
`/${KARMA_APPLICATION_PATH}/vendor.js`,
];
if (request.url && alwaysServe.includes(request.url)) {
response.statusCode = 200;
response.end();
} else {
next();
}
});
} else {
next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"codelyzer": "^6.0.0",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.2.0",
"karma": "~6.0.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
Expand Down
11 changes: 11 additions & 0 deletions tests/legacy-cli/e2e/tests/test/test-karma-5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ng } from '../../utils/process';
import { installPackage } from '../../utils/packages';

/**
* Ensure karma 5.2 continues to function
*/
export default async function () {
await installPackage('karma@5.2');

await ng('test', '--watch=false');
}
Loading