Skip to content

Commit 7000ffe

Browse files
authored
Fix type declaration (#248)
* Fix type declaration * Fixes * Move types to global * Fix tsconfig files * Fix tests
1 parent dbbebf1 commit 7000ffe

File tree

7 files changed

+95
-111
lines changed

7 files changed

+95
-111
lines changed

src/PlaywrightEnvironment.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import type {
1010
JestPlaywrightConfig,
1111
GenericBrowser,
1212
BrowserType,
13-
JestPlaywrightJestConfig,
13+
JestPlaywrightProjectConfig,
1414
ConnectOptions,
15-
} from './types'
15+
} from '../types/global'
1616
import {
1717
CHROMIUM,
1818
IMPORT_KIND_PLAYWRIGHT,
@@ -82,10 +82,10 @@ export const getPlaywrightEnv = (basicEnv = 'node'): unknown => {
8282
: 'jest-environment-jsdom')
8383

8484
return class PlaywrightEnvironment extends RootEnv {
85-
readonly _config: JestPlaywrightJestConfig
85+
readonly _config: JestPlaywrightProjectConfig
8686
_jestPlaywrightConfig!: JestPlaywrightConfig
8787

88-
constructor(config: JestPlaywrightJestConfig) {
88+
constructor(config: JestPlaywrightProjectConfig) {
8989
super(config)
9090
this._config = config
9191
}

src/PlaywrightRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
WsEndpointType,
1717
JestPlaywrightTest,
1818
JestPlaywrightConfig,
19-
} from './types'
19+
} from '../types/global'
2020
import {
2121
checkBrowserEnv,
2222
checkDeviceEnv,

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { JestPlaywrightConfig, ConnectOptions } from './types'
1+
import type { JestPlaywrightConfig, ConnectOptions } from '../types/global'
22

33
export const IMPORT_KIND_PLAYWRIGHT = 'playwright'
44

src/types.d.ts

Lines changed: 0 additions & 88 deletions
This file was deleted.

src/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33
import * as Utils from './utils'
44
import { DEFAULT_CONFIG, CHROMIUM, FIREFOX } from './constants'
5-
import type { BrowserType } from './types'
5+
import type { BrowserType } from '../types/global'
66

77
const {
88
readConfig,

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
PlaywrightRequireType,
99
SkipOption,
1010
Options,
11-
} from './types'
11+
} from '../types/global'
1212
import {
1313
CHROMIUM,
1414
DEFAULT_CONFIG,

types/global.d.ts

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
import {
2-
Page,
32
Browser,
43
BrowserContext,
4+
Page,
5+
BrowserContextOptions,
6+
LaunchOptions,
57
BrowserType as PlaywrightBrowserType,
68
ChromiumBrowser,
79
FirefoxBrowser,
810
WebKitBrowser,
11+
devices,
912
} from 'playwright-core'
13+
import { Config as JestConfig } from '@jest/types'
14+
import { Context } from 'jest-runner/build/types'
15+
import { Test } from 'jest-runner'
16+
import { JestProcessManagerOptions } from 'jest-process-manager'
1017

11-
type BrowserType = 'chromium' | 'firefox' | 'webkit'
18+
// TODO Find out flexable ways to reuse constants
19+
declare const IMPORT_KIND_PLAYWRIGHT = 'playwright'
1220

13-
type GenericBrowser = PlaywrightBrowserType<
14-
WebKitBrowser | ChromiumBrowser | FirefoxBrowser
15-
>
21+
declare const CHROMIUM = 'chromium'
22+
declare const FIREFOX = 'firefox'
23+
declare const WEBKIT = 'webkit'
1624

17-
type SkipOption = {
25+
declare const LAUNCH = 'LAUNCH'
26+
declare const PERSISTENT = 'PERSISTENT'
27+
declare const SERVER = 'SERVER'
28+
29+
export type BrowserType = typeof CHROMIUM | typeof FIREFOX | typeof WEBKIT
30+
31+
export type SkipOption = {
1832
browsers: BrowserType[]
1933
devices?: string[] | RegExp
2034
}
2135

36+
export type GenericBrowser = PlaywrightBrowserType<
37+
WebKitBrowser | ChromiumBrowser | FirefoxBrowser
38+
>
39+
2240
type ContextOptions = Parameters<GenericBrowser['connect']>[0]
2341

2442
interface JestPlaywright {
@@ -83,16 +101,15 @@ interface JestParams<T> {
83101
(options: T, name: string, fn?: jest.ProvidesCallback, timeout?: number): void
84102
}
85103

86-
// TODO Replace any
87-
interface JestPlaywrightDebug extends JestParams<any> {
104+
interface JestPlaywrightTestDebug extends JestParams<JestPlaywrightConfig> {
88105
(name: string, fn?: jest.ProvidesCallback, timeout?: number): void
89-
skip: JestParams<any> | JestPlaywrightDebug
90-
only: JestParams<any> | JestPlaywrightDebug
106+
skip: JestParams<JestPlaywrightConfig> | JestPlaywrightTestDebug
107+
only: JestParams<JestPlaywrightConfig> | JestPlaywrightTestDebug
91108
}
92109

93-
interface JestPlaywrightConfig extends JestParams<any> {
94-
skip: JestParams<any> | JestPlaywrightConfig
95-
only: JestParams<any> | JestPlaywrightConfig
110+
interface JestPlaywrightTestConfig extends JestParams<JestPlaywrightConfig> {
111+
skip: JestParams<JestPlaywrightConfig> | JestPlaywrightTestConfig
112+
only: JestParams<JestPlaywrightConfig> | JestPlaywrightTestConfig
96113
}
97114

98115
declare global {
@@ -105,8 +122,63 @@ declare global {
105122
namespace jest {
106123
interface It {
107124
jestPlaywrightSkip: JestParams<SkipOption>
108-
jestPlaywrightDebug: JestPlaywrightDebug
109-
jestPlaywrightConfig: JestPlaywrightConfig
125+
jestPlaywrightDebug: JestPlaywrightTestDebug
126+
jestPlaywrightConfig: JestPlaywrightTestConfig
110127
}
111128
}
112129
}
130+
131+
export type CustomDeviceType = BrowserContextOptions & {
132+
name: string
133+
}
134+
135+
export type DeviceType = CustomDeviceType | string | null
136+
137+
export type WsEndpointType = string | null
138+
139+
export type SelectorType = {
140+
script: string | Function | { path?: string; content?: string }
141+
name: string
142+
}
143+
144+
export type PlaywrightRequireType = BrowserType | typeof IMPORT_KIND_PLAYWRIGHT
145+
146+
export interface Playwright {
147+
name: PlaywrightRequireType
148+
instance: GenericBrowser
149+
devices: typeof devices
150+
}
151+
152+
type LaunchType = typeof LAUNCH | typeof SERVER | typeof PERSISTENT
153+
154+
type Options<T> = T & Partial<Record<BrowserType, T>>
155+
156+
export type ConnectOptions = Parameters<GenericBrowser['connect']>[0]
157+
158+
export interface JestPlaywrightConfig {
159+
launchType?: LaunchType
160+
launchOptions?: Options<LaunchOptions>
161+
connectOptions?: Options<ConnectOptions>
162+
contextOptions?: Options<BrowserContextOptions>
163+
userDataDir?: string
164+
exitOnPageError: boolean
165+
browsers: BrowserType[]
166+
devices?: (string | CustomDeviceType)[] | RegExp
167+
serverOptions?: JestProcessManagerOptions
168+
selectors?: SelectorType[]
169+
collectCoverage: boolean
170+
}
171+
172+
export interface JestPlaywrightProjectConfig extends JestConfig.ProjectConfig {
173+
browserName: BrowserType
174+
wsEndpoint: WsEndpointType
175+
device: DeviceType
176+
}
177+
178+
interface JestPlaywrightContext extends Context {
179+
config: JestPlaywrightProjectConfig
180+
}
181+
182+
export interface JestPlaywrightTest extends Test {
183+
context: JestPlaywrightContext
184+
}

0 commit comments

Comments
 (0)