1+ // e2e/samples.spec.ts
2+ import { test , expect } from '@playwright/test' ;
3+ import fs from 'fs' ;
4+ import path from 'path' ;
5+ import childProcess from 'child_process' ; // Import childProcess
6+
7+ const samplesDir = path . join ( __dirname , '..' , 'samples' ) ;
8+
9+ const sampleFolders = fs . readdirSync ( samplesDir ) . filter ( ( file ) => {
10+ return fs . statSync ( path . join ( samplesDir , file ) ) . isDirectory ( ) ;
11+ } ) ;
12+
13+ sampleFolders . forEach ( ( sampleFolder ) => {
14+ test ( `test ${ sampleFolder } ` , async ( { page } ) => {
15+ const url = `http://localhost:8080/samples/${ sampleFolder } /` ;
16+
17+ // START Build the sample
18+ const buildProcess = childProcess . spawn ( 'npm' , [ 'run' , 'build' ] , {
19+ cwd : path . join ( samplesDir , sampleFolder ) ,
20+ stdio : 'inherit' ,
21+ } ) ;
22+
23+ await new Promise ( ( resolve , reject ) => {
24+ buildProcess . on ( 'close' , ( code ) => {
25+ if ( code === 0 ) {
26+ resolve ( true ) ;
27+ } else {
28+ reject ( `Build process exited with code ${ code } ` ) ;
29+ }
30+ } ) ;
31+ } ) ;
32+ // END Build the sample
33+
34+ // START run the preview
35+ const viteProcess = childProcess . spawn ( 'npm' , [ 'run' , 'preview' , '--' , '--port=8080' ] , {
36+ cwd : path . join ( samplesDir , sampleFolder ) ,
37+ stdio : 'inherit' ,
38+ } ) ;
39+
40+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
41+ // END run the preview
42+
43+ try {
44+ await page . goto ( url ) ;
45+ await page . waitForLoadState ( 'domcontentloaded' ) ;
46+ // Add your assertions here
47+ } finally {
48+ viteProcess . kill ( ) ;
49+ }
50+ } ) ;
51+ } ) ;
0 commit comments