Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: simple testing library #275

Merged
merged 73 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
5e22384
skip writing empty files
AdrianGonz97 Nov 4, 2024
95ff386
rename dir because vitest hates us
AdrianGonz97 Nov 6, 2024
71e47f2
dont expose `options`
AdrianGonz97 Nov 6, 2024
7e0f152
simplify
AdrianGonz97 Nov 6, 2024
974e5ad
pm
AdrianGonz97 Nov 6, 2024
8570940
remove old tests
AdrianGonz97 Nov 6, 2024
809a72f
initial install helper impl
AdrianGonz97 Nov 7, 2024
3e6f6bd
ignores
AdrianGonz97 Nov 7, 2024
0c86f37
add initial tests
AdrianGonz97 Nov 7, 2024
665ead0
..pin storybook
AdrianGonz97 Nov 7, 2024
6446e59
fix scripts
AdrianGonz97 Nov 7, 2024
53934db
add storybook test
AdrianGonz97 Nov 7, 2024
d3ef737
simplify
AdrianGonz97 Nov 7, 2024
e398dc2
deps
AdrianGonz97 Nov 7, 2024
8068dfc
add retries
AdrianGonz97 Nov 7, 2024
4b2f036
not needed
AdrianGonz97 Nov 7, 2024
0a0ba1a
tweak community template
AdrianGonz97 Nov 7, 2024
cd1a56d
add fixtures
AdrianGonz97 Nov 7, 2024
ed32699
community template tests
AdrianGonz97 Nov 7, 2024
d40929a
naming
AdrianGonz97 Nov 7, 2024
36939bb
tweaks
AdrianGonz97 Nov 7, 2024
c1b61be
unneeded
AdrianGonz97 Nov 7, 2024
7ae9ad9
clean
AdrianGonz97 Nov 7, 2024
1379621
Merge branch 'main' into feat/simple-testing-lib
AdrianGonz97 Nov 7, 2024
6adea4d
lockfile trickery
AdrianGonz97 Nov 7, 2024
266fea4
fix eslint test
AdrianGonz97 Nov 7, 2024
6a139e2
fix paraglide
AdrianGonz97 Nov 7, 2024
0fa007f
tweak workflow
AdrianGonz97 Nov 7, 2024
d417939
...
AdrianGonz97 Nov 7, 2024
19e80da
fix lint
AdrianGonz97 Nov 7, 2024
b2af1bb
fix check
AdrianGonz97 Nov 7, 2024
6d96a56
Merge branch 'main' into feat/simple-testing-lib
AdrianGonz97 Nov 7, 2024
fa0a992
simplify
AdrianGonz97 Nov 7, 2024
a9bbe4f
simplify
AdrianGonz97 Nov 8, 2024
a76f89b
upgrade vitest
AdrianGonz97 Nov 8, 2024
55afcbe
fix nit
AdrianGonz97 Nov 8, 2024
e6386a7
fix lint
AdrianGonz97 Nov 8, 2024
6a1d29f
simplify
AdrianGonz97 Nov 8, 2024
1e5be34
cleanup
AdrianGonz97 Nov 8, 2024
1e676fe
rename `vi` to `vitest`
AdrianGonz97 Nov 8, 2024
0a56d26
windows fixes
AdrianGonz97 Nov 8, 2024
f6523d4
more windows annoyances
AdrianGonz97 Nov 8, 2024
5870036
tweaks
AdrianGonz97 Nov 8, 2024
677fbb0
add `test:ui` script
manuel3108 Nov 8, 2024
6e9360c
`tinyexec` `throwOnError: true`
manuel3108 Nov 8, 2024
7181351
revert
AdrianGonz97 Nov 8, 2024
d98c630
properly terminate child processes
AdrianGonz97 Nov 9, 2024
9aa59cf
dont skip storybook
AdrianGonz97 Nov 9, 2024
dd141e7
cleanup on failure to load the page
AdrianGonz97 Nov 9, 2024
d4794f0
unused
AdrianGonz97 Nov 9, 2024
50fde5c
newline
AdrianGonz97 Nov 9, 2024
2b98217
use vitest workspaces
AdrianGonz97 Nov 9, 2024
8e00672
add `try-catch` while killing processes
manuel3108 Nov 9, 2024
9526fee
increase navigation timeout
manuel3108 Nov 9, 2024
ebe7812
fix timeout
AdrianGonz97 Nov 9, 2024
ed81a57
add retries
AdrianGonz97 Nov 9, 2024
a3baf83
remove storybook log
AdrianGonz97 Nov 9, 2024
987b1e8
cleanup cli package
AdrianGonz97 Nov 10, 2024
650ede7
fixes
AdrianGonz97 Nov 10, 2024
047c549
format before evaluating test
AdrianGonz97 Nov 10, 2024
fe810a5
fix dir path
AdrianGonz97 Nov 10, 2024
25cadb6
setup matrix
AdrianGonz97 Nov 10, 2024
88f9473
skip running docker containers outside of linux runners
AdrianGonz97 Nov 11, 2024
9d5b6b2
print console output?
manuel3108 Nov 11, 2024
ef2df6e
force `npm` for storybook?
manuel3108 Nov 11, 2024
c3aa2fc
Revert "force `npm` for storybook?"
manuel3108 Nov 11, 2024
1c095d6
try latest
manuel3108 Nov 11, 2024
57ab58e
Revert "try latest"
manuel3108 Nov 11, 2024
88bf2ec
Revert "print console output?"
manuel3108 Nov 11, 2024
fc72e9a
skip runnung storybook tests in ci on windows
manuel3108 Nov 11, 2024
77fd78e
last bits
AdrianGonz97 Nov 11, 2024
c01e72e
Merge branch 'main' into feat/simple-testing-lib
AdrianGonz97 Nov 11, 2024
b6d91be
format
AdrianGonz97 Nov 11, 2024
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
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ jobs:
- run: pnpm build
- run: pnpm check
test:
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
Expand All @@ -48,5 +51,6 @@ jobs:
node-version: 18
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm exec playwright install chromium
- run: pnpm build
- run: pnpm test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ package-lock.json
yarn.lock
vite.config.js.timestamp-*
/packages/create-svelte/template/CHANGELOG.md
.test-tmp
.test-output
2 changes: 0 additions & 2 deletions .prettierignore

