Skip to content

Commit

Permalink
test coverage for js watch output to stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jan 17, 2023
1 parent a691c04 commit 345ac51
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions scripts/js-api-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6806,6 +6806,107 @@ let childProcessTests = {
testKeepAlivePingPromise,
])
},

async testWatchStdoutChildProcess({ testDir, esbuild }) {
const sequence = [
{
input: 'console.log(1+2)',
stdout: ['console.log(1 + 2);'],
stderr: ['[watch] build finished, watching for changes...'],
},
{
input: 'console.log(2+3)',
stdout: ['console.log(2 + 3);'],
stderr: ['[watch] build started (change: "in.js")', '[watch] build finished'],
},
{
input: 'console.log(3+4)',
stdout: ['console.log(3 + 4);'],
stderr: ['[watch] build started (change: "in.js")', '[watch] build finished'],
},
]

const infile = path.join(testDir, 'in.js')
const file = path.join(testDir, 'build.js')
await writeFileAsync(infile, sequence[0].input)
await writeFileAsync(file, `
const esbuild = require(${JSON.stringify(esbuild.ESBUILD_PACKAGE_PATH)})
esbuild.context({
entryPoints: [${JSON.stringify(infile)}],
logLevel: 'info',
}).then(ctx => ctx.watch())
`)

// Start the child
const maxSeconds = 60
const child = child_process.spawn('node', [file], {
cwd: testDir,
stdio: ['inherit', 'pipe', 'pipe'],
timeout: maxSeconds * 1000,
})

// Make sure the child is always killed
try {
for (const { input, stdout: expectedStdout, stderr: expectedStderr } of sequence) {
let totalStdout = ''
let totalStderr = ''
let stdoutBuffer = ''
let stderrBuffer = ''
const onstdout = data => {
totalStdout += data
stdoutBuffer += data
check()
}
const onstderr = data => {
totalStderr += data
stderrBuffer += data
check()
}
let check = () => { }

child.stdout.on('data', onstdout)
child.stderr.on('data', onstderr)

await new Promise((resolve, reject) => {
const seconds = 30
const timeout = setTimeout(() => reject(new Error(
`Watch mode + stdout test failed to match expected output after ${seconds} seconds
input: ${JSON.stringify(input)}
stdout: ${JSON.stringify(totalStdout)}
stderr: ${JSON.stringify(totalStderr)}
`)), seconds * 1000)

check = () => {
let index

while ((index = stdoutBuffer.indexOf('\n')) >= 0) {
const line = stdoutBuffer.slice(0, index)
stdoutBuffer = stdoutBuffer.slice(index + 1)
if (line === expectedStdout[0]) expectedStdout.shift()
}

while ((index = stderrBuffer.indexOf('\n')) >= 0) {
const line = stderrBuffer.slice(0, index)
stderrBuffer = stderrBuffer.slice(index + 1)
if (line === expectedStderr[0]) expectedStderr.shift()
}

if (!expectedStdout.length && !expectedStderr.length) {
clearTimeout(timeout)
resolve()
}
}

writeFileAtomic(infile, input)
})

child.stdout.off('data', onstdout)
child.stderr.off('data', onstderr)
}
} finally {
child.kill()
}
},
}

async function assertSourceMap(jsSourceMap, source) {
Expand Down

0 comments on commit 345ac51

Please sign in to comment.