Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const browser = false;
const { test, prepareServer, testCases } = setupTest(
{ addon },
{
kinds: [{ type: 'default', options: { addon: { who: 'you' } } }],
kinds: [{ type: 'default', options: { [addon.id]: { who: 'you' } } }],
filter: (testCase) => testCase.variant.includes('kit'),
browser
}
Expand All @@ -21,15 +21,18 @@ test.concurrent.for(testCases)(
async (testCase, { page, ...ctx }) => {
const cwd = ctx.cwd(testCase);

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

const contentPath = path.resolve(cwd, `src/lib/@my-org/sv/content.txt`);
const contentContent = fs.readFileSync(contentPath, 'utf8');

// Check if we have the imports
expect(contentContent).toContain(msg);

const helloPath = path.resolve(cwd, `src/lib/@my-org/sv/HelloComponent.svelte`);
const helloContent = fs.readFileSync(helloPath, 'utf8');
// Check if we have the imports
expect(helloContent).toContain('you');

// For browser testing
if (browser) {
const { close } = await prepareServer({ cwd, page });
Expand Down
11 changes: 7 additions & 4 deletions packages/sv/src/create/templates/addon/tests/addon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const browser = false;
const { test, prepareServer, testCases } = setupTest(
{ addon },
{
kinds: [{ type: 'default', options: { addon: { who: 'you' } } }],
kinds: [{ type: 'default', options: { [addon.id]: { who: 'you' } } }],
filter: (testCase) => testCase.variant.includes('kit'),
browser
}
Expand All @@ -21,15 +21,18 @@ test.concurrent.for(testCases)(
async (testCase, { page, ...ctx }) => {
const cwd = ctx.cwd(testCase);

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

const contentPath = path.resolve(cwd, `src/lib/~SV-NAME-TODO~/content.txt`);
const contentContent = fs.readFileSync(contentPath, 'utf8');

// Check if we have the imports
expect(contentContent).toContain(msg);

const helloPath = path.resolve(cwd, `src/lib/~SV-NAME-TODO~/HelloComponent.svelte`);
const helloContent = fs.readFileSync(helloPath, 'utf8');
// Check if we have the imports
expect(helloContent).toContain('you');

// For browser testing
if (browser) {
const { close } = await prepareServer({ cwd, page });
Expand Down
23 changes: 16 additions & 7 deletions packages/sv/src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,17 @@ export async function prepareServer({
return { url, close };
}

export type PlaywrightContext = Pick<typeof import('@playwright/test'), 'chromium'>;

export type VitestContext = Pick<
typeof import('vitest'),
'inject' | 'test' | 'beforeAll' | 'beforeEach'
>;

export function createSetupTest(vitest: VitestContext): <Addons extends AddonMap>(
export function createSetupTest(
vitest: VitestContext,
playwright?: PlaywrightContext
): <Addons extends AddonMap>(
addons: Addons,
options?: SetupTestOptions<Addons>
) => {
Expand Down Expand Up @@ -272,12 +277,16 @@ export function createSetupTest(vitest: VitestContext): <Addons extends AddonMap
if (withBrowser) {
beforeAll(async () => {
let chromium: Awaited<typeof import('@playwright/test')>['chromium'];
try {
({ chromium } = await import('@playwright/test'));
} catch {
throw new Error(
'Browser testing requires @playwright/test. Install it with: pnpm add -D @playwright/test'
);
if (playwright) {
chromium = playwright.chromium;
} else {
try {
({ chromium } = await import('@playwright/test'));
} catch {
throw new Error(
'Browser testing requires @playwright/test. Install it with: pnpm add -D @playwright/test'
);
}
}
browser = await chromium.launch();
return async () => {
Expand Down
Loading