Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ vi.mock( 'node:worker_threads', () => ( {

describe( 'parallelWorker (worker defined in executeInParallel())', () => {
it( 'should execute a module from specified path and pass a package path and task options as arguments', async () => {
const postMessage = vi.mocked( parentPort ).postMessage;

await import( '../../lib/utils/parallelworker.js' );

// It's needed because `parallelworker` does not export anything. Instead, it processes
// an asynchronous loop. We must wait until the current JavaScript loop ends. Adding a new promise at the end
// forces it.
await new Promise( resolve => {
setTimeout( resolve, 100 );
} );
// Importing the worker starts async work, so wait until it reports completion.
await vi.waitFor( () => {
expect( postMessage ).toHaveBeenCalledTimes( 2 );
expect( postMessage ).toHaveBeenCalledWith( 'done:package' );
}, { timeout: 1000 } );

expect( vi.mocked( parentPort ).postMessage ).toHaveBeenCalledTimes( 2 );
expect( vi.mocked( parentPort ).postMessage ).toHaveBeenCalledWith( 'done:package' );
expect( vi.mocked( virtual ) ).toHaveBeenCalledTimes( 2 );
expect( vi.mocked( virtual ) ).toHaveBeenCalledWith( '/home/ckeditor/packages/a', taskOptions );
expect( vi.mocked( virtual ) ).toHaveBeenCalledWith( '/home/ckeditor/packages/b', taskOptions );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import getWebpackConfigForAutomatedTests from '../../../lib/utils/automated-tests/getwebpackconfig.js';
import getKarmaConfig from '../../../lib/utils/automated-tests/getkarmaconfig.js';

vi.mock( 'node:path', async () => {
const originalModule = await vi.importActual( 'node:path' );

return {
default: {
join: vi.fn( ( ...chunks ) => chunks.join( '/' ) ),
dirname: vi.fn(),
resolve: originalModule.resolve
}
};
} );

vi.mock( '../../../lib/utils/automated-tests/getwebpackconfig.js' );

vi.mock( '../../../lib/utils/resolve-path.js', () => ( {
Expand Down Expand Up @@ -119,7 +107,7 @@ describe( 'getKarmaConfig()', () => {
expect( karmaConfig.files ).toEqual( expect.arrayContaining( [
'workspace/entry-file.js',
expect.objectContaining( {
pattern: expect.stringContaining( 'ckeditor5-utils/tests/_assets/**/*' )
pattern: expect.stringContaining( path.join( 'ckeditor5-utils', 'tests', '_assets', '**', '*' ) )
} )
] ) );
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe( 'copyAssets()', () => {
const buildDir = '/build';
copyAssets( buildDir );

expect( mkdirSync ).toHaveBeenCalledWith( buildDir + '/assets', expect.any( Object ) );
expect( mkdirSync ).toHaveBeenCalledWith( path.join( buildDir, 'assets' ), expect.any( Object ) );
expect( copyFileSync ).toHaveBeenCalledTimes( ASSETS.length + 1 );

const scriptPath = path.resolve( import.meta.dirname, '..', '..', '..', 'lib', 'utils', 'manual-tests' );
Expand All @@ -40,7 +40,7 @@ describe( 'copyAssets()', () => {
expect( copyFileSync ).toHaveBeenNthCalledWith(
index + 1,
path.resolve( scriptPath, assetPath ),
path.resolve( buildDir, 'assets', path.basename( assetPath ) )
path.join( buildDir, 'assets', path.basename( assetPath ) )
);
}
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@ describe( 'createManualTestServer()', () => {
info: loggerStub
} );

vi.spyOn( process, 'platform', 'get' ).mockReturnValue( 'linux' );

vi.spyOn( http, 'createServer' ).mockImplementation( ( ...theArgs ) => {
server = createServer( ...theArgs );
servers.push( server );

vi.spyOn( server, 'listen' );
vi.spyOn( server, 'listen' ).mockImplementation( ( _port, callback ) => {
process.nextTick( callback );

return server;
} );

return server;
} );
Expand Down Expand Up @@ -121,11 +127,28 @@ describe( 'createManualTestServer()', () => {
} );

it( 'should try next port when the requested port is in use', async () => {
// Occupy the port first.
const blockingServer = http.createServer.getMockImplementation()();
const originalCreateServer = http.createServer.getMockImplementation();
let attempt = 0;

vi.mocked( http.createServer ).mockImplementation( ( ...args ) => {
const currentServer = originalCreateServer( ...args );

await new Promise( resolve => {
blockingServer.listen( 49555, resolve );
attempt++;

if ( attempt === 1 ) {
vi.mocked( currentServer.listen ).mockImplementation( () => {
process.nextTick( () => {
const error = new Error( 'EADDRINUSE: port is already in use' );
error.code = 'EADDRINUSE';

currentServer.emit( 'error', error );
} );

return currentServer;
} );
}

return currentServer;
} );

createManualTestServer( 'workspace/build/.manual-tests', 49555 );
Expand All @@ -134,9 +157,9 @@ describe( 'createManualTestServer()', () => {
expect( loggerStub ).toHaveBeenCalledWith( '[Server] Server running at http://localhost:49556/' );
} );

expect( servers[ 0 ].listen ).toHaveBeenCalledWith( 49555, expect.any( Function ) );
expect( servers[ 1 ].listen ).toHaveBeenCalledWith( 49556, expect.any( Function ) );
expect( loggerStub ).toHaveBeenCalledWith( '[Server] Port 49555 is in use, trying 49556...' );

blockingServer.close();
} );

it( 'should reject when a non-EADDRINUSE error occurs', async () => {
Expand All @@ -160,7 +183,7 @@ describe( 'createManualTestServer()', () => {

describe( 'request handler', () => {
beforeEach( async () => {
createManualTestServer( 'workspace/build/.manual-tests', 49800 );
Comment thread
psmyrek marked this conversation as resolved.
createManualTestServer( 'workspace/build/.manual-tests', 49700 );

await vi.waitFor( () => {
expect( loggerStub ).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe( 'serveTranslations()', () => {
let cwdSpy;

beforeEach( () => {
cwdSpy = vi.spyOn( process, 'cwd' ).mockReturnValue( '/project' );
cwdSpy = vi.spyOn( process, 'cwd' ).mockReturnValue( path.join( path.sep, 'project' ) );
vi.mocked( fs.existsSync ).mockReturnValue( false );
vi.mocked( fs.readFileSync ).mockReset();
vi.mocked( rimraf.sync ).mockReset();
Expand Down Expand Up @@ -141,7 +141,7 @@ describe( 'serveTranslations()', () => {
expect( module.loaders[ 0 ].options.translateSource( 'SOURCE', 'file.ts' ) ).to.equal( 'translated:file.ts' );
expect( translationService.translateSource ).toHaveBeenCalledWith( 'SOURCE', 'file.ts' );
expect(
translationService.loadPackage.mock.calls.some( call => String( call[ 0 ] ).includes( 'packages/ckeditor5-foo/' ) )
translationService.loadPackage.mock.calls.some( call => /packages[/\\]ckeditor5-foo[/\\]/.test( String( call[ 0 ] ) ) )
).to.equal( true );

compilation.hooks.optimizeChunkAssets.call( [ { files: [ 'main.js', 'other.js' ] } ] );
Expand Down