Skip to content

Commit

Permalink
add DeepPartial type for overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanner Reits committed Apr 12, 2024
1 parent fb7a515 commit 0986956
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import merge from 'lodash.merge';
import { loadConfigMeta } from './load-config-meta';
import { ProcessConstants } from './process-constants';

// Recursively apply the `Partial` type to all nested object types in the provided generic type
type DeepPartial<T> = T extends object
? {
[P in keyof T]?: DeepPartial<T[P]>;
}
: T;

/**
* Helper function to easily create a Playwright config for Stencil projects. This function will
* automatically load the Stencil config meta to set default values for the Playwright config respecting the
Expand All @@ -13,7 +20,7 @@ import { ProcessConstants } from './process-constants';
* @param overrides Values to override in the default config. Any Playwright config option can be overridden.
* @returns A {@link PlaywrightTestConfig} object
*/
export const createConfig = async (overrides?: Partial<PlaywrightTestConfig>): Promise<PlaywrightTestConfig> => {
export const createConfig = async (overrides?: DeepPartial<PlaywrightTestConfig>): Promise<PlaywrightTestConfig> => {
const { webServerUrl, baseURL, stencilEntryPath, stencilNamespace } = await loadConfigMeta();

// Set the Stencil namespace and entry path as environment variables so we can use them when constructing
Expand All @@ -37,5 +44,5 @@ export const createConfig = async (overrides?: Partial<PlaywrightTestConfig>): P
},
},
overrides,
);
) as PlaywrightTestConfig;
};
5 changes: 3 additions & 2 deletions src/test/create-config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('createConfig', () => {
it('should override the default config', async () => {
const config = await createConfig({
webServer: {
command: 'npm start -- --no-open --port 4444',
port: 4444,
},
testDir: 'tests/e2e',
});
Expand All @@ -43,10 +43,11 @@ describe('createConfig', () => {
baseURL: 'http://localhost:3333',
},
webServer: {
command: 'npm start -- --no-open --port 4444',
command: 'npm start -- --no-open',
url: 'http://localhost:3333/ping',
reuseExistingServer: !process.env.CI,
timeout: undefined,
port: 4444,
},
});
});
Expand Down

0 comments on commit 0986956

Please sign in to comment.