Skip to content

Commit

Permalink
chore: uvu -> vitest for create-svelte tests (#9910)
Browse files Browse the repository at this point in the history
* chore: uvu -> vitest for create-svelte tests

* format

* concurrency (#9921)

* realised we werent typechecking this file, found some errors. fixed

* Wait for beforeAll hook to complete

* simplify

* exclude create-svelte/template files from prettier, so that we can emit correctly formatted templates

* remove unused file

* Revert "exclude create-svelte/template files from prettier, so that we can emit correctly formatted templates"

This reverts commit aa188d4.

---------

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
Co-authored-by: Rich Harris <git@rich-harris.dev>
  • Loading branch information
3 people authored May 16, 2023
1 parent 223b46a commit 7042766
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 59 deletions.
4 changes: 2 additions & 2 deletions packages/create-svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
"sucrase": "^3.29.0",
"svelte": "^3.56.0",
"tiny-glob": "^0.2.9",
"uvu": "^0.5.6"
"vitest": "^0.31.0"
},
"scripts": {
"build": "node scripts/build-templates",
"test": "pnpm build && uvu test",
"test": "pnpm build && vitest run",
"check": "tsc",
"lint": "prettier --check . --config ../../.prettierrc --ignore-path ../../.gitignore --ignore-path .gitignore --plugin prettier-plugin-svelte --plugin-search-dir=.",
"format": "pnpm lint --write",
Expand Down
135 changes: 85 additions & 50 deletions packages/create-svelte/test/check.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { exec, execSync } from 'node:child_process';
import fs from 'node:fs';
import { execSync } from 'node:child_process';
import path from 'node:path';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { create } from '../index.js';
import { fileURLToPath } from 'node:url';
import glob from 'tiny-glob';
import { promisify } from 'node:util';
import glob from 'tiny-glob/sync.js';
import { beforeAll, describe, test } from 'vitest';
import { create } from '../index.js';

/** Resolve the given path relative to the current file */
/**
* Resolve the given path relative to the current file
* @param {string} path
*/
const resolve_path = (path) => fileURLToPath(new URL(path, import.meta.url));

// use a directory outside of packages to ensure it isn't added to the pnpm workspace
Expand All @@ -19,18 +22,18 @@ const existing_workspace_overrides = JSON.parse(

const overrides = { ...existing_workspace_overrides };

(await glob(resolve_path('../../../packages') + '/*/package.json')).forEach((pkgPath) => {
const name = JSON.parse(fs.readFileSync(pkgPath, 'utf-8')).name;
for (const pkg_path of glob(resolve_path('../../../packages/*/package.json'))) {
const name = JSON.parse(fs.readFileSync(pkg_path, 'utf-8')).name;
// use `file:` protocol for opting into stricter resolve logic which catches more bugs,
// but only on CI because it doesn't work locally for some reason
const protocol = process.env.CI ? 'file:' : '';
overrides[name] = `${protocol}${path.dirname(path.resolve(pkgPath))}`;
});
overrides[name] = `${protocol}${path.dirname(path.resolve(pkg_path))}`;
}

try {
const kit_dir = resolve_path('../../../packages/kit');
const ls_vite_result = execSync(`pnpm ls --json vite`, { cwd: kit_dir });
const vite_version = JSON.parse(ls_vite_result)[0].devDependencies.vite.version;
const vite_version = JSON.parse(ls_vite_result.toString())[0].devDependencies.vite.version;
overrides.vite = vite_version;
} catch (e) {
console.error('failed to parse installed vite version from packages/kit');
Expand All @@ -47,16 +50,61 @@ const workspace = {
pnpm: { overrides },
devDependencies: overrides
};

fs.writeFileSync(
path.join(test_workspace_dir, 'package.json'),
JSON.stringify(workspace, null, '\t')
);

fs.writeFileSync(path.join(test_workspace_dir, 'pnpm-workspace.yaml'), 'packages:\n - ./*\n');

for (const template of fs.readdirSync('templates')) {
const exec_async = promisify(exec);

beforeAll(async () => {
await exec_async(`pnpm install --no-frozen-lockfile`, {
cwd: test_workspace_dir
});
}, 60000);

/** @param {any} pkg */
function patch_package_json(pkg) {
Object.entries(overrides).forEach(([key, value]) => {
if (pkg.devDependencies?.[key]) {
pkg.devDependencies[key] = value;
}

if (pkg.dependencies?.[key]) {
pkg.dependencies[key] = value;
}

if (!pkg.pnpm) {
pkg.pnpm = {};
}

if (!pkg.pnpm.overrides) {
pkg.pnpm.overrides = {};
}

pkg.pnpm.overrides = { ...pkg.pnpm.overrides, ...overrides };
});
pkg.private = true;
}

/**
* Tests in different templates can be run concurrently for a nice speedup locally, but tests within a template must be run sequentially.
* It'd be better to group tests by template, but vitest doesn't support that yet.
* @type {Map<string, [string, () => import('node:child_process').PromiseWithChild<any>][]>}
*/
const script_test_map = new Map();

const templates = /** @type {Array<'default' | 'skeleton' | 'skeletonlib'>} */ (
fs.readdirSync('templates')
);

for (const template of templates) {
if (template[0] === '.') continue;

for (const types of ['checkjs', 'typescript']) {
for (const types of /** @type {const} */ (['checkjs', 'typescript'])) {
const cwd = path.join(test_workspace_dir, `${template}-${types}`);
fs.rmSync(cwd, { recursive: true, force: true });

Expand All @@ -66,51 +114,38 @@ for (const template of fs.readdirSync('templates')) {
types,
prettier: true,
eslint: true,
playwright: false
playwright: false,
vitest: false
});

const pkg = JSON.parse(fs.readFileSync(path.join(cwd, 'package.json'), 'utf-8'));
Object.entries(overrides).forEach(([key, value]) => {
if (pkg.devDependencies?.[key]) {
pkg.devDependencies[key] = value;
}
if (pkg.dependencies?.[key]) {
pkg.dependencies[key] = value;
}
if (!pkg.pnpm) {
pkg.pnpm = {};
}
if (!pkg.pnpm.overrides) {
pkg.pnpm.overrides = {};
}
pkg.pnpm.overrides = { ...pkg.pnpm.overrides, ...overrides };
});
pkg.private = true;
patch_package_json(pkg);

fs.writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(pkg, null, '\t') + '\n');

// run provided scripts that are non-blocking. All of them should exit with 0
// package script requires lib dir
const scripts_to_test = ['sync', 'format', 'lint', 'check', 'build'];
if (fs.existsSync(path.join(cwd, 'src', 'lib'))) {
scripts_to_test.push('package');
}
// TODO: lint should run before format
const scripts_to_test = ['format', 'lint', 'check', 'build', 'package'].filter(
(s) => s in pkg.scripts
);

for (const script of scripts_to_test.filter((s) => !!pkg.scripts[s])) {
test(`${template}-${types}: ${script}`, () => {
try {
execSync(`pnpm ${script}`, { cwd, stdio: 'pipe' });
} catch (e) {
assert.unreachable(
`script: ${script} failed\n` +
`---\nstdout:\n${e.stdout}\n` +
`---\nstderr:\n${e.stderr}`
);
}
});
for (const script of scripts_to_test) {
const tests = script_test_map.get(script) ?? [];
tests.push([`${template}-${types}`, () => exec_async(`pnpm ${script}`, { cwd })]);
script_test_map.set(script, tests);
}
}
}

console.log('installing dependencies...');
execSync('pnpm install --no-frozen-lockfile', { cwd: test_workspace_dir, stdio: 'ignore' });
console.log('done installing dependencies');
test.run();
for (const [script, tests] of script_test_map) {
describe.concurrent(
script,
() => {
for (const [name, task] of tests) {
test(name, task);
}
},
{ timeout: 60000 }
);
}
2 changes: 1 addition & 1 deletion packages/create-svelte/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"strict": true,
"skipLibCheck": true
},
"include": ["./scripts/**/*", "./index.js", "./bin.js", "./utils.js"]
"include": ["./scripts/**/*", "./test/*.js", "./index.js", "./bin.js", "./utils.js"]
}
5 changes: 5 additions & 0 deletions packages/create-svelte/vitest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: { dir: './test', include: ['*.js'] }
});
6 changes: 3 additions & 3 deletions packages/kit/src/runtime/server/cookie.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ test('warns if cookie exceeds 4,129 bytes', () => {
const error = /** @type {Error} */ (e);

assert.equal(error.message, `Cookie "a" is too large, and will be discarded by the browser`);
} finally {
// @ts-expect-error
globalThis.__SVELTEKIT_DEV__ = false;
}

// @ts-expect-error
globalThis.__SVELTEKIT_DEV__ = false;
});

test('get all cookies from header and set calls', () => {
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7042766

Please sign in to comment.