playwright/tests/playwright-test/only-changed.spec.ts

140 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-07-17 10:42:51 +02:00
/**
* 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.
*/
2024-07-17 11:36:35 +02:00
import { test as baseTest, expect } from './playwright-test-fixtures';
2024-07-17 10:42:51 +02:00
import { execSync } from 'node:child_process';
2024-07-17 11:36:35 +02:00
const test = baseTest.extend({
setupRepository: async ({ writeFiles }, use, testInfo) => {
const baseDir = testInfo.outputPath();
const git = (command: string) => execSync(`git ${command}`, { cwd: baseDir });
await use(async () => {
await writeFiles({
'a.spec.ts': `
2024-07-17 10:42:51 +02:00
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
2024-07-17 10:42:51 +02:00
`,
2024-07-17 11:36:35 +02:00
'b.spec.ts': `
2024-07-17 10:42:51 +02:00
import { test, expect } from '@playwright/test';
import { answer, question } from './utils';
test('fails', () => { expect(question).toBe(answer); });
`,
'utils.ts': `
2024-07-17 12:16:55 +02:00
export * from './answer';
export * from './question';
`,
'answer.ts': `
export const answer = 42;
2024-07-17 12:16:55 +02:00
`,
'question.ts': `
export const question = "???";
2024-07-17 10:42:51 +02:00
`,
2024-07-17 11:36:35 +02:00
});
git(`init --initial-branch=main`);
git(`add .`);
git(`commit -m init`);
return git;
});
},
});
2024-07-17 10:42:51 +02:00
2024-07-17 11:36:35 +02:00
test('should detect untracked files', async ({ runInlineTest, setupRepository }) => {
await setupRepository();
const result = await runInlineTest({
'c.spec.ts': `
import { test, expect } from '@playwright/test';
test('fails', () => { expect(1).toBe(2); });
`
2024-07-17 10:42:51 +02:00
}, { 'only-changed': true });
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(result.output).toContain('c.spec.ts');
});
2024-07-17 10:45:41 +02:00
2024-07-17 11:36:35 +02:00
test('should detect changed files', async ({ runInlineTest, setupRepository }) => {
await setupRepository();
2024-07-17 10:45:41 +02:00
const result = await runInlineTest({
'b.spec.ts': `
import { test, expect } from '@playwright/test';
test('fails', () => { expect(1).toBe(3); });
2024-07-17 11:36:35 +02:00
`,
2024-07-17 10:45:41 +02:00
}, { 'only-changed': true });
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(result.output).toContain('b.spec.ts');
});
2024-07-17 11:36:35 +02:00
test('should diff based on base commit', async ({ runInlineTest, setupRepository, writeFiles }) => {
const git = await setupRepository();
await writeFiles({
2024-07-17 11:00:28 +02:00
'b.spec.ts': `
import { test, expect } from '@playwright/test';
test('fails', () => { expect(1).toBe(3); });
2024-07-17 11:36:35 +02:00
`,
});
git('commit -a -m update');
const result = await runInlineTest({}, { 'only-changed': `HEAD~1` });
2024-07-17 11:00:28 +02:00
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(result.output).toContain('b.spec.ts');
});
test.describe('should be smart about PR base reference from CI', () => {
2024-07-17 11:36:35 +02:00
function testCIEnvironment(name: string, envVar: string) {
test(name, async ({ runInlineTest, setupRepository, writeFiles }) => {
const git = await setupRepository();
await writeFiles({
'b.spec.ts': `
import { test, expect } from '@playwright/test';
test('fails', () => { expect(1).toBe(3); });
`,
});
git('commit -a -m update');
const result = await runInlineTest({}, { 'only-changed': true }, { [envVar]: 'HEAD~1' });
2024-07-17 11:36:35 +02:00
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(1);
expect(result.output).toContain('b.spec.ts');
});
2024-07-17 11:36:35 +02:00
}
2024-07-17 11:36:35 +02:00
testCIEnvironment('Github Actions', 'GITHUB_BASE_REF');
testCIEnvironment('Azure DevOps', 'Build.PullRequest.TargetBranch');
});
test('should understand dependency structure', async ({ runInlineTest, setupRepository, writeFiles }) => {
await setupRepository();
await writeFiles({
2024-07-17 12:16:55 +02:00
'question.ts': `
export const question = "what is the answer to life the universe and everything";
`,
});
const result = await runInlineTest({}, { 'only-changed': true });
expect(result.exitCode).toBe(1);
expect(result.failed).toBe(2);
expect(result.output).toContain('a.spec.ts');
expect(result.output).toContain('b.spec.ts');
});