diff --git a/src/generator.ts b/src/generator.ts index a6a4460..fcc2cbc 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -214,6 +214,13 @@ export class Generator { files.set(`playwright/index.${extension}`, jsTemplate); } + if (!this._hasDependency('@types/node')) { + commands.push({ + name: 'Installing Types', + command: packageManager.installDevDependency(`@types/node`), + }); + } + const browsersSuffix = this.options.browser ? ' ' + this.options.browser.join(' ') : ''; if (answers.installPlaywrightBrowsers) { commands.push({ @@ -225,6 +232,15 @@ export class Generator { return { files, commands }; } + private _hasDependency(pkg: string) { + try { + const packageJSON = JSON.parse(fs.readFileSync(path.join(this.rootDir, 'package.json'), 'utf-8')); + return packageJSON.dependencies?.[pkg] || packageJSON.devDependencies?.[pkg] || packageJSON.optionalDependencies?.[pkg]; + } catch (e) { + return false; + } + } + private _patchGitIgnore() { const gitIgnorePath = path.join(this.rootDir, '.gitignore'); let gitIgnore = ''; diff --git a/tests/integration.spec.ts b/tests/integration.spec.ts index 997d646..785e640 100644 --- a/tests/integration.spec.ts +++ b/tests/integration.spec.ts @@ -32,12 +32,15 @@ test('should generate a project in the current directory', async ({ run, package if (packageManager === 'npm') { expect(stdout).toContain('Initializing NPM project (npm init -y)…'); expect(stdout).toContain('Installing Playwright Test (npm install --save-dev @playwright/test)…'); + expect(stdout).toContain('Installing Types (npm install --save-dev @types/node)…'); } else if (packageManager === 'yarn') { expect(stdout).toContain('Initializing Yarn project (yarn init -y)…'); expect(stdout).toContain('Installing Playwright Test (yarn add --dev @playwright/test)…'); + expect(stdout).toContain('Installing Types (yarn add --dev @types/node)…'); } else if (packageManager === 'pnpm') { expect(stdout).toContain('pnpm init'); // pnpm command outputs name in different case, hence we are not testing the whole string expect(stdout).toContain('Installing Playwright Test (pnpm add --save-dev @playwright/test)…'); + expect(stdout).toContain('Installing Types (pnpm add --save-dev @types/node)…'); } expect(stdout).toContain('npx playwright install' + process.platform === 'linux' ? ' --with-deps' : ''); });