Skip to content

Commit 6a4c0bb

Browse files
authored
feat(adapter): update createConfig helper to use deep merge (#38)
* remove custom overrides type * use deep merge to combine default config w/ overrides * add `DeepPartial` type for overrides * swap to deepmerge package * add to breaking changes * fix rebase errors
1 parent a054d67 commit 6a4c0bb

6 files changed

+49
-46
lines changed

BREAKING_CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ This is a list of all breaking changes introduced in the Stencil Playwright adap
1010
### v0.2.0
1111

1212
- `createStencilPlaywrightConfig` renamed to `createConfig`
13+
- `createConfig` uses a deep merge to combine default config values with overrides

cspell.json

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
{
2-
"ignorePaths": ["**/node_modules/**", "**/third-party/**"],
3-
"words": ["appload", "domcontentloaded", "networkidle", "outfile", "tada", "upvote"]
2+
"ignorePaths": [
3+
"**/node_modules/**",
4+
"**/third-party/**"
5+
],
6+
"words": [
7+
"appload",
8+
"deepmerge",
9+
"domcontentloaded",
10+
"networkidle",
11+
"outfile",
12+
"tada",
13+
"upvote"
14+
]
415
}

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
},
7272
"prettier": "@ionic/prettier-config",
7373
"dependencies": {
74+
"deepmerge": "^4.3.1",
7475
"glob": "^10.3.10"
7576
}
7677
}

src/create-config.ts

+28-41
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
import { PlaywrightTestConfig } from '@playwright/test';
2+
import merge from 'deepmerge';
23

34
import { loadConfigMeta } from './load-config-meta';
45
import { ProcessConstants } from './process-constants';
56

6-
/**
7-
* Options for creating a Playwright config for Stencil projects. This extends the default Playwright config
8-
* with some additional options specific to overriding options for the Playwright dev server.
9-
*/
10-
export type CreateStencilPlaywrightConfigOptions = Partial<PlaywrightTestConfig> & {
11-
/**
12-
* The command to execute to start the dev server. This can be a bash command or a local npm script.
13-
*
14-
* Defaults to `npm start -- --no-open`.
15-
*/
16-
webServerCommand?: string;
17-
/**
18-
* The maximum time to wait for the dev server to start before aborting.
19-
*
20-
* Defaults to `60000` (60 seconds).
21-
*/
22-
webServerTimeout?: number;
23-
};
7+
// Recursively apply the `Partial` type to all nested object types in the provided generic type
8+
type DeepPartial<T> = T extends object
9+
? {
10+
[P in keyof T]?: DeepPartial<T[P]>;
11+
}
12+
: T;
2413

2514
/**
2615
* Helper function to easily create a Playwright config for Stencil projects. This function will
@@ -31,7 +20,9 @@ export type CreateStencilPlaywrightConfigOptions = Partial<PlaywrightTestConfig>
3120
* @param overrides Values to override in the default config. Any Playwright config option can be overridden.
3221
* @returns A {@link PlaywrightTestConfig} object
3322
*/
34-
export const createConfig = async (overrides?: CreateStencilPlaywrightConfigOptions): Promise<PlaywrightTestConfig> => {
23+
export const createConfig = async (
24+
overrides: DeepPartial<PlaywrightTestConfig> = {},
25+
): Promise<PlaywrightTestConfig> => {
3526
const { webServerUrl, baseURL, stencilEntryPath, stencilNamespace } = await loadConfigMeta();
3627

3728
// Set the Stencil namespace and entry path as environment variables so we can use them when constructing
@@ -40,27 +31,23 @@ export const createConfig = async (overrides?: CreateStencilPlaywrightConfigOpti
4031
process.env[ProcessConstants.STENCIL_NAMESPACE] = stencilNamespace;
4132
process.env[ProcessConstants.STENCIL_ENTRY_PATH] = stencilEntryPath;
4233

43-
// Strip off overrides that are not top-level playwright options. We need to remove
44-
// this properties to avoid runtime errors since they are not valid playwright options.
45-
const playwrightOverrides = JSON.parse(JSON.stringify(overrides ?? {}));
46-
delete playwrightOverrides.webServerCommand;
47-
delete playwrightOverrides.webServerTimeout;
48-
49-
return {
50-
testMatch: '*.e2e.ts',
51-
use: {
52-
baseURL,
53-
},
54-
webServer: {
55-
command: overrides?.webServerCommand ?? 'stencil build --dev --watch --serve --no-open',
56-
url: webServerUrl,
57-
reuseExistingServer: !!!process.env.CI,
58-
// Max time to wait for dev server to start before aborting, defaults to 60000 (60 seconds)
59-
timeout: overrides?.webServerTimeout ?? undefined,
60-
// Pipe the dev server output to the console
61-
// Gives visibility to the developer if the dev server fails to start
62-
stdout: 'pipe',
34+
return merge<DeepPartial<PlaywrightTestConfig>>(
35+
{
36+
testMatch: '*.e2e.ts',
37+
use: {
38+
baseURL,
39+
},
40+
webServer: {
41+
command: 'stencil build --dev --watch --serve --no-open',
42+
url: webServerUrl,
43+
reuseExistingServer: !!!process.env.CI,
44+
// Max time to wait for dev server to start before aborting, defaults to 60000 (60 seconds)
45+
timeout: undefined,
46+
// Pipe the dev server output to the console
47+
// Gives visibility to the developer if the dev server fails to start
48+
stdout: 'pipe',
49+
},
6350
},
64-
...playwrightOverrides,
65-
};
51+
overrides,
52+
) as PlaywrightTestConfig;
6653
};

src/test/create-config.spec.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ describe('createConfig', () => {
3131

3232
it('should override the default config', async () => {
3333
const config = await createConfig({
34-
webServerCommand: 'npm start -- --no-open --port 4444',
34+
webServer: {
35+
port: 4444,
36+
},
3537
testDir: 'tests/e2e',
3638
});
3739

@@ -42,10 +44,11 @@ describe('createConfig', () => {
4244
baseURL: 'http://localhost:3333',
4345
},
4446
webServer: {
45-
command: 'npm start -- --no-open --port 4444',
47+
command: 'stencil build --dev --watch --serve --no-open',
4648
url: 'http://localhost:3333/ping',
4749
reuseExistingServer: !process.env.CI,
4850
timeout: undefined,
51+
port: 4444,
4952
stdout: 'pipe',
5053
},
5154
});

0 commit comments

Comments
 (0)