Skip to content

Commit 87d9511

Browse files
authored
chore: Add missing tests for dev mode CSP (#662)
1 parent 5d8efef commit 87d9511

File tree

2 files changed

+102
-1
lines changed

2 files changed

+102
-1
lines changed

Diff for: packages/wxt/src/core/utils/__tests__/manifest.test.ts

+84
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
fakeOptionsEntrypoint,
1111
fakePopupEntrypoint,
1212
fakeSidepanelEntrypoint,
13+
fakeWxtDevServer,
1314
setFakeWxt,
1415
} from '../testing/fake-objects';
1516
import { Manifest } from 'webextension-polyfill';
@@ -1496,6 +1497,89 @@ describe('Manifest Utils', () => {
14961497
expect(actual.host_permissions).toBeUndefined();
14971498
});
14981499
});
1500+
1501+
describe('Dev mode', () => {
1502+
it('should not add any code for production builds', async () => {
1503+
setFakeWxt({
1504+
config: {
1505+
command: 'build',
1506+
},
1507+
server: {
1508+
hostname: 'localhost',
1509+
port: 3000,
1510+
origin: 'http://localhost:3000',
1511+
},
1512+
});
1513+
const output = fakeBuildOutput();
1514+
const entrypoints: Entrypoint[] = [];
1515+
1516+
const { manifest: actual } = await generateManifest(
1517+
entrypoints,
1518+
output,
1519+
);
1520+
1521+
expect(actual.permissions).toBeUndefined();
1522+
expect(actual.content_security_policy).toBeUndefined();
1523+
});
1524+
1525+
it('should add required permissions for dev mode to function for MV2', async () => {
1526+
setFakeWxt({
1527+
config: {
1528+
command: 'serve',
1529+
manifestVersion: 2,
1530+
},
1531+
server: fakeWxtDevServer({
1532+
port: 3000,
1533+
hostname: 'localhost',
1534+
origin: 'http://localhost:3000',
1535+
}),
1536+
});
1537+
const output = fakeBuildOutput();
1538+
const entrypoints: Entrypoint[] = [];
1539+
1540+
const { manifest: actual } = await generateManifest(
1541+
entrypoints,
1542+
output,
1543+
);
1544+
1545+
expect(actual).toMatchObject({
1546+
content_security_policy:
1547+
"script-src 'self' http://localhost:3000; object-src 'self';",
1548+
permissions: ['http://localhost/*', 'tabs'],
1549+
});
1550+
});
1551+
1552+
it('should add required permissions for dev mode to function for MV3', async () => {
1553+
setFakeWxt({
1554+
config: {
1555+
command: 'serve',
1556+
manifestVersion: 3,
1557+
browser: 'chrome',
1558+
},
1559+
server: fakeWxtDevServer({
1560+
hostname: 'localhost',
1561+
port: 3000,
1562+
origin: 'http://localhost:3000',
1563+
}),
1564+
});
1565+
const output = fakeBuildOutput();
1566+
const entrypoints: Entrypoint[] = [];
1567+
1568+
const { manifest: actual } = await generateManifest(
1569+
entrypoints,
1570+
output,
1571+
);
1572+
1573+
expect(actual).toMatchObject({
1574+
content_security_policy: {
1575+
extension_pages:
1576+
"script-src 'self' 'wasm-unsafe-eval' http://localhost:3000; object-src 'self';",
1577+
},
1578+
host_permissions: ['http://localhost/*'],
1579+
permissions: ['tabs', 'scripting'],
1580+
});
1581+
});
1582+
});
14991583
});
15001584

15011585
describe('stripPathFromMatchPattern', () => {

Diff for: packages/wxt/src/core/utils/testing/fake-objects.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,27 @@ export const fakeWxt = fakeObjectCreator<Wxt>(() => ({
311311
logger: mock(),
312312
reloadConfig: vi.fn(),
313313
pm: mock(),
314-
server: faker.helpers.arrayElement([undefined, mock<WxtDevServer>()]),
314+
server: faker.helpers.arrayElement([undefined, fakeWxtDevServer()]),
315315
builder: mock(),
316316
}));
317317

318+
export const fakeWxtDevServer = fakeObjectCreator<WxtDevServer>(() => ({
319+
currentOutput: fakeBuildOutput(),
320+
hostname: 'localhost',
321+
origin: 'http://localhost:3000',
322+
port: 3000,
323+
reloadContentScript: vi.fn(),
324+
reloadExtension: vi.fn(),
325+
reloadPage: vi.fn(),
326+
restart: vi.fn(),
327+
restartBrowser: vi.fn(),
328+
start: vi.fn(),
329+
stop: vi.fn(),
330+
transformHtml: vi.fn(),
331+
watcher: mock(),
332+
ws: mock(),
333+
}));
334+
318335
export function setFakeWxt(overrides?: DeepPartial<Wxt>) {
319336
const wxt = fakeWxt(overrides);
320337
setWxtForTesting(wxt);

0 commit comments

Comments
 (0)