Skip to content

Commit a7cf68b

Browse files
authored
fix: default community addon test (#1033)
1 parent 61c533a commit a7cf68b

3 files changed

Lines changed: 30 additions & 15 deletions

File tree

packages/sv/src/cli/tests/snapshots/@my-org/sv/tests/addon.test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const browser = false;
1010
const { test, prepareServer, testCases } = setupTest(
1111
{ addon },
1212
{
13-
kinds: [{ type: 'default', options: { addon: { who: 'you' } } }],
13+
kinds: [{ type: 'default', options: { [addon.id]: { who: 'you' } } }],
1414
filter: (testCase) => testCase.variant.includes('kit'),
1515
browser
1616
}
@@ -21,15 +21,18 @@ test.concurrent.for(testCases)(
2121
async (testCase, { page, ...ctx }) => {
2222
const cwd = ctx.cwd(testCase);
2323

24-
const msg =
25-
"This is a text file made by the Community Addon Template demo for the add-on: '@my-org/sv'!";
24+
const msg = "Community Addon Template demo for the add-on: '@my-org/sv'!";
2625

2726
const contentPath = path.resolve(cwd, `src/lib/@my-org/sv/content.txt`);
2827
const contentContent = fs.readFileSync(contentPath, 'utf8');
29-
3028
// Check if we have the imports
3129
expect(contentContent).toContain(msg);
3230

31+
const helloPath = path.resolve(cwd, `src/lib/@my-org/sv/HelloComponent.svelte`);
32+
const helloContent = fs.readFileSync(helloPath, 'utf8');
33+
// Check if we have the imports
34+
expect(helloContent).toContain('you');
35+
3336
// For browser testing
3437
if (browser) {
3538
const { close } = await prepareServer({ cwd, page });

packages/sv/src/create/templates/addon/tests/addon.test.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const browser = false;
1010
const { test, prepareServer, testCases } = setupTest(
1111
{ addon },
1212
{
13-
kinds: [{ type: 'default', options: { addon: { who: 'you' } } }],
13+
kinds: [{ type: 'default', options: { [addon.id]: { who: 'you' } } }],
1414
filter: (testCase) => testCase.variant.includes('kit'),
1515
browser
1616
}
@@ -21,15 +21,18 @@ test.concurrent.for(testCases)(
2121
async (testCase, { page, ...ctx }) => {
2222
const cwd = ctx.cwd(testCase);
2323

24-
const msg =
25-
"This is a text file made by the Community Addon Template demo for the add-on: '~SV-NAME-TODO~'!";
24+
const msg = "Community Addon Template demo for the add-on: '~SV-NAME-TODO~'!";
2625

2726
const contentPath = path.resolve(cwd, `src/lib/~SV-NAME-TODO~/content.txt`);
2827
const contentContent = fs.readFileSync(contentPath, 'utf8');
29-
3028
// Check if we have the imports
3129
expect(contentContent).toContain(msg);
3230

31+
const helloPath = path.resolve(cwd, `src/lib/~SV-NAME-TODO~/HelloComponent.svelte`);
32+
const helloContent = fs.readFileSync(helloPath, 'utf8');
33+
// Check if we have the imports
34+
expect(helloContent).toContain('you');
35+
3336
// For browser testing
3437
if (browser) {
3538
const { close } = await prepareServer({ cwd, page });

packages/sv/src/testing.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,17 @@ export async function prepareServer({
239239
return { url, close };
240240
}
241241

242+
export type PlaywrightContext = Pick<typeof import('@playwright/test'), 'chromium'>;
243+
242244
export type VitestContext = Pick<
243245
typeof import('vitest'),
244246
'inject' | 'test' | 'beforeAll' | 'beforeEach'
245247
>;
246248

247-
export function createSetupTest(vitest: VitestContext): <Addons extends AddonMap>(
249+
export function createSetupTest(
250+
vitest: VitestContext,
251+
playwright?: PlaywrightContext
252+
): <Addons extends AddonMap>(
248253
addons: Addons,
249254
options?: SetupTestOptions<Addons>
250255
) => {
@@ -272,12 +277,16 @@ export function createSetupTest(vitest: VitestContext): <Addons extends AddonMap
272277
if (withBrowser) {
273278
beforeAll(async () => {
274279
let chromium: Awaited<typeof import('@playwright/test')>['chromium'];
275-
try {
276-
({ chromium } = await import('@playwright/test'));
277-
} catch {
278-
throw new Error(
279-
'Browser testing requires @playwright/test. Install it with: pnpm add -D @playwright/test'
280-
);
280+
if (playwright) {
281+
chromium = playwright.chromium;
282+
} else {
283+
try {
284+
({ chromium } = await import('@playwright/test'));
285+
} catch {
286+
throw new Error(
287+
'Browser testing requires @playwright/test. Install it with: pnpm add -D @playwright/test'
288+
);
289+
}
281290
}
282291
browser = await chromium.launch();
283292
return async () => {

0 commit comments

Comments
 (0)