Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit f02ff4b

Browse files
committed
Add test for rebuilds after config change
1 parent fb47a9e commit f02ff4b

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

tests/rebuild.worker.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const fs = require("fs")
2+
const path = require("path")
3+
const postcss = require("postcss")
4+
const tailwind = require("../src/index.js")
5+
const configPath = path.resolve(__dirname, './rebuilds.tailwind.config.js')
6+
7+
function run(input, config = {}, from = null) {
8+
from = from || path.resolve(__filename)
9+
10+
return postcss([tailwind(config)]).process(input, { from })
11+
}
12+
13+
async function runTest() {
14+
let messages = []
15+
let config1 = {
16+
darkMode: 'class',
17+
purge: [path.resolve(__dirname, './rebuilds.test.html')],
18+
corePlugins: { preflight: false },
19+
theme: {
20+
colors: { plorange: 'orange' },
21+
},
22+
plugins: [],
23+
}
24+
25+
let config2 = {
26+
...config1,
27+
theme: {
28+
colors: { plorange: 'purple' },
29+
},
30+
}
31+
32+
let from = path.resolve(__filename)
33+
let css = `
34+
@tailwind utilities;
35+
body { @apply bg-plorange; }
36+
`
37+
38+
fs.writeFileSync(configPath, `module.exports = ${JSON.stringify(config1)};`)
39+
40+
let results = [
41+
await run(css, configPath, `${from}?id=1`),
42+
await run(css, configPath, `${from}?id=2`),
43+
]
44+
45+
results.forEach(result => {
46+
messages.push({
47+
css: result.css,
48+
})
49+
})
50+
51+
await new Promise(resolve => setTimeout(resolve, 100))
52+
53+
fs.writeFileSync(configPath, `module.exports = ${JSON.stringify(config2)};`)
54+
55+
results = [
56+
await run(css, configPath, `${from}?id=1`),
57+
await run(css, configPath, `${from}?id=2`),
58+
]
59+
60+
results.forEach(result => {
61+
messages.push({
62+
css: result.css,
63+
})
64+
})
65+
66+
process.stdout.write(JSON.stringify(messages))
67+
}
68+
69+
runTest().finally(() => {
70+
fs.unlinkSync(configPath)
71+
})

tests/rebuilds.test.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" href="/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Title</title>
8+
<link rel="stylesheet" href="./tailwind.css" />
9+
</head>
10+
<body></body>
11+
</html>

tests/rebuilds.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const path = require('path')
2+
const process = require('child_process')
3+
const { promisify } = require('util')
4+
const exec = promisify(process.exec)
5+
6+
test('builds with multiple sources handle config file changes', async () => {
7+
const filepath = path.resolve(__dirname, './rebuild.worker.js')
8+
9+
const { stdout } = await exec(`node ${filepath}`)
10+
11+
const results = JSON.parse(stdout.trim())
12+
expect(results[0].css).toMatchCss(`
13+
body {
14+
--tw-bg-opacity: 1;
15+
background-color: rgba(255, 165, 0, var(--tw-bg-opacity));
16+
}
17+
`)
18+
expect(results[1].css).toMatchCss(`
19+
body {
20+
--tw-bg-opacity: 1;
21+
background-color: rgba(255, 165, 0, var(--tw-bg-opacity));
22+
}
23+
`)
24+
expect(results[2].css).toMatchCss(`
25+
body {
26+
--tw-bg-opacity: 1;
27+
background-color: rgba(128, 0, 128, var(--tw-bg-opacity));
28+
}
29+
`)
30+
expect(results[3].css).toMatchCss(`
31+
body {
32+
--tw-bg-opacity: 1;
33+
background-color: rgba(128, 0, 128, var(--tw-bg-opacity));
34+
}
35+
`)
36+
})

0 commit comments

Comments
 (0)