Skip to content

Commit 5d390ea

Browse files
authored
chore: unplugin ct generator (#23)
1 parent e03c6db commit 5d390ea

File tree

5 files changed

+17
-40
lines changed

5 files changed

+17
-40
lines changed

assets/playwright.config.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
// @ts-check
2-
const { devices } = require('@playwright/test');
3-
//--begin-ct
4-
const ct = require('{{ctPackageName}}');
5-
//--end-ct
2+
const { devices } = require('{{testRunnerImport}}');
63

74
/**
85
* Read environment variables from file.
@@ -34,10 +31,6 @@ const config = {
3431
workers: process.env.CI ? 1 : undefined,
3532
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
3633
reporter: 'html',
37-
//--begin-ct
38-
/* Enable component testing */
39-
plugins: [ ct() ],
40-
//--end-ct
4134
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
4235
use: {
4336
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
@@ -47,6 +40,11 @@ const config = {
4740

4841
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
4942
trace: 'on-first-retry',
43+
//--begin-ct
44+
45+
/* Port to use for Playwright component endpoint. */
46+
vitePort: 3100,
47+
//--end-ct
5048
},
5149

5250
/* Configure projects for major browsers */

assets/playwright.config.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import type { PlaywrightTestConfig } from '@playwright/test';
2-
import { devices } from '@playwright/test';
3-
//--begin-ct
4-
import ct from '{{ctPackageName}}';
5-
//--end-ct
1+
import type { PlaywrightTestConfig } from '{{testRunnerImport}}';
2+
import { devices } from '{{testRunnerImport}}';
63

74
/**
85
* Read environment variables from file.
@@ -32,10 +29,6 @@ const config: PlaywrightTestConfig = {
3229
workers: process.env.CI ? 1 : undefined,
3330
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
3431
reporter: 'html',
35-
//--begin-ct
36-
/* Enable component testing */
37-
plugins: [ ct() ],
38-
//--end-ct
3932
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
4033
use: {
4134
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
@@ -45,6 +38,11 @@ const config: PlaywrightTestConfig = {
4538

4639
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
4740
trace: 'on-first-retry',
41+
//--begin-ct
42+
43+
/* Port to use for Playwright component endpoint. */
44+
vitePort: 3100,
45+
//--end-ct
4846
},
4947

5048
/* Configure projects for major browsers */

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-playwright",
3-
"version": "1.17.112",
3+
"version": "1.17.113",
44
"description": "Getting started with writing end-to-end tests with Playwright.",
55
"repository": "github:Microsoft/playwright",
66
"homepage": "https://playwright.dev",

src/generator.ts

+2-21
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ export class Generator {
130130
let ctPackageName = '';
131131
if (answers.framework) {
132132
ctPackageName = `@playwright/experimental-ct-${answers.framework}`;
133-
sections.set('ct', 'show');
134133
installExamples = false;
134+
sections.set('ct', 'show');
135135
} else {
136136
sections.set('ct', 'hide');
137137
}
138138

139139
files.set(`playwright.config.${fileExtension}`, executeTemplate(this._readAsset(`playwright.config.${fileExtension}`), {
140140
testDir: answers.testDir || '',
141-
ctPackageName,
141+
testRunnerImport: ctPackageName || '@playwright/test',
142142
}, sections));
143143

144144
if (answers.installGitHubActions) {
@@ -181,11 +181,6 @@ export class Generator {
181181

182182
const jsTemplate = this._readAsset(path.join('playwright', 'index.js'));
183183
files.set(`playwright/index.${extension}`, jsTemplate);
184-
185-
if (answers.language === 'TypeScript') {
186-
files.set(`playwright/types.d.ts`, `import '${ctPackageName}';\n`);
187-
this._patchTsconfigJSON();
188-
}
189184
}
190185

191186
const browsersSuffix = this.options.browser ? ' ' + this.options.browser.join(' ') : '';
@@ -226,20 +221,6 @@ export class Generator {
226221
await createFiles(this.rootDir, files, true);
227222
}
228223

229-
private async _patchTsconfigJSON() {
230-
const tsconfigFile = path.join(this.rootDir, 'tsconfig.json');
231-
const files = new Map<string, string>();
232-
if (!fs.existsSync(tsconfigFile)) {
233-
files.set(`tsconfig.json`, this._readAsset(path.join('tsconfig.json')));
234-
} else {
235-
const tsconfigJSON = fs.readFileSync(path.join(this.rootDir, 'tsconfig.json'), 'utf-8');
236-
const newJSON = tsconfigJSON.replace(/("include"[\s\S]*:[\s\S]\[[\s\S]*"src")/m, '$1, "playwright/types.d.ts"');
237-
if (!tsconfigJSON.includes('playwright') && tsconfigJSON !== newJSON)
238-
files.set('tsconfig.json', newJSON);
239-
}
240-
await createFiles(this.rootDir, files, true);
241-
}
242-
243224
private _printEpilogue(answers: PromptOptions) {
244225
console.log(colors.green('✔ Success!') + ' ' + colors.bold(`Created a Playwright Test project at ${this.rootDir}`));
245226
const pathToNavigate = path.relative(process.cwd(), this.rootDir);

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function determinePackageManager(rootDir: string): 'yarn' | 'npm' {
6565

6666
export function executeTemplate(input: string, args: Record<string, string>, sections: Map<string, 'show' | 'hide' | 'comment'>): string {
6767
for (const key in args)
68-
input = input.replace(`{{${key}}}`, args[key]);
68+
input = input.replace(new RegExp('{{' + key + '}}', 'g'), args[key]);
6969
const result: string[] = [];
7070
let mode: 'show' | 'hide' | 'comment' = 'show';
7171
let indent = '';

0 commit comments

Comments
 (0)