Skip to content

Commit

Permalink
Try to add beforeAll/afterAll
Browse files Browse the repository at this point in the history
  • Loading branch information
pkirilin committed Jan 3, 2024
1 parent 200b5cd commit 38edcb5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
51 changes: 41 additions & 10 deletions tests/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,49 @@ import { test as base } from '@playwright/test';
import { DockerComposeEnvironment, type StartedDockerComposeEnvironment } from 'testcontainers';

interface MyFixtures {
environment: StartedDockerComposeEnvironment;
// environment: DockerComposeEnvironment;
// containers: StartedDockerComposeEnvironment;

sut: SystemUnderTest;
}

export const test = base.extend<Record<string, unknown>, MyFixtures>({
environment: [
async ({}, use, workerInfo) => {
const environment = await new DockerComposeEnvironment('.', 'docker-compose.yml').up();
await use(environment);
await environment.down();
},
{ scope: 'worker' },
],
export class SystemUnderTest {
private readonly _environment = new DockerComposeEnvironment('.', 'docker-compose.yml');
private _containers?: StartedDockerComposeEnvironment;

public async setUp(): Promise<void> {
this._containers = await this._environment.up();
}

public async tearDown(): Promise<void> {
await this._containers?.down();
}

public get appUrl(): string {
const container = this._containers?.getContainer('web-1');
return `https://${container?.getHost()}:${container?.getFirstMappedPort()}`;
}
}

const sut = new SystemUnderTest();

// export const test = base.extend<Record<string, unknown>, MyFixtures>({
// sut: [
// async ({}, use, workerInfo) => {
// // const environment = new DockerComposeEnvironment('.', 'docker-compose.yml');

// // const containers = await environment.up();
// await use(sut);
// // await containers.down();
// },
// { scope: 'worker' },
// ],
// });

export const test = base.extend<MyFixtures>({
sut: async ({}, use) => {
await use(sut);
},
});

export { expect } from '@playwright/test';
16 changes: 8 additions & 8 deletions tests/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ export default defineConfig({
use: { ...devices['Desktop Chrome'] },
},

// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
// },
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},

// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},

/* Test against mobile viewports. */
// {
Expand Down
13 changes: 9 additions & 4 deletions tests/scenarios/main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { expect } from '@playwright/test';
import { test } from '../fixtures';

test('should display sign in page', async ({ page, environment }) => {
const container = environment.getContainer('web-1');
const url = `https://${container.getHost()}:${container.getFirstMappedPort()}`;
test.beforeAll(async ({ sut }) => {
await sut.setUp();
});

test.afterAll(async ({ sut }) => {
await sut.tearDown();
});

await page.goto(url);
test('should display sign in page', async ({ page, sut }) => {
await page.goto(sut.appUrl);

await expect(page.getByRole('button', { name: /sign in with google/i })).toBeVisible();
});

0 comments on commit 38edcb5

Please sign in to comment.