Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 63 additions & 5 deletions e2e/fixtures/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import {test as base} from '@playwright/test';
import {DevServer} from './server';
import path from 'node:path';
import {stat} from 'node:fs/promises';
import {mkdtemp, readFile, rm, stat, writeFile} from 'node:fs/promises';
import {tmpdir} from 'node:os';
import {StorefrontPage} from './storefront';
import {CartUtil} from './cart-utils';
import {DiscountUtil} from './discount-utils';
import {GiftCardUtil} from './gift-card-utils';
import type {MswScenario} from './msw/scenarios';
import {getHandlersForScenario} from './msw/handlers';

export * from '@playwright/test';
export * from './storefront';
export {getTestSecrets, getRequiredSecret} from './test-secrets';
export {CartUtil} from './cart-utils';
export {DiscountUtil} from './discount-utils';
export {GiftCardUtil} from './gift-card-utils';
export {mockCustomerAccountOperation} from './msw/graphql';
export {MSW_SCENARIOS} from './msw/scenarios';

export const test = base.extend<
{storefront: StorefrontPage; cart: CartUtil; discount: DiscountUtil; giftCard: GiftCardUtil},
{
storefront: StorefrontPage;
cart: CartUtil;
discount: DiscountUtil;
giftCard: GiftCardUtil;
},
{forEachWorker: void}
>({
storefront: async ({page}, use) => {
Expand Down Expand Up @@ -47,11 +57,44 @@ const TEST_STORE_KEYS = [

type TestStoreKey = (typeof TEST_STORE_KEYS)[number];

type TestStoreOptions = {
mock?: {
scenario: MswScenario;
};
};

async function createMockEnvFile(envFile: string, scenario: MswScenario) {
const mockEnvDir = await mkdtemp(path.join(tmpdir(), 'hydrogen-e2e-msw-'));
const mockEnvFile = path.join(mockEnvDir, '.env.mock');

const baseEnvContents = await readFile(envFile, 'utf8');
const normalizedEnvContents = baseEnvContents.endsWith('\n')
? baseEnvContents
: `${baseEnvContents}\n`;

const scenarioMeta = getHandlersForScenario(scenario);
/** these variables are required by the CAAPI client */
const caapiVars = scenarioMeta.mocksCustomerAccountApi
? `SHOP_ID="mock-shop"\nPUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID="shp_mock-client-id"\n`
: '';

await writeFile(
mockEnvFile,
`${normalizedEnvContents}HYDROGEN_E2E_MSW_SCENARIO=${scenario}\n${caapiVars}`,
);

return {mockEnvDir, mockEnvFile};
}

export const setTestStore = async (
testStore: TestStoreKey | `https://${string}`,
options: TestStoreOptions = {},
) => {
const isLocal = !testStore.startsWith('https://');
let server: DevServer | null = null;
let mockEnvDir: string | undefined;

const mockScenario = isLocal ? options.mock?.scenario : undefined;

test.use({
baseURL: async ({}, use) => {
Expand All @@ -66,16 +109,31 @@ export const setTestStore = async (

test.afterAll(async () => {
await server?.stop();

if (mockEnvDir) {
await rm(mockEnvDir, {recursive: true, force: true});
}
});

test.beforeAll(async ({}) => {
const filepath = path.resolve(__dirname, `../envs/.env.${testStore}`);
await stat(filepath); // Ensure the file exists
const envFile = path.resolve(__dirname, `../envs/.env.${testStore}`);
await stat(envFile); // Ensure the file exists

let runtimeEnvFile = envFile;

if (mockScenario) {
const mockEnvFiles = await createMockEnvFile(envFile, mockScenario);
runtimeEnvFile = mockEnvFiles.mockEnvFile;
mockEnvDir = mockEnvFiles.mockEnvDir;
}

server = new DevServer({
storeKey: testStore,
customerAccountPush: false,
envFile: filepath,
envFile: runtimeEnvFile,
entry: mockScenario
? path.resolve(__dirname, './msw/entry.ts')
: undefined,
});

await server.start();
Expand Down
Loading
Loading