storebound/tests/playwright-resolve.test.ts

28 lines
1.7 KiB
TypeScript

// A project's playwright is CommonJS: its chromium may come through the ESM import only under `default`. A playwright
// 1.57 project under a global Bun install gave undefined and "Cannot read properties of undefined (reading 'launch')".
import { expect, test } from 'bun:test';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { chromiumFor } from '../cli/shots/browser';
const fakePlaywright = (body: string): string => {
const dir = mkdtempSync(join(tmpdir(), 'sb-pw-'));
const pkg = join(dir, 'node_modules', 'playwright');
mkdirSync(pkg, { recursive: true });
writeFileSync(join(pkg, 'package.json'), JSON.stringify({ name: 'playwright', version: '1.57.0', main: 'index.js' }));
writeFileSync(join(pkg, 'index.js'), body);
writeFileSync(join(dir, 'package.json'), '{"name":"app","private":true}');
return dir;
};
test('chromium from a CommonJS playwright, named or under default; a wrong shape names the file', async () => {
const named = fakePlaywright('module.exports = { chromium: { launch() { return "ok"; } } };');
const viaDefault = fakePlaywright('const api = { chromium: { launch() { return "ok"; } } }; module.exports = { __esModule: true, default: api };');
const broken = fakePlaywright('module.exports = { firefox: {} };');
try {
expect(typeof (await chromiumFor(named)).launch).toBe('function');
expect(typeof (await chromiumFor(viaDefault)).launch).toBe('function');
await expect(chromiumFor(broken)).rejects.toThrow(/node_modules[\\/]playwright[\\/]index\.js has no chromium\.launch/);
} finally { for (const d of [named, viaDefault, broken]) rmSync(d, { recursive: true, force: true }); }
});