This file was deleted.

1 change: 1 addition & 0 deletions community-adder-template/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
temp
.outputs
.test-output
25 changes: 14 additions & 11 deletions community-adder-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,28 @@
"name": "community-adder-template",
"private": true,
"version": "0.0.0",
"license": "MIT",
"type": "module",
"exports": "./src/index.js",
"keywords": [
"svelte-add-on",
"sv"
],
"license": "MIT",
"scripts": {
"create-temp": "sv create temp --types ts --template minimal --no-add-ons --no-install",
"start": "sv add -C temp --community file:../",
"create-temp": "sv create temp --check-types typescript --template skeleton --no-adders --no-install"
"test": "vitest run"
},
"files": [
"src",
"!src/**/*.test.*"
],
"exports": "./src/index.js",
"dependencies": {
"@sveltejs/cli-core": "workspace:*"
},
"devDependencies": {
"sv": "workspace:*"
"@playwright/test": "^1.48.2",
"sv": "workspace:*",
"vitest": "^2.1.4"
},
"files": [
"src",
"!src/**/*.test.*"
"keywords": [
"svelte-add-on",
"sv"
]
}
25 changes: 13 additions & 12 deletions community-adder-template/src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { defineAdder, defineAdderOptions } from '@sveltejs/cli-core';
import { imports } from '@sveltejs/cli-core/js';
import { parseScript } from '@sveltejs/cli-core/parsers';
import { parseSvelte } from '@sveltejs/cli-core/parsers';

export const options = defineAdderOptions({
demo: {
question: 'Do you want to use a demo?',
type: 'boolean',
default: false
}
demo: {
question: 'Do you want to use a demo?',
type: 'boolean',
default: false
}
});

