Skip to content
This repository has been archived by the owner on Oct 20, 2024. It is now read-only.

chore: use vitest #480

Closed
wants to merge 14 commits into from
Prev Previous commit
Next Next commit
Merge branch 'main' into chore/use-vitest
  • Loading branch information
manuel3108 committed Jul 28, 2024
commit 9ceada42dddc6e1114aca6c4b763a6080d98e63e
16 changes: 6 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
"description": "",
"scripts": {
"ci:publish": "changeset publish",
"prettier:check": "pnpm prettier . --check",
"prettier:fix": "pnpm prettier . --write",
"eslint:check": "pnpm eslint .",
"eslint:fix": "pnpm eslint --fix .",
"build:dev": "rollup --config --watch",
"build:prod": "rollup -c",
"format": "prettier . --write && eslint --fix .",
"lint": "prettier . --check && eslint .",
"dev": "rollup --config --watch",
"build": "rollup -c",
"website:dev": "pnpm -C ./packages/website dev",
"website:build": "pnpm -C ./packages/website build",
"website:preview": "pnpm -C ./packages/website preview",
"types:check": "tsc --project tsconfig.json --noEmit",
"test": "node ./packages/tests/build/index.js",
"check": "tsc --project tsconfig.json --noEmit",
"utils:dependencies": "node ./packages/dev-utils/build/index.js dependencies && pnpm prettier:fix",
"check": "pnpm eslint:check && pnpm prettier:check && pnpm types:check",
"test": "pnpm -F @svelte-add/tests exec vitest",
"test:ui": "pnpm -F @svelte-add/tests exec vitest --ui",
"postinstall": "pnpm -F @svelte-add/testing-library exec playwright install chromium"
},
"keywords": [],
Expand Down
4 changes: 2 additions & 2 deletions packages/testing-library/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateTestCases, runAdderTests } from "./utils/test-cases";
import { prepareTests } from "./utils/workspace";
import { generateTestCases, runAdderTests } from './utils/test-cases';
import { prepareTests } from './utils/workspace';

export type TestOptions = {
headless: boolean;
Expand Down
71 changes: 36 additions & 35 deletions packages/testing-library/package.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
{
"name": "@svelte-add/testing-library",
"version": "2.1.4",
"type": "module",
"license": "MIT",
"exports": {
".": {
"types": "./build/index.d.ts",
"default": "./build/index.js"
}
},
"files": [
"build"
],
"dependencies": {
"@svelte-add/core": "workspace:^",
"create-svelte": "^6.3.0",
"playwright": "^1.44.1"
},
"devDependencies": {
"terminate": "^2.6.1",
"uid": "^2.0.2"
},
"bugs": "https://github.com/svelte-add/svelte-add/issues",
"repository": {
"type": "git",
"url": "https://github.com/svelte-add/svelte-add/tree/main/projects/testing-library"
},
"keywords": [
"adders",
"adder",
"testing",
"svelte",
"kit",
"svelte-kit"
]
"name": "@svelte-add/testing-library",
"version": "2.1.12",
"type": "module",
"license": "MIT",
"exports": {
".": {
"types": "./build/index.d.ts",
"default": "./build/index.js"
}
},
"files": [
"build"
],
"dependencies": {
"@svelte-add/core": "workspace:^",
"playwright": "^1.44.1",
"create-svelte": "^6.3.4"
},
"devDependencies": {
"promise-parallel-throttle": "^3.5.0",
"terminate": "^2.6.1",
"uid": "^2.0.2"
},
"bugs": "https://github.com/svelte-add/svelte-add/issues",
"repository": {
"type": "git",
"url": "https://github.com/svelte-add/svelte-add/tree/main/projects/testing-library"
},
"keywords": [
"adders",
"adder",
"testing",
"svelte",
"kit",
"svelte-kit"
]
}
155 changes: 133 additions & 22 deletions packages/testing-library/utils/test-cases.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { join } from "node:path";
import { mkdir } from "node:fs/promises";
import { ProjectTypesList } from "./create-project";
import { runTests } from "./test";
import { uid } from "uid";
import { startDevServer, stopDevServer } from "./dev-server";
import { startBrowser, stopBrowser } from "./browser-control";
import { getTemplatesDirectory, installDependencies, prepareWorkspaceWithTemplate, saveOptionsFile } from "./workspace";
import { runAdder } from "./adder";
import { prompts, remoteControl } from "@svelte-add/core/internal";
import type { AdderWithoutExplicitArgs } from "@svelte-add/core/adder/config";
import type { TestOptions } from "..";
import type { OptionValues, Question } from "@svelte-add/core/adder/options";
import { join } from 'node:path';
import { mkdir } from 'node:fs/promises';
import { ProjectTypesList } from './create-project';
import { runTests } from './test';
import { uid } from 'uid';
import { startDevServer, stopDevServer } from './dev-server';
import { startBrowser, stopBrowser } from './browser-control';
import {
getTemplatesDirectory,
installDependencies,
prepareWorkspaceWithTemplate,
saveOptionsFile,
} from './workspace';
import { runAdder } from './adder';
import { prompts } from '@svelte-add/core/internal';
import * as Throttle from 'promise-parallel-throttle';
import type { AdderWithoutExplicitArgs } from '@svelte-add/core/adder/config';
import type { TestOptions } from '..';
import type { OptionValues, Question } from '@svelte-add/core/adder/options';

export type TestCase = {
template: string;
Expand Down Expand Up @@ -63,10 +69,10 @@ export async function runAdderTests(
'The adder is not exporting any tests. Please make sure to properly define your tests while calling `defineAdder`',
);

remoteControl.enable();
remoteControl.enable();

const output = join(testOptions.outputDirectory, adder.config.metadata.id, template, uid());
await mkdir(output, { recursive: true });
const output = join(testOptions.outputDirectory, adder.config.metadata.id, template, uid());
await mkdir(output, { recursive: true });

const workingDirectory = await prepareWorkspaceWithTemplate(
output,
Expand All @@ -90,16 +96,121 @@ export async function runAdderTests(
await prompts.textPrompt('Browser opened! Press any key to continue!');
}

await runTests(page, adder, options);
} finally {
await stopBrowser(browser, page);
await stopDevServer(devServer);
remoteControl.disable();
}
await runTests(page, adder, options);
} finally {
await stopBrowser(browser, page);
await stopDevServer(devServer);
}
}

