Skip to content

Commit 5941575

Browse files
feat: simple testing library (#275)
Co-authored-by: Manuel Serret <[email protected]>
1 parent 4e6552b commit 5941575

File tree

125 files changed

+2110
-1182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+2110
-1182
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ jobs:
3939
- run: pnpm build
4040
- run: pnpm check
4141
test:
42-
runs-on: ubuntu-latest
42+
runs-on: ${{ matrix.os }}
43+
strategy:
44+
matrix:
45+
os: [ubuntu-latest, windows-latest, macOS-latest]
4346
steps:
4447
- uses: actions/checkout@v4
4548
- uses: pnpm/action-setup@v4
@@ -48,5 +51,6 @@ jobs:
4851
node-version: 18
4952
cache: pnpm
5053
- run: pnpm install --frozen-lockfile
54+
- run: pnpm exec playwright install chromium
5155
- run: pnpm build
5256
- run: pnpm test

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ package-lock.json
66
yarn.lock
77
vite.config.js.timestamp-*
88
/packages/create-svelte/template/CHANGELOG.md
9-
.test-tmp
9+
.test-output

.prettierignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

community-adder-template/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
temp
33
.outputs
4+
.test-output

community-adder-template/package.json

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,28 @@
22
"name": "community-adder-template",
33
"private": true,
44
"version": "0.0.0",
5-
"license": "MIT",
65
"type": "module",
7-
"exports": "./src/index.js",
8-
"keywords": [
9-
"svelte-add-on",
10-
"sv"
11-
],
6+
"license": "MIT",
127
"scripts": {
8+
"create-temp": "sv create temp --types ts --template minimal --no-add-ons --no-install",
139
"start": "sv add -C temp --community file:../",
14-
"create-temp": "sv create temp --check-types typescript --template skeleton --no-adders --no-install"
10+
"test": "vitest run"
1511
},
12+
"files": [
13+
"src",
14+
"!src/**/*.test.*"
15+
],
16+
"exports": "./src/index.js",
1617
"dependencies": {
1718
"@sveltejs/cli-core": "workspace:*"
1819
},
1920
"devDependencies": {
20-
"sv": "workspace:*"
21+
"@playwright/test": "^1.48.2",
22+
"sv": "workspace:*",
23+
"vitest": "^2.1.4"
2124
},
22-
"files": [
23-
"src",
24-
"!src/**/*.test.*"
25+
"keywords": [
26+
"svelte-add-on",
27+
"sv"
2528
]
2629
}

community-adder-template/src/index.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { defineAdder, defineAdderOptions } from '@sveltejs/cli-core';
22
import { imports } from '@sveltejs/cli-core/js';
3-
import { parseScript } from '@sveltejs/cli-core/parsers';
3+
import { parseSvelte } from '@sveltejs/cli-core/parsers';
44

55
export const options = defineAdderOptions({
6-
demo: {
7-
question: 'Do you want to use a demo?',
8-
type: 'boolean',
9-
default: false
10-
}
6+
demo: {
7+
question: 'Do you want to use a demo?',
8+
type: 'boolean',
9+
default: false
10+
}
1111
});
1212

13-
export const adder = defineAdder({
14-
id: 'community-adder-template',
13+
export default defineAdder({
14+
id: 'community-addon',
1515
environments: { kit: true, svelte: true },
1616
options,
1717
packages: [],
@@ -27,10 +27,11 @@ export const adder = defineAdder({
2727
},
2828
{
2929
name: () => 'src/DemoComponent.svelte',
30-
content: ({ content }) => {
31-
const { ast, generateCode } = parseScript(content);
32-
imports.addDefault(ast, '../adder-template-demo.txt?raw', 'Demo');
33-
return generateCode();
30+
content: ({ content, options, typescript }) => {
31+
if (!options.demo) return content;
32+
const { script, generateCode } = parseSvelte(content, { typescript });
33+
imports.addDefault(script.ast, '../adder-template-demo.txt?raw', 'demo');
34+
return generateCode({ script: script.generateCode(), template: '{demo}' });
3435
}
3536
}
3637
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import path from 'node:path';
2+
import { expect } from '@playwright/test';
3+
import { fixture, setupTest } from './setup/suite.js';
4+
import addon from '../src/index.js';
5+
6+
const id = addon.id;
7+
const { test, variants, prepareServer } = setupTest({ [id]: addon });
8+
9+
test.concurrent.for(variants)('demo - %s', async (variant, { page, ...ctx }) => {
10+
const cwd = await ctx.run(variant, { [id]: { demo: true } });
11+
12+
// ...add files
13+
if (variant.startsWith('kit')) {
14+
const target = path.resolve(cwd, 'src', 'routes', '+page.svelte');
15+
fixture({ name: '+page.svelte', target });
16+
} else {
17+
const target = path.resolve(cwd, 'src', 'App.svelte');
18+
fixture({ name: 'App.svelte', target });
19+
}
20+
21+
const { close } = await prepareServer({ cwd, page });
22+
// kill server process when we're done
23+
ctx.onTestFinished(async () => await close());
24+
25+
// expectations
26+
const textContent = await page.getByTestId('demo').textContent();
27+
expect(textContent).toContain('This is a text file made by the Community Adder Template demo!');
28+
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import demo from '../../adder-template-demo.txt?raw';
3+
</script>
4+
5+
<span data-testid="demo">{demo}</span>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
import demo from '../adder-template-demo.txt?raw';
3+
</script>
4+
5+
<span data-testid="demo">{demo}</span>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { fileURLToPath } from 'node:url';
2+
import { setup, type ProjectVariant } from 'sv/testing';
3+
import type { GlobalSetupContext } from 'vitest/node';
4+
5+
const variants: ProjectVariant[] = ['kit-js', 'kit-ts', 'vite-js', 'vite-ts'];
6+
const TEST_DIR = fileURLToPath(new URL('../../.test-output/', import.meta.url));
7+
8+
export default async function ({ provide }: GlobalSetupContext) {
9+
// global setup (e.g. spin up docker containers)
10+
11+
// downloads different project configurations (sveltekit, js/ts, vite-only, etc)
12+
const { templatesDir } = await setup({ cwd: TEST_DIR, variants, clean: true });
13+
14+
provide('testDir', TEST_DIR);
15+
provide('templatesDir', templatesDir);
16+
provide('variants', variants);
17+
18+
return async () => {
19+
// tear down... (e.g. cleanup docker containers)
20+
};
21+
}
22+
23+
declare module 'vitest' {
24+
export interface ProvidedContext {
25+
testDir: string;
26+
templatesDir: string;
27+
variants: ProjectVariant[];
28+
}
29+
}

0 commit comments

Comments
 (0)