export const adder = defineAdder({
id: 'community-adder-template',
export default defineAdder({
id: 'community-addon',
environments: { kit: true, svelte: true },
options,
packages: [],
Expand All @@ -27,10 +27,11 @@ export const adder = defineAdder({
},
{
name: () => 'src/DemoComponent.svelte',
content: ({ content }) => {
const { ast, generateCode } = parseScript(content);
imports.addDefault(ast, '../adder-template-demo.txt?raw', 'Demo');
return generateCode();
content: ({ content, options, typescript }) => {
if (!options.demo) return content;
const { script, generateCode } = parseSvelte(content, { typescript });
imports.addDefault(script.ast, '../adder-template-demo.txt?raw', 'demo');
return generateCode({ script: script.generateCode(), template: '{demo}' });
}
}
]
Expand Down
28 changes: 28 additions & 0 deletions community-adder-template/tests/custom-addon.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'node:path';
import { expect } from '@playwright/test';
import { fixture, setupTest } from './setup/suite.js';
import addon from '../src/index.js';

const id = addon.id;
const { test, variants, prepareServer } = setupTest({ [id]: addon });

test.concurrent.for(variants)('demo - %s', async (variant, { page, ...ctx }) => {
const cwd = await ctx.run(variant, { [id]: { demo: true } });

// ...add files
if (variant.startsWith('kit')) {
const target = path.resolve(cwd, 'src', 'routes', '+page.svelte');
fixture({ name: '+page.svelte', target });
} else {
const target = path.resolve(cwd, 'src', 'App.svelte');
fixture({ name: 'App.svelte', target });
}
Comment on lines +13 to +19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contents of those files look identical to me, why duplicate them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import specifiers for the demo are different. Though, we could simplify it to just modify the contents of the file like we do in the mdsvex test


const { close } = await prepareServer({ cwd, page });
// kill server process when we're done
ctx.onTestFinished(async () => await close());

// expectations
const textContent = await page.getByTestId('demo').textContent();
expect(textContent).toContain('This is a text file made by the Community Adder Template demo!');
});
5 changes: 5 additions & 0 deletions community-adder-template/tests/fixtures/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import demo from '../../adder-template-demo.txt?raw';
</script>

<span data-testid="demo">{demo}</span>
5 changes: 5 additions & 0 deletions community-adder-template/tests/fixtures/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
import demo from '../adder-template-demo.txt?raw';
</script>

<span data-testid="demo">{demo}</span>
29 changes: 29 additions & 0 deletions community-adder-template/tests/setup/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { fileURLToPath } from 'node:url';
import { setup, type ProjectVariant } from 'sv/testing';
import type { GlobalSetupContext } from 'vitest/node';

const variants: ProjectVariant[] = ['kit-js', 'kit-ts', 'vite-js', 'vite-ts'];
manuel3108 marked this conversation as resolved.
Show resolved Hide resolved
const TEST_DIR = fileURLToPath(new URL('../../.test-output/', import.meta.url));

export default async function ({ provide }: GlobalSetupContext) {
// global setup (e.g. spin up docker containers)

// downloads different project configurations (sveltekit, js/ts, vite-only, etc)
const { templatesDir } = await setup({ cwd: TEST_DIR, variants, clean: true });

provide('testDir', TEST_DIR);
provide('templatesDir', templatesDir);
provide('variants', variants);

return async () => {
// tear down... (e.g. cleanup docker containers)
};
}

declare module 'vitest' {
export interface ProvidedContext {
testDir: string;
templatesDir: string;
variants: ProjectVariant[];
}
}
108 changes: 108 additions & 0 deletions community-adder-template/tests/setup/suite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import fs from 'node:fs';
import path from 'node:path';
import { execSync } from 'node:child_process';
import * as vitest from 'vitest';
import { installAddon, type AddonMap, type OptionMap } from 'sv';
import { createProject, startPreview, type CreateProject, type ProjectVariant } from 'sv/testing';
import { chromium, type Browser, type Page } from '@playwright/test';
import { fileURLToPath } from 'node:url';

const cwd = vitest.inject('testDir');
const templatesDir = vitest.inject('templatesDir');
const variants = vitest.inject('variants');

const SETUP_DIR = fileURLToPath(new URL('.', import.meta.url));

type Fixtures<Addons extends AddonMap> = {
page: Page;
run(variant: ProjectVariant, options: OptionMap<Addons>): Promise<string>;
};

export function setupTest<Addons extends AddonMap>(addons: Addons) {
let create: CreateProject;
let browser: Browser;

const test = vitest.test.extend<Fixtures<Addons>>({} as any);

vitest.beforeAll(async () => {
browser = await chromium.launch();
return async () => {
await browser.close();
};
});

vitest.beforeAll(({ name }) => {
const testName = path.parse(name).name.replace('.test', '');

// constructs a builder for create test projects
create = createProject({ cwd, templatesDir, testName });

// creates a pnpm workspace in each addon dir
fs.writeFileSync(
path.resolve(cwd, testName, 'pnpm-workspace.yaml'),
`packages:\n - '**/*'`,
'utf8'
);
});

// runs before each test case
vitest.beforeEach<Fixtures<Addons>>(async (ctx) => {
const browserCtx = await browser.newContext();
ctx.page = await browserCtx.newPage();
ctx.run = async (variant, options) => {
const cwd = create({ testId: ctx.task.id, variant });

// test metadata
const metaPath = path.resolve(cwd, 'meta.json');
fs.writeFileSync(metaPath, JSON.stringify({ variant, options }, null, '\t'), 'utf8');

// run addon
await installAddon({ cwd, addons, options, packageManager: 'pnpm' });

return cwd;
};

return async () => {
await browserCtx.close();
// ...other tear downs
};
});

return {
test,
variants,
prepareServer
};
}

/**
* Installs dependencies, builds the project, and spins up the preview server
*/
async function prepareServer({ cwd, page }: { cwd: string; page: Page }) {
// install deps
execSync('pnpm install --no-frozen-lockfile', { cwd, stdio: 'pipe' });

// ...do commands and any other extra stuff

// build project
execSync('npm run build', { cwd, stdio: 'pipe' });

// start preview server `vite preview`
const { url, close } = await startPreview({ cwd });

// navigate to the page
await page.goto(url);

return { url, close };
}

/**
* Applies a fixture to the target path
*/
export function fixture({ name, target }: { name: string; target: string }) {
const fixturePath = path.resolve(SETUP_DIR, '..', 'fixtures', name);
if (!fs.existsSync(fixturePath)) {
throw new Error(`Fixture does not exist at: ${fixturePath}`);
}
fs.copyFileSync(fixturePath, target);
}
16 changes: 0 additions & 16 deletions community-adder-template/tests/tests.js

This file was deleted.

14 changes: 14 additions & 0 deletions community-adder-template/vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { defineConfig } from 'vitest/config';

const ONE_MINUTE = 1000 * 60;

export default defineConfig({
test: {
include: ['tests/**/*.test.{js,ts}'],
exclude: ['tests/setup/*'],
testTimeout: ONE_MINUTE * 3,
hookTimeout: ONE_MINUTE * 3,
maxConcurrency: 10,
globalSetup: ['tests/setup/global.ts']
}
});
5 changes: 3 additions & 2 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ export default [
'packages/create/scripts/**/*',
'packages/create/templates/**/*',
'**/temp/*',
'.test-tmp/**/*',
'**/.test-output/*',
'**/dist/*',
'packages/**/tests/**/{output,input}.ts',
'rollup.config.js'
'rollup.config.js',
'community-adder-template/tests/*'
]
}
];
28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
{
"name": "sv-monorepo",
"version": "0.0.1",
"description": "monorepo for sv and friends",
"private": true,
"version": "0.0.1",
"type": "module",
"description": "monorepo for sv and friends",
"engines": {
"pnpm": "^9.0.0"
},
"scripts": {
"build": "rollup -c",
"changeset:publish": "changeset publish",
"check": "pnpm --parallel check",
"lint": "pnpm --parallel lint && eslint --cache --cache-location node_modules/.eslintcache",
"format": "pnpm --parallel format",
"dev": "rollup --config --watch",
"build": "rollup -c",
"test": "pnpm --parallel test",
"changeset:publish": "changeset publish"
"format": "pnpm --parallel format",
"lint": "pnpm --parallel lint && eslint --cache --cache-location node_modules/.eslintcache",
"test": "vitest run --silent",
"test:ui": "vitest --ui"
},
"devDependencies": {
"@changesets/cli": "^2.27.9",
"@playwright/test": "^1.48.2",
"@rollup/plugin-commonjs": "^26.0.1",
"@rollup/plugin-dynamic-import-vars": "^2.1.2",
"@rollup/plugin-json": "^6.1.0",
Expand All @@ -23,7 +28,7 @@
"@sveltejs/eslint-config": "^8.1.0",
"@svitejs/changesets-changelog-github-compact": "^1.1.0",
"@types/node": "^22.3.0",
"@vitest/ui": "^2.0.5",
"@vitest/ui": "^2.1.4",
"eslint": "^9.10.0",
"magic-string": "^0.30.11",
"prettier": "^3.3.3",
Expand All @@ -37,10 +42,7 @@
"typescript": "^5.6.2",
"typescript-eslint": "^8.5.0",
"unplugin-isolated-decl": "^0.6.5",
"vitest": "^2.0.5"
"vitest": "^2.1.4"
},
"packageManager": "[email protected]",
"engines": {
"pnpm": "^9.0.0"
}
"packageManager": "[email protected]"
}
Loading