Skip to content

Commit

Permalink
Merge pull request #106 from RyanClementsHax/playwright
Browse files Browse the repository at this point in the history
Add more e2e tests
  • Loading branch information
RyanClementsHax authored Nov 6, 2023
2 parents 2e87d0d + cdef1c6 commit dff8ab2
Show file tree
Hide file tree
Showing 64 changed files with 900 additions and 376 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ module.exports = {

```html
<!-- this example uses pure html for demonstration purposes -->
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand All @@ -162,7 +162,7 @@ You do this by adding a class of the theme's name to whatever you want themed. S

```html
<!-- this example uses pure html for demonstration purposes -->
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand Down Expand Up @@ -237,7 +237,7 @@ require('tailwindcss-themer')({
```

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand All @@ -250,7 +250,7 @@ require('tailwindcss-themer')({
```

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand All @@ -263,7 +263,7 @@ require('tailwindcss-themer')({
```

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand Down Expand Up @@ -332,7 +332,7 @@ require('tailwindcss-themer')({
```

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand All @@ -345,7 +345,7 @@ require('tailwindcss-themer')({
```

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand Down Expand Up @@ -388,10 +388,10 @@ In order to prevent a [flash of unstyled content](https://css-tricks.com/flash-o

### Simultaneous themes

Because this plugin enables themes based on existance of classes, it is possible to have multiple themes enabled at the same time. They can be overlapping or not. Its all up to how you apply the classes!
Because this plugin enables themes in part based on existance of classes, it is possible to have multiple themes enabled at the same time. They can be overlapping or not. Its all up to how you apply the classes!

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand Down Expand Up @@ -656,7 +656,7 @@ As specified above, variants are generated for every named theme you make, even
The theme variant generated for the default theme is `defaultTheme` (e.g. `defaultTheme:rounded-sm`), but this now requires that instead of omitting any theme class to enable the default theme, you explicitly declare you are using the default theme by adding the class of `defaultTheme` to the place you want themed (no other feature is affected by this, using the default theme variant is the only feature that requires you to add the `defaultTheme` class to use). This is because I haven't been able to create a css selector that excludes all parents with any of the other theme classes. If you can make one, feel free to [open up an issue](https://github.com/RyanClementsHax/tailwindcss-themer/issues).

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand All @@ -670,7 +670,7 @@ The theme variant generated for the default theme is `defaultTheme` (e.g. `defau
```

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand All @@ -683,7 +683,7 @@ The theme variant generated for the default theme is `defaultTheme` (e.g. `defau
```

```html
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<!-- ... -->
Expand Down
14 changes: 14 additions & 0 deletions e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"execa": "^8.0.1",
"fs-extra": "^11.1.1",
"get-port": "^7.0.0",
"pidtree": "^0.6.0",
"serialize-javascript": "^6.0.1"
}
}
9 changes: 7 additions & 2 deletions e2e/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,24 @@ export default defineConfig({

/* Configure projects for major browsers */
projects: [
// Runs before all other projects to initialize all int tests builds without concurrency problems
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] }
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] }
use: { ...devices['Desktop Firefox'] },
// Counts on the first project to initialize all int test builds to reuse for a performance boost
dependencies: ['chromium']
},

{
name: 'webkit',
use: { ...devices['Desktop Safari'] }
use: { ...devices['Desktop Safari'] },
// Counts on the first project to initialize all int test builds to reuse for a performance boost
dependencies: ['chromium']
}

/* Test against mobile viewports. */
Expand Down
130 changes: 130 additions & 0 deletions e2e/test_repos/drivers/create-react-app.ts
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}`
)
}
Loading

0 comments on commit dff8ab2

Please sign in to comment.