Skip to content

Commit

Permalink
run smoke-test server with t.before
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Apr 14, 2021
1 parent 3fac37c commit 9de18db
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
6 changes: 3 additions & 3 deletions smoke-tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ t.cleanSnapshot = s => s.split(cwd).join('{CWD}')
.replace(/^npm@.*\ /mg, 'npm ')

// setup server
const registryServer = require('./server.js')
const { registry } = registryServer
t.test('setup server', { bail: true, buffered: false }, registryServer)
const { start, stop, registry } = require('./server.js')
t.before(start)
t.teardown(stop)

// setup fixtures
const path = t.testdir({
Expand Down
31 changes: 13 additions & 18 deletions smoke-tests/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ let advisoryBulkResponse = null
let failAdvisoryBulk = false
let auditResponse = null
let failAudit = false
const startServer = cb => {
const server = module.exports.server = http.createServer((req, res) => {
const startServer = () => new Promise((res, rej) => {
const server = exports.server = http.createServer((req, res) => {
res.setHeader('connection', 'close')

if (req.url === '/-/npm/v1/security/advisories/bulk') {
Expand Down Expand Up @@ -226,51 +226,46 @@ const startServer = cb => {
res.end(er.stack)
}
})
server.listen(PORT, cb)
}

module.exports = t => startServer(() => {
t.parent.teardown(() => module.exports.server.close())
t.end()
server.listen(PORT, res)
})

module.exports.auditResponse = value => {
exports.auditResponse = value => {
if (auditResponse && auditResponse !== value) {
throw new Error('setting audit response, but already set\n' +
'(did you forget to call the returned function on teardown?)')
}
auditResponse = value
return () => auditResponse = null
}
module.exports.failAudit = () => {
exports.failAudit = () => {
failAudit = true
return () => failAudit = false
}

module.exports.advisoryBulkResponse = value => {
exports.advisoryBulkResponse = value => {
if (advisoryBulkResponse && advisoryBulkResponse !== value) {
throw new Error('setting advisory bulk response, but already set\n' +
'(did you forget to call the returned function on teardown?)')
}
advisoryBulkResponse = value
return () => advisoryBulkResponse = null
}
module.exports.failAdvisoryBulk = () => {
exports.failAdvisoryBulk = () => {
failAdvisoryBulk = true
return () => failAdvisoryBulk = false
}

module.exports.registry = `http://localhost:${PORT}/`
exports.registry = `http://localhost:${PORT}/`

module.exports.start = startServer
module.exports.stop = () => module.exports.server.close()
exports.start = startServer
exports.stop = () => exports.server.close()

if (require.main === module) {
startServer(() => {
startServer().then(() => {
console.log(`Mock registry live at:
${module.exports.registry}
${exports.registry}
Press ^D to close gracefully.`)
})
process.openStdin()
process.stdin.on('end', () => module.exports.server.close())
process.stdin.on('end', () => exports.stop())
}

0 comments on commit 9de18db

Please sign in to comment.