Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed Sep 19, 2021
1 parent 0c0fd3d commit 1e73ea4
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { test } from '@playwright/test';

// Element selector overview, for more information see
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { test, expect } from '@playwright/test';

// For a full overview see here: https://playwright.dev/docs/test-assertions/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { test, expect } from '@playwright/test';

test.beforeEach(async ({ context }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"id": 1,
"fullName": "John Doe"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<object data="header.html" style="width: 100%; height: 200px;">
Your browser doesn’t support the object tag.
</object>
</object>
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@

<button>Buy</button>
<button>Buy</button>
<button>Buy</button>
<button>Buy</button>
87 changes: 87 additions & 0 deletions packages/create-playwright/tests/baseFixtures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { test as base } from '@playwright/test';
import { spawn, SpawnOptions } from 'child_process';
import path from 'path';
import fs from 'fs';
import { PromptOptions } from '../src';

type TestFixtures = {
packageManager: 'npm' | 'yarn';
run: (parameters: string[], options: PromptOptions) => Promise<RunResult>,
};

type RunResult = {
exitCode: number,
dir: string,
stdout: string,
exec: typeof spawnAsync
};

function spawnAsync(cmd: string, args: string[], options?: SpawnOptions): Promise<{stdout: string, stderr: string, code: number, error?: Error}> {
const p = spawn(cmd, args, options);

return new Promise(resolve => {
let stdout = '';
let stderr = '';
if (process.env.DEBUG) {
p.stdout.on('data', chunk => process.stdout.write(chunk));
p.stderr.on('data', chunk => process.stderr.write(chunk));
}
if (p.stdout)
p.stdout.on('data', data => stdout += data);
if (p.stderr)
p.stderr.on('data', data => stderr += data);
p.on('close', code => resolve({stdout, stderr, code}));
p.on('error', error => resolve({stdout, stderr, code: 0, error}));
});
}

export const test = base.extend<TestFixtures>({
packageManager: 'npm',
run: async ({ packageManager }, use, testInfo) => {
await use(async (parameters: string[], options: PromptOptions): Promise<RunResult> => {
fs.mkdirSync(testInfo.outputDir, { recursive: true });
const env = packageManager === 'yarn' ? {
'npm_config_user_agent': 'yarn'
} : undefined;
const result = await spawnAsync('node', [path.join(__dirname, '..'), ...parameters], {
shell: true,
cwd: testInfo.outputDir,
env: {
...process.env,
...env,
'TEST_OPTIONS': JSON.stringify(options),
}
});
const execWrapper = (cmd: string, args: string[], options?: SpawnOptions): ReturnType<typeof spawnAsync> => {
return spawnAsync(cmd, args, {
...options,
cwd: testInfo.outputDir,
});
};
return {
exitCode: result.code,
dir: testInfo.outputDir,
stdout: result.stdout,
exec: execWrapper,
};
});
},
});

export const expect = test.expect;
86 changes: 6 additions & 80 deletions packages/create-playwright/tests/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,79 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test as base, expect } from '@playwright/test';
import { spawn, SpawnOptions } from 'child_process';
import { test, expect } from './baseFixtures';
import path from 'path';
import fs from 'fs';

import type { PromptOptions } from '../src';

type TestFixtures = {
packageManager: 'npm' | 'yarn';
run: (parameters: string[], options: PromptOptions) => Promise<RunResult>,
};

type RunResult = {
exitCode: number,
dir: string,
stderr: string,
stdout: string,
};

export function spawnAsync(cmd: string, args: string[], options?: SpawnOptions): Promise<{stdout: string, stderr: string, code: number, error?: Error}> {
const p = spawn(cmd, args, options);

return new Promise(resolve => {
let stdout = '';
let stderr = '';
if (process.env.DEBUG) {
p.stdout.on('data', chunk => process.stdout.write(chunk));
p.stderr.on('data', chunk => process.stderr.write(chunk));
}
if (p.stdout)
p.stdout.on('data', data => stdout += data);
if (p.stderr)
p.stderr.on('data', data => stderr += data);
p.on('close', code => resolve({stdout, stderr, code}));
p.on('error', error => resolve({stdout, stderr, code: 0, error}));
});
}

const test = base.extend<TestFixtures>({
packageManager: 'npm',
run: async ({ packageManager }, use, testInfo) => {
await use(async (parameters: string[], options: PromptOptions): Promise<RunResult> => {
fs.mkdirSync(testInfo.outputDir, { recursive: true });
const env = packageManager === 'yarn' ? {
'npm_config_user_agent': 'yarn'
} : undefined;
const p = spawn('node', [path.join(__dirname, '..'), ...parameters], {
shell: true,
cwd: testInfo.outputDir,
env: {
...process.env,
...env,
'TEST_OPTIONS': JSON.stringify(options),
}
});
let stdout = '';
let stderr = '';
p.stdout.on('data', data => stdout += data.toString());
p.stderr.on('data', data => stderr += data.toString());
let resolve = (result: RunResult) => { };
const waitUntilExit = new Promise<RunResult>(r => resolve = r);
p.on('exit', exitCode => {
resolve({ exitCode, dir: testInfo.outputDir, stdout, stderr });
});
if (process.env.DEBUG) {
p.stdout.on('data', chunk => process.stdout.write(chunk));
p.stderr.on('data', chunk => process.stderr.write(chunk));
}
return await waitUntilExit;
});
},
});

for (const packageManager of ['npm', 'yarn'] as ('npm'|'yarn')[]) {
test.describe(`Package manager: ${packageManager}`, () => {
test.use({packageManager});
Expand Down Expand Up @@ -141,7 +72,7 @@ for (const packageManager of ['npm', 'yarn'] as ('npm'|'yarn')[]) {
});

test('should generate be able to run the examples successfully', async ({ run }) => {
const { exitCode, dir } = await run([], { installGitHubActions: false, testDir: 'tests', language: 'TypeScript', addExamples: true });
const { exitCode, dir, exec } = await run([], { installGitHubActions: false, testDir: 'tests', language: 'TypeScript', addExamples: true });
expect(exitCode).toBe(0);
expect(fs.existsSync(path.join(dir, 'tests/example.spec.ts'))).toBeTruthy();
expect(fs.existsSync(path.join(dir, 'package.json'))).toBeTruthy();
Expand All @@ -152,15 +83,10 @@ for (const packageManager of ['npm', 'yarn'] as ('npm'|'yarn')[]) {
expect(fs.existsSync(path.join(dir, 'tests-examples/1-getting-started.spec.ts'))).toBeTruthy();

let result;
if (packageManager === 'npm') {
result = await spawnAsync('npm', ['run', 'test:e2e-examples'], {
cwd: dir,
});
} else {
result = await spawnAsync('yarn', ['test:e2e-examples'], {
cwd: dir,
});
}
if (packageManager === 'npm')
result = await exec('npm', ['run', 'test:e2e-examples']);
else
result = await exec('yarn', ['test:e2e-examples']);
expect(result.code).toBe(0);
});
});
Expand Down

0 comments on commit 1e73ea4

Please sign in to comment.