Skip to content

Commit fe61de7

Browse files
committed
step 1 - no browser & preview if not test on page
1 parent fc3f670 commit fe61de7

File tree

11 files changed

+45
-45
lines changed

11 files changed

+45
-45
lines changed

.changeset/dull-islands-lead.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'sv': patch
3+
---
4+
5+
chore(cli): speedup internal tests

packages/addons/_tests/_setup/suite.ts

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
type CreateProject,
1111
type ProjectVariant
1212
} from 'sv/testing';
13-
import { chromium, type Browser, type Page } from '@playwright/test';
13+
import { chromium, type Browser, type BrowserContext, type Page } from '@playwright/test';
1414

1515
const cwd = vitest.inject('testDir');
1616
const templatesDir = vitest.inject('templatesDir');
@@ -21,18 +21,25 @@ type Fixtures<Addons extends AddonMap> = {
2121
run(variant: ProjectVariant, options: OptionMap<Addons>): Promise<string>;
2222
};
2323

24-
export function setupTest<Addons extends AddonMap>(addons: Addons) {
24+
export function setupTest<Addons extends AddonMap>(
25+
addons: Addons,
26+
options?: { skipBrowser?: boolean }
27+
) {
2528
const test = vitest.test.extend<Fixtures<Addons>>({} as any);
2629

30+
const withBrowser = !options?.skipBrowser;
31+
2732
let create: CreateProject;
2833
let browser: Browser;
2934

30-
vitest.beforeAll(async () => {
31-
browser = await chromium.launch();
32-
return async () => {
33-
await browser.close();
34-
};
35-
});
35+
if (withBrowser) {
36+
vitest.beforeAll(async () => {
37+
browser = await chromium.launch();
38+
return async () => {
39+
await browser.close();
40+
};
41+
});
42+
}
3643

3744
vitest.beforeAll(({ name }) => {
3845
const testName = path.dirname(name).split('/').at(-1)!;
@@ -59,8 +66,11 @@ export function setupTest<Addons extends AddonMap>(addons: Addons) {
5966

6067
// runs before each test case
6168
vitest.beforeEach<Fixtures<Addons>>(async (ctx) => {
62-
const browserCtx = await browser.newContext();
63-
ctx.page = await browserCtx.newPage();
69+
let browserCtx: BrowserContext;
70+
if (withBrowser) {
71+
browserCtx = await browser.newContext();
72+
ctx.page = await browserCtx.newPage();
73+
}
6474
ctx.run = async (variant, options) => {
6575
const cwd = create({ testId: ctx.task.id, variant });
6676

@@ -81,7 +91,9 @@ export function setupTest<Addons extends AddonMap>(addons: Addons) {
8191
};
8292

8393
return async () => {
84-
await browserCtx.close();
94+
if (withBrowser) {
95+
await browserCtx.close();
96+
}
8597
// ...other tear downs
8698
};
8799
});
@@ -91,7 +103,7 @@ export function setupTest<Addons extends AddonMap>(addons: Addons) {
91103

92104
type PrepareServerOptions = {
93105
cwd: string;
94-
page: Page;
106+
page: Page | null;
95107
previewCommand?: string;
96108
buildCommand?: string;
97109
installCommand?: string;
@@ -116,6 +128,10 @@ async function prepareServer(
116128
// build project
117129
if (buildCommand) execSync(buildCommand, { cwd, stdio: 'pipe' });
118130

131+
if (!page) {
132+
return { url: '', close: () => Promise.resolve() };
133+
}
134+
119135
// start preview server
120136
const { url, close } = await startPreview({ cwd, command: previewCommand });
121137

packages/addons/_tests/all-addons/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const defaultOptions = officialAddons.reduce<OptionMap<typeof addons>>((options,
1616
return options;
1717
}, {});
1818

19-
const { test, variants, prepareServer } = setupTest(addons);
19+
const { test, variants, prepareServer } = setupTest(addons, { skipBrowser: true });
2020

2121
const kitOnly = variants.filter((v) => v.startsWith('kit'));
2222
test.concurrent.for(kitOnly)('run all addons - %s', async (variant, { page, ...ctx }) => {

packages/addons/_tests/devtools-json/test.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import devtoolsJson from '../../devtools-json/index.ts';
44
import fs from 'node:fs';
55
import path from 'node:path';
66

7-
const { test, variants, prepareServer } = setupTest({ devtoolsJson } as {
8-
devtoolsJson?: typeof devtoolsJson;
9-
});
7+
const { test, variants, prepareServer } = setupTest({ devtoolsJson }, { skipBrowser: true });
108

119
test.concurrent.for(variants)('default - %s', async (variant, { page, ...ctx }) => {
1210
const cwd = await ctx.run(variant, { devtoolsJson: {} });
@@ -26,25 +24,3 @@ test.concurrent.for(variants)('default - %s', async (variant, { page, ...ctx })
2624
// Check if it's called
2725
expect(viteContent).toContain(`devtoolsJson()`);
2826
});
29-
30-
test.concurrent.for(variants)(
31-
'without selecting the addon specifically - %s',
32-
async (variant, { page, ...ctx }) => {
33-
const cwd = await ctx.run(variant, {});
34-
35-
const { close } = await prepareServer({ cwd, page });
36-
// kill server process when we're done
37-
ctx.onTestFinished(async () => await close());
38-
39-
const ext = variant.includes('ts') ? 'ts' : 'js';
40-
const viteFile = path.resolve(cwd, `vite.config.${ext}`);
41-
const viteContent = fs.readFileSync(viteFile, 'utf8');
42-
43-
// Check if we have the import part
44-
expect(viteContent).toContain(`import devtoolsJson from`);
45-
expect(viteContent).toContain(`vite-plugin-devtools-json`);
46-
47-
// Check if it's called
48-
expect(viteContent).toContain(`devtoolsJson()`);
49-
}
50-
);

packages/addons/_tests/eslint/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { expect } from '@playwright/test';
55
import { setupTest } from '../_setup/suite.ts';
66
import eslint from '../../eslint/index.ts';
77

8-
const { test, variants, prepareServer } = setupTest({ eslint });
8+
const { test, variants, prepareServer } = setupTest({ eslint }, { skipBrowser: true });
99

1010
test.concurrent.for(variants)('core - %s', async (variant, { page, ...ctx }) => {
1111
const cwd = await ctx.run(variant, { eslint: {} });

packages/addons/_tests/lucia/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { setupTest } from '../_setup/suite.ts';
33
import lucia from '../../lucia/index.ts';
44
import drizzle from '../../drizzle/index.ts';
55

6-
const { test, variants, prepareServer } = setupTest({ drizzle, lucia });
6+
const { test, variants, prepareServer } = setupTest({ drizzle, lucia }, { skipBrowser: true });
77

88
const kitOnly = variants.filter((v) => v.includes('kit'));
99
test.concurrent.for(kitOnly)('core - %s', async (variant, { page, ...ctx }) => {

packages/addons/_tests/paraglide/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from '@playwright/test';
22
import { setupTest } from '../_setup/suite.ts';
33
import paraglide from '../../paraglide/index.ts';
44

5-
const { test, variants, prepareServer } = setupTest({ paraglide });
5+
const { test, variants, prepareServer } = setupTest({ paraglide }, { skipBrowser: true });
66

77
const kitOnly = variants.filter((v) => v.includes('kit'));
88
test.concurrent.for(kitOnly)('core - %s', async (variant, { page, ...ctx }) => {

packages/addons/_tests/playwright/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from '@playwright/test';
22
import { setupTest } from '../_setup/suite.ts';
33
import playwright from '../../playwright/index.ts';
44

5-
const { test, variants, prepareServer } = setupTest({ playwright });
5+
const { test, variants, prepareServer } = setupTest({ playwright }, { skipBrowser: true });
66

77
test.concurrent.for(variants)('core - %s', async (variant, { page, ...ctx }) => {
88
const cwd = await ctx.run(variant, { playwright: {} });

packages/addons/_tests/prettier/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { expect } from '@playwright/test';
55
import { setupTest } from '../_setup/suite.ts';
66
import prettier from '../../prettier/index.ts';
77

8-
const { test, variants, prepareServer } = setupTest({ prettier });
8+
const { test, variants, prepareServer } = setupTest({ prettier }, { skipBrowser: true });
99

1010
test.concurrent.for(variants)('core - %s', async (variant, { page, ...ctx }) => {
1111
const cwd = await ctx.run(variant, { prettier: {} });

packages/addons/_tests/sveltekit-adapter/test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import sveltekitAdapter from '../../sveltekit-adapter/index.ts';
55
import { setupTest } from '../_setup/suite.ts';
66

77
const addonId = sveltekitAdapter.id;
8-
const { test, variants, prepareServer } = setupTest({ [addonId]: sveltekitAdapter });
8+
const { test, variants, prepareServer } = setupTest(
9+
{ [addonId]: sveltekitAdapter },
10+
{ skipBrowser: true }
11+
);
912

1013
const kitOnly = variants.filter((v) => v.includes('kit'));
1114
test.concurrent.for(kitOnly)('core - %s', async (variant, { page, ...ctx }) => {

0 commit comments

Comments
 (0)