Skip to content

Commit

Permalink
refactor: 10x better types for test kit. (#2931)
Browse files Browse the repository at this point in the history
  • Loading branch information
barak007 authored Dec 29, 2023
1 parent 2c441dd commit 9a33a7e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
22 changes: 15 additions & 7 deletions packages/core-test-kit/src/test-stylable-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { IDirectoryContents, IFileSystem } from '@file-services/types';
import { isAbsolute } from 'path';

export interface TestOptions {
entries: string[];
entries: `/${string}`[];
stylableConfig: TestStylableConfig;
}

Expand All @@ -18,17 +18,20 @@ export type TestStylableConfig = Omit<
resolveNamespace?: StylableConfig['resolveNamespace'];
};

type AddLeadingSlash<T extends string> = T extends `/${infer U}` ? `/${U}` : `/${T}`;
type RemoveRelative<T extends string> = T extends `./${infer U}` ? U : T;

/**
* The test function takes in a single '/entry.st.css' stylesheet string
* or a directory structure and then runs 2 phases
* 1. build stylesheets from entries in a configurable order
* 2. run inline test on all '.st.css' files found
* @param input single '/entry.st.css' string or file system structure
*/
export function testStylableCore(
input: string | IDirectoryContents,
options: Partial<TestOptions> = {}
) {
export function testStylableCore<
const T extends string | IDirectoryContents,
const O extends Partial<TestOptions>
>(input: T, options: O = {} as O) {
// infra
const fs =
options.stylableConfig?.filesystem ||
Expand All @@ -46,14 +49,19 @@ export function testStylableCore(
const entries = options.entries || allSheets;

// transform entries - run build in requested order
const sheets: Record<string, StylableResults> = {};
type Entries = O['entries'] extends string[] ? O['entries'][number] : string;
type HasExternalFS = O['stylableConfig'] extends { filesystem: IFileSystem } ? true : false;
type InputFsKeys = T extends string ? '/entry.st.css' : keyof T;
type Keys = HasExternalFS extends true ? string : InputFsKeys;
type Sheets = Record<AddLeadingSlash<RemoveRelative<Keys & Entries>>, StylableResults>;
const sheets = {} as Sheets;
for (const path of entries) {
if (!isAbsolute(path || '')) {
throw new Error(testStylableCore.errors.absoluteEntry(path));
}
const meta = stylable.analyze(path);
const { exports } = stylable.transform(meta);
sheets[path] = { meta, exports };
sheets[path as keyof Sheets] = { meta, exports };
}

// inline test - build all and test
Expand Down
4 changes: 3 additions & 1 deletion packages/core-test-kit/test/test-stylable-core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ describe(`testStylableCore()`, () => {
entries: [`/a.st.css`, `/b.st.css`],
}
);


// @ts-expect-error entry is not processed
const entryNotProcessed = sheets[`/entry.st.css`];
const a = sheets[`/a.st.css`];
const b = sheets[`/b.st.css`];
Expand All @@ -86,6 +87,7 @@ describe(`testStylableCore()`, () => {
'/a.st.css': ``,
},
{
// @ts-expect-error paths must start with "/"
entries: [`a.st.css`],
}
)
Expand Down

0 comments on commit 9a33a7e

Please sign in to comment.