Skip to content

Commit

Permalink
Merge pull request #763 from ckeditor/ck/11337
Browse files Browse the repository at this point in the history
Other: Bumped Karma test runner to v6.x. Closes ckeditor/ckeditor5#11337.
  • Loading branch information
pomek authored Mar 18, 2022
2 parents f25b9e6 + 363c700 commit bd5e23c
Show file tree
Hide file tree
Showing 4 changed files with 479 additions and 530 deletions.
62 changes: 44 additions & 18 deletions packages/ckeditor5-dev-tests/lib/tasks/runautomatedtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const glob = require( 'glob' );
const minimatch = require( 'minimatch' );
const mkdirp = require( 'mkdirp' );
const karmaLogger = require( 'karma/lib/logger.js' );
const { Server: KarmaServer } = require( 'karma' );
const karma = require( 'karma' );
const transformFileOptionToTestGlob = require( '../utils/transformfileoptiontotestglob' );

// Glob patterns that should be ignored. It means if a specified test file is located under path
Expand Down Expand Up @@ -120,22 +120,7 @@ function createEntryFile( globPatterns, production ) {
}

if ( production ) {
entryFileContent.unshift( `
const originalWarn = console.warn;
window.production = true;
beforeEach( () => {
Object.keys( console )
.filter( methodOrProperty => typeof console[ methodOrProperty ] === 'function' )
.forEach( method => {
console[ method ] = ( ...data ) => {
originalWarn( 'Detected \`console.' + method + '()\`:', ...data );
throw new Error( 'Detected \`console.' + method + '()\`:' );
}
} );
} );
` );
entryFileContent.unshift( assertConsoleUsageToThrowErrors() );
}

fs.writeFileSync( ENTRY_FILE_PATH, entryFileContent.join( '\n' ) + '\n' );
Expand All @@ -149,11 +134,52 @@ beforeEach( () => {
fs.utimesSync( ENTRY_FILE_PATH, then, then );
}

function assertConsoleUsageToThrowErrors() {
const functionString = makeConsoleUsageToThrowErrors.toString();

return functionString
// Extract the body of the function from between the opening and closing braces.
.substring(
functionString.indexOf( '{' ) + 1,
functionString.lastIndexOf( '}' )
)
// Remove the leading and trailing new lines.
.trim()
// Decrease indent for the extracted function body by one tab.
.replace( /^\t/gm, '' );
}

/* eslint-disable no-undef,mocha/no-top-level-hooks */
function makeConsoleUsageToThrowErrors() {
const originalWarn = console.warn;

window.production = true;

// Important: Do not remove the comment below. It is used to assert this function insertion in tests.
//
// Make using any method from the console to fail.
beforeEach( () => {
Object.keys( console )
.filter( methodOrProperty => typeof console[ methodOrProperty ] === 'function' )
.forEach( method => {
console[ method ] = ( ...data ) => {
originalWarn( 'Detected `console.' + method + '()`:', ...data );
throw new Error( 'Detected `console.' + method + '()`:' );
};
} );
} );
}
/* eslint-enable no-undef,mocha/no-top-level-hooks */

function runKarma( options ) {
return new Promise( ( resolve, reject ) => {
const KarmaServer = karma.Server;
const parseConfig = karma.config.parseConfig;

const config = getKarmaConfig( options );
const parsedConfig = parseConfig( null, config, { throwErrors: true } );

const server = new KarmaServer( config, exitCode => {
const server = new KarmaServer( parsedConfig, exitCode => {
if ( exitCode === 0 ) {
resolve();
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/ckeditor5-dev-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"glob": "^7.1.6",
"istanbul-instrumenter-loader": "^3.0.1",
"js-beautify": "^1.11.0",
"karma": "^5.0.9",
"karma": "^6.3.17",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.3",
Expand Down
61 changes: 41 additions & 20 deletions packages/ckeditor5-dev-tests/tests/tasks/runautomatedtests.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@ const path = require( 'path' );
describe( 'runAutomatedTests', () => {
let sandbox, stubs, runAutomatedTests, karmaServerCallback;

const codeMakingConsoleUseThrowErrors = `
const originalWarn = console.warn;
window.production = true;
beforeEach( () => {
Object.keys( console )
.filter( methodOrProperty => typeof console[ methodOrProperty ] === 'function' )
.forEach( method => {
console[ method ] = ( ...data ) => {
originalWarn( 'Detected \`console.' + method + '()\`:', ...data );
throw new Error( 'Detected \`console.' + method + '()\`:' );
}
} );
} );
`;

beforeEach( () => {
karmaServerCallback = null;
sandbox = sinon.createSandbox();
Expand Down Expand Up @@ -74,7 +57,10 @@ beforeEach( () => {
}
},
karmaServerOn: sandbox.stub(),
karmaServerStart: sandbox.stub()
karmaServerStart: sandbox.stub(),
config: {
parseConfig: sandbox.stub()
}
},
getKarmaConfig: sandbox.stub(),
transformFileOptionToTestGlob: sandbox.stub()
Expand Down Expand Up @@ -202,6 +188,41 @@ beforeEach( () => {
);
} );

it( 'throws when Karma config parser throws', () => {
const options = {
files: [
'basic-styles'
],
production: true
};

stubs.fs.readdirSync.returns( [] );

stubs.transformFileOptionToTestGlob.returns( [
'/workspace/packages/ckeditor5-basic-styles/tests/**/*.js',
'/workspace/packages/ckeditor-basic-styles/tests/**/*.js'
] );

stubs.glob.sync.onFirstCall().returns( [
'/workspace/packages/ckeditor5-basic-styles/tests/bold.js',
'/workspace/packages/ckeditor5-basic-styles/tests/italic.js'
] );

stubs.glob.sync.onSecondCall().returns( [] );

stubs.karma.config.parseConfig.throws( new Error( 'Example error from Karma config parser.' ) );

return runAutomatedTests( options )
.then(
() => {
throw new Error( 'Expected to be rejected.' );
},
err => {
expect( err.message ).to.equal( 'Example error from Karma config parser.' );
}
);
} );

it( 'should warn when the `production` option is set to `false`', () => {
const options = {
files: [
Expand Down Expand Up @@ -270,7 +291,7 @@ beforeEach( () => {

return runAutomatedTests( options )
.then( () => {
expect( stubs.fs.writeFileSync.firstCall.args[ 1 ] ).to.not.include( codeMakingConsoleUseThrowErrors );
expect( stubs.fs.writeFileSync.firstCall.args[ 1 ] ).to.not.include( '// Make using any method from the console to fail.' );
} );
} );

Expand Down Expand Up @@ -302,7 +323,7 @@ beforeEach( () => {

return runAutomatedTests( options )
.then( () => {
expect( stubs.fs.writeFileSync.firstCall.args[ 1 ] ).to.include( codeMakingConsoleUseThrowErrors );
expect( stubs.fs.writeFileSync.firstCall.args[ 1 ] ).to.include( '// Make using any method from the console to fail.' );
} );
} );

Expand Down
Loading

0 comments on commit bd5e23c

Please sign in to comment.