Skip to content

Commit 8611ee8

Browse files
chore(testrunner): typescript test files (#2751)
This lets our spec files be .ts instead of just .js. The typescript files will be checked against the public types. Tests are compiled with babel just in time before running them, emulating the jest experience. TypeScript tests are also linted with eslint. I converted keyboard.spec.js as the first demo. I'll follow up converting some more more tests.
1 parent e97badc commit 8611ee8

File tree

9 files changed

+1530
-90
lines changed

9 files changed

+1530
-90
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
parser: '@typescript-eslint/parser',
33
plugins: ['@typescript-eslint', 'notice'],
44
parserOptions: {
5-
project: './tsconfig.json',
5+
project: ['./tsconfig.json', './test/tsconfig.json'],
66
ecmaVersion: 9,
77
sourceType: 'module',
88
},

package-lock.json

Lines changed: 1398 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@
3535
"prepare": "node install-from-github.js",
3636
"build": "node utils/runWebpack.js --mode='development' && tsc -p . && npm run generate-types",
3737
"watch": "node utils/runWebpack.js --mode='development' --watch --silent | tsc -w -p .",
38-
"test-types": "npm run generate-types && npx -p [email protected] tsc -p utils/generate_types/test/tsconfig.json",
39-
"generate-types": "node utils/generate_types/"
38+
"test-types": "npm run generate-types && npx -p [email protected] tsc -p utils/generate_types/test/tsconfig.json && npm run typecheck-tests",
39+
"generate-types": "node utils/generate_types/",
40+
"typecheck-tests": "tsc -p ./test/"
4041
},
4142
"author": {
4243
"name": "Microsoft Corporation"
@@ -56,6 +57,9 @@
5657
"ws": "^6.1.0"
5758
},
5859
"devDependencies": {
60+
"@babel/core": "^7.10.3",
61+
"@babel/preset-env": "^7.10.3",
62+
"@babel/preset-typescript": "^7.10.1",
5963
"@types/debug": "0.0.31",
6064
"@types/extract-zip": "^1.6.2",
6165
"@types/mime": "^2.0.1",
@@ -77,6 +81,7 @@
7781
"formidable": "^1.2.1",
7882
"ncp": "^2.0.0",
7983
"node-stream-zip": "^1.8.2",
84+
"pirates": "^4.0.1",
8085
"pixelmatch": "^4.0.2",
8186
"socksv5": "0.0.6",
8287
"text-diff": "^1.0.1",

test/keyboard.spec.js renamed to test/keyboard.spec.ts

Lines changed: 80 additions & 68 deletions
Large diffs are not rendered by default.

test/navigation.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ describe('Page.goto', function() {
446446
});
447447

448448
/**
449-
* @param {import('../src/frames').Frame} frame
449+
* @param {import('../index').Frame} frame
450450
* @param {TestServer} server
451451
* @param {() => Promise<void>} action
452452
* @param {boolean} isSetContent

test/test.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module.exports = {
8585
'./focus.spec.js',
8686
'./input.spec.js',
8787
'./jshandle.spec.js',
88-
'./keyboard.spec.js',
88+
'./keyboard.spec.ts',
8989
'./mouse.spec.js',
9090
'./navigation.spec.js',
9191
'./network.spec.js',

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
const fs = require('fs');
1919
const utils = require('./utils');
20+
const path = require('path');
21+
const pirates = require('pirates');
22+
const babel = require('@babel/core');
2023
const TestRunner = require('../utils/testrunner/');
2124
const { PlaywrightEnvironment, BrowserTypeEnvironment, BrowserEnvironment, PageEnvironment} = require('./environments.js');
2225

@@ -132,7 +135,18 @@ function collect(browserNames) {
132135
}
133136
}
134137
for (const file of spec.files || []) {
138+
const revert = pirates.addHook((code, filename) => {
139+
const result = babel.transformFileSync(filename, {
140+
presets: [
141+
['@babel/preset-env', {targets: {node: 'current'}}],
142+
'@babel/preset-typescript']
143+
});
144+
return result.code;
145+
}, {
146+
exts: ['.ts']
147+
});
135148
require(file);
149+
revert();
136150
delete require.cache[require.resolve(file)];
137151
}
138152
});

test/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"moduleResolution": "node",
77
"target": "ESNext",
88
"strictNullChecks": false,
9+
"allowSyntheticDefaultImports": true,
910
},
10-
"include": ["*.spec.js", "types.d.ts"]
11+
"include": ["*.spec.js", "types.d.ts", "*.spec.ts"]
1112
}

test/types.d.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
type ServerResponse = import('http').ServerResponse;
22
type IncomingMessage = import('http').IncomingMessage;
33

4-
type Falsy = false|""|0|null|undefined;
4+
type Falsy = false|''|0|null|undefined;
55
interface Expect<T> {
66
toBe(other: T, message?: string): void;
77
toBeFalsy(message?: string): void;
@@ -24,7 +24,12 @@ interface Expect<T> {
2424

2525
type DescribeFunction = ((name: string, inner: () => void) => void) & {fail(condition: boolean): DescribeFunction};
2626

27-
type ItFunction<STATE> = ((name: string, inner: (state: STATE) => Promise<void>) => void) & {fail(condition: boolean): ItFunction<STATE>; repeat(n: number): ItFunction<STATE>};
27+
type ItFunction<STATE> = ((name: string, inner: (state: STATE) => Promise<void>) => void) & {
28+
fail(condition: boolean): ItFunction<STATE>;
29+
skip(condition: boolean): ItFunction<STATE>;
30+
slow(): ItFunction<STATE>;
31+
repeat(n: number): ItFunction<STATE>;
32+
};
2833

2934
type TestRunner<STATE> = {
3035
describe: DescribeFunction;
@@ -50,9 +55,9 @@ interface TestSetup<STATE> {
5055
WIN: boolean;
5156
playwright: typeof import('../index');
5257
browserType: import('../index').BrowserType<import('../index').Browser>;
53-
selectors: import('../src/selectors').Selectors;
58+
selectors: import('../index').Selectors;
5459
expect<T>(value: T): Expect<T>;
55-
defaultBrowserOptions: import('../src/server/browserType').LaunchOptions;
60+
defaultBrowserOptions: import('../index').LaunchOptions;
5661
playwrightPath;
5762
headless: boolean;
5863
ASSETS_DIR: string;
@@ -100,3 +105,20 @@ interface TestServer {
100105
EMPTY_PAGE: string;
101106

102107
}
108+
109+
declare const describe: DescribeFunction;
110+
declare const fdescribe: DescribeFunction;
111+
declare const xdescribe: DescribeFunction;
112+
declare function expect<T>(value: T): Expect<T>;
113+
declare const it: ItFunction<PageState>;
114+
declare const fit: ItFunction<PageState>;
115+
declare const dit: ItFunction<PageState>;
116+
declare const xit: ItFunction<PageState>;
117+
118+
declare const browserType: import('../index').BrowserType<import('../index').Browser>;
119+
120+
121+
// global variables in assets
122+
123+
// keyboard.html
124+
declare function getResult(): string;

0 commit comments

Comments
 (0)