export type AdderError = {
adder: string;
template: string;
message: string;
} & Error;

export async function runTestCases(testCases: Map<string, TestCase[]>, testOptions: TestOptions) {
const asyncTasks: Array<() => Promise<void>> = [];
const syncTasks: Array<() => Promise<void>> = [];
const asyncTestCaseInputs: TestCase[] = [];
const syncTestCaseInputs: TestCase[] = [];
for (const values of testCases.values()) {
for (const testCase of values) {
const taskExecutor = async () => {
try {
await runAdderTests(testCase.template, testCase.adder, testCase.options, testOptions);
} catch (e) {
const error = e as Error;
const adderError: AdderError = {
name: 'AdderError',
adder: testCase.adder.config.metadata.id,
template: testCase.template,
message: error.message,
};
throw adderError;
}
};

if (testCase.runSynchronously) {
syncTasks.push(taskExecutor);
syncTestCaseInputs.push(testCase);
} else {
asyncTasks.push(taskExecutor);
asyncTestCaseInputs.push(testCase);
}
}
}

let testProgressCount = 0;
const overallTaskCount = asyncTasks.length + syncTasks.length;
const parallelTasks = testOptions.pauseExecutionAfterBrowser ? 1 : ProjectTypesList.length;

const allAsyncResults = await Throttle.raw(asyncTasks, {
failFast: false,
maxInProgress: parallelTasks,
progressCallback: (result) => {
testProgressCount++;
logTestProgress(
testProgressCount,
overallTaskCount,
result.amountResolved,
result.amountRejected,
asyncTestCaseInputs[result.lastCompletedIndex],
);
},
});

const allSyncResults = await Throttle.raw(syncTasks, {
failFast: false,
maxInProgress: 1,
progressCallback: (result) => {
testProgressCount++;
logTestProgress(
testProgressCount,
overallTaskCount,
allAsyncResults.amountResolved + result.amountResolved,
allAsyncResults.amountRejected + result.amountRejected,
syncTestCaseInputs[result.lastCompletedIndex],
);
},
});

const rejectedAsyncPromisesResult = allAsyncResults.rejectedIndexes.map<AdderError>(
(x) => allAsyncResults.taskResults[x] as unknown as AdderError,
);
const rejectedSyncPromisesResult = allSyncResults.rejectedIndexes.map<AdderError>(
(x) => allSyncResults.taskResults[x] as unknown as AdderError,
);

const rejectedPromisesResult = [...rejectedAsyncPromisesResult, ...rejectedSyncPromisesResult];
for (const error of rejectedPromisesResult) {
console.log(`${error.adder} (${error.template}): ${error.message}`);
}

if (rejectedPromisesResult.length > 0) {
console.log('At least one test failed. Exiting.');
process.exit(1);
}

if (testProgressCount != overallTaskCount) {
console.log(
`Number of executed tests (${testProgressCount.toString()}) does not match number of expected tests (${overallTaskCount.toString()}). Tests failed!`,
);
process.exit(1);
}
}

function logTestProgress(
current: number,
total: number,
success: number,
failed: number,
testCaseInput: TestCase,
) {
const length = total.toString().length;
const zeroPad = (num: number) => String(num).padStart(length, '0');

console.log(
`Total: ${zeroPad(current)} / ${total.toString()} Success: ${zeroPad(success)} Failed: ${zeroPad(failed)} (${testCaseInput.adder.config.metadata.id} / ${testCaseInput.template})`,
);
}
22 changes: 11 additions & 11 deletions packages/testing-library/utils/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { join } from "node:path";
import { cp, mkdir, rm, writeFile } from "node:fs/promises";
import { executeCli } from "@svelte-add/core";
import type { TestOptions } from "..";
import type { OptionValues, Question } from "@svelte-add/core/adder/options";
import { downloadProjectTemplates } from "./create-project";
import { join } from 'node:path';
import { cp, mkdir, rm, writeFile } from 'node:fs/promises';
import { executeCli } from '@svelte-add/core';
import type { TestOptions } from '..';
import type { OptionValues, Question } from '@svelte-add/core/adder/options';
import { downloadProjectTemplates } from './create-project';

const templatesDirectory = 'templates';

Expand Down Expand Up @@ -43,10 +43,10 @@ export async function saveOptionsFile(
}

export async function prepareTests(options: TestOptions) {
console.log("deleting old files");
await rm(options.outputDirectory, { recursive: true, force: true });
console.log('deleting old files');
await rm(options.outputDirectory, { recursive: true, force: true });

console.log("downloading project templates");
const templatesOutputDirectory = getTemplatesDirectory(options);
await downloadProjectTemplates(templatesOutputDirectory);
console.log('downloading project templates');
const templatesOutputDirectory = getTemplatesDirectory(options);
await downloadProjectTemplates(templatesOutputDirectory);
}
Loading
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.