pull writeFiles into tests

This commit is contained in:
Simon Knott 2024-07-22 14:07:43 +02:00
parent 6ec936f808
commit 9a32dad948
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -17,40 +17,15 @@
import { test as baseTest, expect, playwrightCtConfigText } from './playwright-test-fixtures'; import { test as baseTest, expect, playwrightCtConfigText } from './playwright-test-fixtures';
import { execSync } from 'node:child_process'; import { execSync } from 'node:child_process';
const test = baseTest.extend({ const test = baseTest.extend<{ git(command: string): void }>({
git: async ({ writeFiles }, use, testInfo) => { git: async ({}, use, testInfo) => {
const baseDir = testInfo.outputPath(); const baseDir = testInfo.outputPath();
const git = (command: string) => execSync(`git ${command}`, { cwd: baseDir }); const git = (command: string) => execSync(`git ${command}`, { cwd: baseDir });
await writeFiles({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'b.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
export * from './answer';
export * from './question';
`,
'answer.ts': `
export const answer = 42;
`,
'question.ts': `
export const question = "???";
`,
});
git(`init --initial-branch=main`); git(`init --initial-branch=main`);
git(`config --local user.name "Robert Botman"`); git(`config --local user.name "Robert Botman"`);
git(`config --local user.email "botty@mcbotface.com"`); git(`config --local user.email "botty@mcbotface.com"`);
git(`add .`);
git(`commit -m init`);
await use((command: string) => git(command)); await use((command: string) => git(command));
}, },
@ -58,7 +33,33 @@ const test = baseTest.extend({
test.slow(); test.slow();
test('should detect untracked files', async ({ runInlineTest, git }) => { test('should detect untracked files', async ({ runInlineTest, git, writeFiles }) => {
await writeFiles({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'b.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
export * from './answer';
export * from './question';
`,
'answer.ts': `
export const answer = 42;
`,
'question.ts': `
export const question = "???";
`,
});
git(`add .`);
git(`commit -m init`);
const result = await runInlineTest({ const result = await runInlineTest({
'c.spec.ts': ` 'c.spec.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
@ -72,7 +73,33 @@ test('should detect untracked files', async ({ runInlineTest, git }) => {
}); });
test('should detect changed files', async ({ runInlineTest, git }) => { test('should detect changed files', async ({ runInlineTest, git, writeFiles }) => {
await writeFiles({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'b.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
export * from './answer';
export * from './question';
`,
'answer.ts': `
export const answer = 42;
`,
'question.ts': `
export const question = "???";
`,
});
git(`add .`);
git(`commit -m init`);
const result = await runInlineTest({ const result = await runInlineTest({
'b.spec.ts': ` 'b.spec.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
@ -86,6 +113,32 @@ test('should detect changed files', async ({ runInlineTest, git }) => {
}); });
test('should diff based on base commit', async ({ runInlineTest, git, writeFiles }) => { test('should diff based on base commit', async ({ runInlineTest, git, writeFiles }) => {
await writeFiles({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'b.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
export * from './answer';
export * from './question';
`,
'answer.ts': `
export const answer = 42;
`,
'question.ts': `
export const question = "???";
`,
});
git(`add .`);
git(`commit -m init`);
await writeFiles({ await writeFiles({
'b.spec.ts': ` 'b.spec.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
@ -101,6 +154,32 @@ test('should diff based on base commit', async ({ runInlineTest, git, writeFiles
}); });
test('should understand dependency structure', async ({ runInlineTest, git, writeFiles }) => { test('should understand dependency structure', async ({ runInlineTest, git, writeFiles }) => {
await writeFiles({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'b.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
export * from './answer';
export * from './question';
`,
'answer.ts': `
export const answer = 42;
`,
'question.ts': `
export const question = "???";
`,
});
git(`add .`);
git(`commit -m init`);
await writeFiles({ await writeFiles({
'question.ts': ` 'question.ts': `
export const question = "what is the answer to life the universe and everything"; export const question = "what is the answer to life the universe and everything";
@ -115,13 +194,39 @@ test('should understand dependency structure', async ({ runInlineTest, git, writ
}); });
test('should support watch mode', async ({ git, writeFiles, runWatchTest }) => { test('should support watch mode', async ({ git, writeFiles, runWatchTest }) => {
await writeFiles({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'b.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
export * from './answer';
export * from './question';
`,
'answer.ts': `
export const answer = 42;
`,
'question.ts': `
export const question = "???";
`,
});
git(`add .`);
git(`commit -m init`);
await writeFiles({ await writeFiles({
'b.spec.ts': ` 'b.spec.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
test('fails', () => { expect(1).toBe(3); }); test('fails', () => { expect(1).toBe(3); });
`, `,
}); });
git('commit -a -m update'); git(`commit -a -m update`);
const testProcess = await runWatchTest({}, { 'only-changed': `HEAD~1` }); const testProcess = await runWatchTest({}, { 'only-changed': `HEAD~1` });
await testProcess.waitForOutput('Waiting for file changes.'); await testProcess.waitForOutput('Waiting for file changes.');
@ -174,8 +279,8 @@ test('should suppport component tests', async ({ runInlineTest, git, writeFiles
`, `,
}); });
git('add .'); git(`add .`);
git('commit -m "init components"'); git(`commit -m "init"`);
const result = await runInlineTest({}, { 'workers': 1, 'only-changed': true }); const result = await runInlineTest({}, { 'workers': 1, 'only-changed': true });
@ -200,7 +305,7 @@ test('should suppport component tests', async ({ runInlineTest, git, writeFiles
expect(result2.output).toContain('button2.test.tsx'); expect(result2.output).toContain('button2.test.tsx');
expect(result2.output).not.toContain('button.test.tsx'); expect(result2.output).not.toContain('button.test.tsx');
git('commit -am "update button2 test"'); git(`commit -am "update button2 test"`);
const result3 = await runInlineTest({ const result3 = await runInlineTest({
'src/contents.ts': ` 'src/contents.ts': `
@ -214,14 +319,40 @@ test('should suppport component tests', async ({ runInlineTest, git, writeFiles
test.describe('should work the same if being called in subdirectory', () => { test.describe('should work the same if being called in subdirectory', () => {
test('tracked file', async ({ runInlineTest, git, writeFiles }) => { test('tracked file', async ({ runInlineTest, git, writeFiles }) => {
await writeFiles({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'b.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
export * from './answer';
export * from './question';
`,
'answer.ts': `
export const answer = 42;
`,
'question.ts': `
export const question = "???";
`,
});
git(`add .`);
git(`commit -m init`);
await writeFiles({ await writeFiles({
'tests/c.spec.ts': ` 'tests/c.spec.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
test('fails', () => { expect(1).toBe(2); }); test('fails', () => { expect(1).toBe(2); });
` `
}); });
git('add .'); git(`add .`);
git('commit -a -m "add test"'); git(`commit -a -m "add test"`);
const result = await runInlineTest({ const result = await runInlineTest({
'tests/c.spec.ts': ` 'tests/c.spec.ts': `
@ -235,7 +366,33 @@ test.describe('should work the same if being called in subdirectory', () => {
expect(result.output).toContain('c.spec.ts'); expect(result.output).toContain('c.spec.ts');
}); });
test('untracked file', async ({ runInlineTest, git }) => { test('untracked file', async ({ runInlineTest, git, writeFiles }) => {
await writeFiles({
'a.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'b.spec.ts': `
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
export * from './answer';
export * from './question';
`,
'answer.ts': `
export const answer = 42;
`,
'question.ts': `
export const question = "???";
`,
});
git(`add .`);
git(`commit -m init`);
const result = await runInlineTest({ const result = await runInlineTest({
'tests/c.spec.ts': ` 'tests/c.spec.ts': `
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';