All files / blong-browser/src/playwright config.ts

97.91% Statements 141/144
20% Branches 3/15
100% Functions 2/2
97.91% Lines 141/144

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 1451x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x       5x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x  
/**
 * Shared Playwright configuration for blong suites.
 *
 * Provides sensible defaults (test dir, viewport, reporter, webServer)
 * so suite-level `playwright.config.ts` files stay minimal.
 *
 * Usage:
 * ```ts
 * // playwright.config.ts
 * import {defineBlongConfig} from '@feasibleone/blong-browser/playwright/config';
 * export default defineBlongConfig();
 * ```
 *
 * Include tests from realm packages (in addition to local `./test/`):
 * ```ts
 * export default defineBlongConfig({
 *     realmPackages: ['@feasibleone/blong-marine'],
 * });
 * ```
 *
 * Override any setting via the options parameter:
 * ```ts
 * export default defineBlongConfig({
 *     timeout: 60_000,
 *     use: {blongPermissions: false},
 * });
 * ```
 */
import {defineConfig, type PlaywrightTestConfig, type Project} from '@playwright/test';
import {createRequire} from 'node:module';
import * as os from 'node:os';
import {dirname} from 'node:path';
import type {IBlongTestOptions} from '../playwright.js';
 
type BlongConfig = PlaywrightTestConfig<IBlongTestOptions> & {
    /**
     * npm package names whose `test/` directories should be included.
     * Each realm runs as a separate Playwright project.
     *
     * Adding a realm to a suite:
     * ```ts
     * realmPackages: ['@feasibleone/blong-marine', '@feasibleone/my-realm'],
     * ```
     */
    realmPackages?: string[];
};
 
function resolveRealmTestDir(packageName: string): string | null {
    const require = createRequire(import.meta.url);
    try {
        const pkgPath = require.resolve(`${packageName}/package.json`);
        return dirname(pkgPath) + '/test';
    } catch {
        console.warn(`[blong-browser/playwright] Could not resolve test dir for ${packageName}`);
        return null;
    }
}
 
export function defineBlongConfig(
    overrides: BlongConfig = {},
): ReturnType<typeof defineConfig<IBlongTestOptions>> {
    const {
        use,
        webServer,
        reporter,
        realmPackages,
        projects: projectsOverride,
        ...rest
    } = overrides;
 
    // Build projects: local test dir + one project per realm package
    const realmProjects: Project<IBlongTestOptions>[] = (realmPackages ?? []).flatMap(pkg => {
        const testDir = resolveRealmTestDir(pkg);
        if (!testDir) return [];
        const name = pkg.replace(/^@[^/]+\//, ''); // strip scope
        return [{name, testDir, testMatch: '**/*.play.ts'}];
    });
 
    // If realm projects exist, the default project also needs explicit testDir
    const defaultProject: Project<IBlongTestOptions> | undefined =
        realmProjects.length > 0
            ? {name: 'suite', testDir: './test', testMatch: '**/*.play.ts'}
            : undefined;
 
    const projects =
        projectsOverride ??
        (realmProjects.length > 0 ? [defaultProject!, ...realmProjects] : undefined);
 
    return defineConfig<IBlongTestOptions>({
        testDir: './test',
        testMatch: '**/*.play.ts',
        timeout: 30_000,
        retries: 1,
        use: {
            baseURL: 'http://localhost:5173',
            colorScheme: 'dark',
            viewport: {width: 1600, height: 900},
            trace: 'retain-on-failure',
            screenshot: 'off',
            blongUsername: 'testAdmin',
            blongPassword: 'testPassword',
            ...use,
        },
        expect: {
            toHaveScreenshot: {maxDiffPixelRatio: 0.01},
        },
        outputDir: '.playwright/results',
        reporter: reporter ?? [
            [process.env.CI ? 'list' : 'list'],
            ['html', {open: 'never', outputFolder: '.playwright/report'}],
            [
                'allure-playwright',
                {
                    resultsDir: 'allure-results',
                    environmentInfo: {
                        framework: 'blong',
                        node_version: process.version,
                        os_platform: os.platform(),
                    },
                },
            ],
        ],
        webServer: webServer ?? [
            {
                command: process.env.CI
                    ? 'node --run blong -- microservice integration dev playwright ci'
                    : 'node --run blong-watch --  microservice integration dev playwright',
                port: 8080,
                reuseExistingServer: !process.env.CI,
                stdout: 'pipe',
                timeout: 60_000,
            },
            {
                command: process.env.CI ? 'node --run dev -- --force' : 'node --run dev',
                url: 'http://localhost:5173',
                reuseExistingServer: !process.env.CI,
                stdout: 'pipe',
                timeout: 30_000,
            },
        ],
        ...(projects ? {projects} : {}),
        ...rest,
    });
}