1
1
import { PlaywrightTestConfig } from '@playwright/test' ;
2
+ import merge from 'deepmerge' ;
2
3
3
4
import { loadConfigMeta } from './load-config-meta' ;
4
5
import { ProcessConstants } from './process-constants' ;
5
6
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 ;
24
13
25
14
/**
26
15
* Helper function to easily create a Playwright config for Stencil projects. This function will
@@ -31,7 +20,9 @@ export type CreateStencilPlaywrightConfigOptions = Partial<PlaywrightTestConfig>
31
20
* @param overrides Values to override in the default config. Any Playwright config option can be overridden.
32
21
* @returns A {@link PlaywrightTestConfig} object
33
22
*/
34
- export const createConfig = async ( overrides ?: CreateStencilPlaywrightConfigOptions ) : Promise < PlaywrightTestConfig > => {
23
+ export const createConfig = async (
24
+ overrides : DeepPartial < PlaywrightTestConfig > = { } ,
25
+ ) : Promise < PlaywrightTestConfig > => {
35
26
const { webServerUrl, baseURL, stencilEntryPath, stencilNamespace } = await loadConfigMeta ( ) ;
36
27
37
28
// 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
40
31
process . env [ ProcessConstants . STENCIL_NAMESPACE ] = stencilNamespace ;
41
32
process . env [ ProcessConstants . STENCIL_ENTRY_PATH ] = stencilEntryPath ;
42
33
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
+ } ,
63
50
} ,
64
- ... playwrightOverrides ,
65
- } ;
51
+ overrides ,
52
+ ) as PlaywrightTestConfig ;
66
53
} ;
0 commit comments