-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from RyanClementsHax/playwright
Add more e2e tests
- Loading branch information
Showing
64 changed files
with
900 additions
and
376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import serialize from 'serialize-javascript' | ||
import getPort from 'get-port' | ||
import { MultiThemePluginOptions } from '@/utils/optionsUtils' | ||
import { | ||
ServerStarted, | ||
StartServerResult, | ||
StopServerCallback, | ||
createIsolatedRepoInstance, | ||
parseClasses | ||
} from '.' | ||
import { type Config as TailwindConfig } from 'tailwindcss' | ||
|
||
export interface OpenOptions { | ||
instanceId: number | ||
titlePath: string[] | ||
} | ||
|
||
export async function openWithConfig( | ||
config: MultiThemePluginOptions, | ||
options: OpenOptions | ||
): Promise<{ url: string; stop: StopServerCallback }> { | ||
const tmpDirName = [ | ||
...options.titlePath.map(x => x.replace(/ /g, '_')), | ||
options.instanceId | ||
].join('-') | ||
|
||
const { instance, isAlreadyInitialized } = await createIsolatedRepoInstance({ | ||
template: 'create-react-app', | ||
tmpDirName | ||
}) | ||
|
||
const buildDir = instance.getBuildDir() | ||
|
||
if (!isAlreadyInitialized) { | ||
const classesToPreventPurging = parseClasses(config) | ||
|
||
const tailwindConfig: TailwindConfig = { | ||
content: ['./src/**/*.{js,jsx,ts,tsx}'], | ||
safelist: classesToPreventPurging, | ||
theme: { | ||
extend: {} | ||
} | ||
} | ||
|
||
const { filePath: tailwindConfigFilePath } = await instance.writeFile( | ||
'tailwind.config.js', | ||
`module.exports = { | ||
...${JSON.stringify(tailwindConfig)}, | ||
plugins: [require('tailwindcss-themer')(${serialize(config)})] | ||
}` | ||
) | ||
|
||
await instance.build({ | ||
command: ['npm', ['run', 'build']], | ||
env: { | ||
TAILWIND_CONFIG_PATH: tailwindConfigFilePath, | ||
BUILD_PATH: buildDir | ||
} | ||
}) | ||
} | ||
|
||
const { url, stop } = await startServerWithRetry({ | ||
maxAttempts: 2, | ||
async startServer() { | ||
const port = await getPort() | ||
return await instance.startServer({ | ||
command: ['npm', ['run', 'serve', '--', buildDir]], | ||
env: { | ||
PORT: port.toFixed(0) | ||
}, | ||
isServerStarted: ({ stdout, template }) => { | ||
const startupLogMatch: RegExpMatchArray | null = stdout.match( | ||
/Accepting connections at\s+http:\/\/localhost:(\d+)\s/ | ||
) | ||
if (startupLogMatch) { | ||
const parsedPort = parseInt(startupLogMatch[1], 10) | ||
|
||
if (port !== parsedPort) { | ||
return { | ||
started: false, | ||
continueWaiting: false, | ||
reason: `Expected ${template} to start on port ${port}, but it started on port ${parsedPort}` | ||
} | ||
} | ||
|
||
return { started: true, url: `http://localhost:${port}` } | ||
} | ||
|
||
return { started: false, continueWaiting: true } | ||
} | ||
}) | ||
} | ||
}) | ||
|
||
return { | ||
url, | ||
stop | ||
} | ||
} | ||
|
||
async function startServerWithRetry({ | ||
maxAttempts, | ||
startServer | ||
}: { | ||
maxAttempts: number | ||
startServer: () => Promise<StartServerResult> | ||
}): Promise<ServerStarted> { | ||
let attemptNumber = 0 | ||
let failedReason = 'unknown' | ||
while (attemptNumber <= maxAttempts) { | ||
attemptNumber++ | ||
if (attemptNumber > 1) { | ||
// eslint-disable-next-line no-console | ||
console.log( | ||
`Retrying (attempt ${attemptNumber}) starting the server because: ${failedReason}` | ||
) | ||
} | ||
|
||
const result = await startServer() | ||
|
||
if (result.started) { | ||
return result | ||
} else { | ||
failedReason = result.reason | ||
} | ||
} | ||
throw new Error( | ||
`Attempted to start server ${attemptNumber} times but couldn't start the server\n\n${failedReason}` | ||
) | ||
} |
Oops, something went wrong.