Skip to content

Commit

Permalink
test: dev
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKreil committed Feb 22, 2024
1 parent c546135 commit baee68e
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/dev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable @typescript-eslint/naming-convention */
import { jest } from '@jest/globals';

jest.spyOn(process, 'exit').mockImplementationOnce(() => {
throw new Error('process.exit() was called.');
});
console.error = jest.fn().mockReturnValue(undefined);

// Import all necessary mocks
const { File, FileSystem } = await import('./lib/file_system');

const { mockProgress } = await import('./lib/__mocks__/progress');
jest.unstable_mockModule('./lib/progress', () => mockProgress);
const progress = (await import('./lib/progress')).default;

const { mockServer } = await import('./lib/__mocks__/server');
jest.unstable_mockModule('./lib/server', () => mockServer);
const { Server } = await import('./lib/server');

const { mockFrontend } = await import('./lib/__mocks__/frontend');
jest.unstable_mockModule('./lib/frontend', () => mockFrontend);
const { Frontend, loadFrontendConfigs } = await import('./lib/frontend');

const { mockAssets } = await import('./lib/__mocks__/assets');
jest.unstable_mockModule('./lib/assets', () => mockAssets);
const { getAssets } = await import('./lib/assets');

describe('build process', () => {
beforeEach(() => {
// Clear mocks before each test
jest.clearAllMocks();

// Setup default mock implementations or return values
jest.mocked(loadFrontendConfigs).mockReturnValue([{ name: 'frontend', dev: {}, include: ['frontend'] }]);
});

it('prepares and starts the server for the specified frontend', async () => {
// Mock command line arguments (assuming frontendName is "frontend")
process.argv[2] = 'frontend';

// Import or require the module here if necessary, ensuring mocks are set up beforehand
// For dynamic import or to ensure fresh import, consider using jest.isolateModules()
await jest.isolateModulesAsync(async () => {
await import('./dev');
});

expect(progress.disableAnsi).toHaveBeenCalled();
expect(progress.setHeader).toHaveBeenCalledWith('Preparing Server');
expect(getAssets).toHaveBeenCalled();
expect(loadFrontendConfigs).toHaveBeenCalled();
expect(Frontend).toHaveBeenCalledWith(
new FileSystem(new Map([['index.html', new File('index.html', 42, Buffer.from('file content'))]])),
{ dev: {}, include: ['frontend'], name: 'frontend' },
expect.any(String),
);
expect(Server).toHaveBeenCalled();
expect(progress.finish).toHaveBeenCalled();

// Verify the server started with the correct configuration
// @ts-expect-error too lazy
const serverInstance = new Server();
expect(serverInstance.start).toHaveBeenCalled();
});

it('exits the process if no frontend name is provided', async () => {
process.argv.length = 2; // Simulate missing frontend name

await expect(jest.isolateModulesAsync(async () => {
await import('./dev');
})).rejects.toThrow('process.exit() was called.');

expect(process.exit).toHaveBeenCalledWith(1);
expect(console.error).toHaveBeenCalledWith(expect.stringContaining('set a frontend name as first argument'));
});
});
15 changes: 15 additions & 0 deletions src/lib/__mocks__/assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */
/* eslint-disable @typescript-eslint/require-await */
/* eslint-disable @typescript-eslint/no-empty-function */

import { jest } from '@jest/globals';
import PromiseFunction from '../async';
import type { FileSystem } from '../file_system';

export const mockAssets: jest.Mocked<typeof import('../assets')> = {
getAssets: jest.fn((fileSystem: FileSystem): PromiseFunction => {
return PromiseFunction.single(async () => { }, async () => {
fileSystem.addFile('index.html', 42, Buffer.from('file content'));
});
}),
};
21 changes: 21 additions & 0 deletions src/lib/__mocks__/frontend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/naming-convention */

import { jest } from '@jest/globals';
import PromiseFunction from '../async';

const originalModule = await import('../frontend');

export const mockFrontend = {
generateFrontends: jest.fn((): PromiseFunction => {
return PromiseFunction.single(async () => { }, async () => { });
}),
loadFrontendConfigs: jest.fn(originalModule.loadFrontendConfigs),
Frontend: jest.fn(() => {
return {
enterWatchMode: jest.fn().mockReturnValue(undefined),
//start: jest.fn().mockReturnValue(Promise.resolve(undefined)),
};
}),
} as unknown as jest.Mocked<typeof import('../frontend')>;
15 changes: 15 additions & 0 deletions src/lib/__mocks__/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable @typescript-eslint/consistent-type-imports */
/* eslint-disable @typescript-eslint/naming-convention */

import { jest } from '@jest/globals';

const originalModule = await import('../server');

const serverInstance = {
start: jest.fn().mockReturnValue(Promise.resolve(undefined)),
};

export const mockServer = {
parseDevConfig: jest.fn(originalModule.parseDevConfig),
Server: jest.fn(() => serverInstance),
} as unknown as jest.Mocked<typeof import('../server')>;

0 comments on commit baee68e

Please sign in to comment.