Skip to content

Commit

Permalink
Refactor watch tests (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim authored Nov 25, 2021
1 parent 0184968 commit 6256aa7
Showing 1 changed file with 48 additions and 61 deletions.
109 changes: 48 additions & 61 deletions test/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ testCb('--watch works', (t) => {
}
})
.catch(t.end)

// Timeout:
setTimeout(() => t.end('test timeout'), 50000)
})

testCb('--watch dependencies', (t) => {
Expand Down Expand Up @@ -145,62 +142,61 @@ testCb('--watch dependencies', (t) => {
}
})
.catch(t.end)

// Timeout:
setTimeout(() => t.end('test timeout'), 50000)
})

testCb("--watch doesn't exit on CssSyntaxError", (t) => {
t.plan(0)
// Doesn't work on CI for some reason
;(process.env.CI ? test.cb.skip : test.cb)(
"--watch doesn't exit on CssSyntaxError",
(t) => {
t.plan(0)

ENV('', ['a.css'])
.then((dir) => {
// Init watcher:
const watcher = chokidar.watch('.', {
cwd: dir,
ignoreInitial: true,
awaitWriteFinish: true,
})
watcher.on('add', (p) => {
if (p === 'output.css') {
// Change to invalid CSS
fs.writeFile(path.join(dir, 'a.css'), '.a { color: red').catch(done)
}
})
ENV('', ['a.css'])
.then((dir) => {
// Init watcher:
const watcher = chokidar.watch('.', {
cwd: dir,
ignoreInitial: true,
awaitWriteFinish: true,
})
watcher.on('add', (p) => {
if (p === 'output.css') {
// Change to invalid CSS
fs.writeFile(path.join(dir, 'a.css'), '.a { color: red').catch(done)
}
})

let killed = false
const cp = exec(
`node ${path.resolve('index.js')} a.css -o output.css -w --no-map`,
{ cwd: dir }
)
cp.on('error', t.end)
cp.stderr.on('data', (chunk) => {
// When error message is printed, kill the process after a timeout
if (~chunk.indexOf('Unclosed block')) {
setTimeout(() => {
killed = true
let killed = false
const cp = exec(
`node ${path.resolve('index.js')} a.css -o output.css -w --no-map`,
{ cwd: dir }
)
cp.on('error', t.end)
cp.stderr.on('data', (chunk) => {
// When error message is printed, kill the process after a timeout
if (~chunk.indexOf('Unclosed block')) {
setTimeout(() => {
killed = true
cp.kill()
}, 1000)
}
})
cp.on('exit', (code) => {
if (!killed)
return t.end(`Should not exit (exited with code ${code})`)
done()
})

function done(err) {
try {
cp.kill()
}, 1000)
} catch {}

t.end(err)
}
})
cp.on('exit', (code) => {
if (!killed) return t.end(`Should not exit (exited with code ${code})`)
done()
})

function done(err) {
try {
cp.kill()
} catch {}

t.end(err)
}
})
.catch(t.end)

// Timeout:
setTimeout(() => t.end('test timeout'), 50000)
})
.catch(t.end)
}
)

testCb('--watch does exit on closing stdin (Ctrl-D/EOF)', (t) => {
t.plan(1)
Expand Down Expand Up @@ -305,9 +301,6 @@ testCb('--watch watches dependencies', (t) => {
})
.catch(t.end)
})

// Timeout:
setTimeout(() => t.end('test timeout'), 50000)
})

testCb('--watch watches directory dependencies', (t) => {
Expand Down Expand Up @@ -407,9 +400,6 @@ testCb('--watch watches directory dependencies', (t) => {
.catch(t.end)
}
)

// Timeout:
setTimeout(() => t.end('test timeout'), 50000)
})

testCb(
Expand Down Expand Up @@ -523,8 +513,5 @@ testCb(
})
.catch(t.end)
})

// Timeout:
setTimeout(() => t.end('test timeout'), 50000)
}
)

0 comments on commit 6256aa7

Please sign in to comment.