Skip to content

Commit

Permalink
add new test
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Nov 11, 2023
1 parent 92f9527 commit 7952040
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/commands/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type DynamicDiagramClient = {
onReload: () => void,
enabled: boolean,
app?: Express, // only for testing purposes
server?: http.Server, // only for testing purposes
}

export default async function (autoImportPath: string | undefined, options: Options): Promise<void> {
Expand Down Expand Up @@ -223,13 +224,13 @@ export async function initializeClient(options: Options, repl: Interface, interp
const server = http.createServer(app)

server.addListener('error', ({ port, code }: { port: string, code: string }) => {
console.info('')
logger.info('')

Check warning on line 227 in src/commands/repl.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/repl.ts#L227

Added line #L227 was not covered by tests
if (code === 'EADDRINUSE') {
console.info(yellow(bold(`⚡ We couldn't start dynamic diagram at port ${port}, because it is already in use. ⚡`)))
logger.info(yellow(bold(`⚡ We couldn't start dynamic diagram at port ${port}, because it is already in use. ⚡`)))

Check warning on line 229 in src/commands/repl.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/repl.ts#L229

Added line #L229 was not covered by tests
// eslint-disable-next-line @typescript-eslint/quotes
console.info(yellow(`Please make sure you don't have another REPL session running in another terminal. \nIf you want to start another instance, you can use "--port xxxx" option, where xxxx should be any available port.`))
logger.info(yellow(`Please make sure you don't have another REPL session running in another terminal. \nIf you want to start another instance, you can use "--port xxxx" option, where xxxx should be any available port.`))

Check warning on line 231 in src/commands/repl.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/repl.ts#L231

Added line #L231 was not covered by tests
} else {
console.info(yellow(bold(`⚡ REPL couldn't be started at port ${port}, error code ["${code}]. ⚡`)))
logger.info(yellow(bold(`⚡ REPL couldn't be started at port ${port}, error code ["${code}]. ⚡`)))

Check warning on line 233 in src/commands/repl.ts

View check run for this annotation

Codecov / codecov/patch

src/commands/repl.ts#L233

Added line #L233 was not covered by tests
}
process.exit(1)
})
Expand Down Expand Up @@ -261,6 +262,7 @@ export async function initializeClient(options: Options, repl: Interface, interp
},
enabled: true,
app,
server,
}
}

Expand Down
15 changes: 14 additions & 1 deletion test/dynamicDiagramClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,23 @@ describe('dynamic diagram client', () => {
})

it('should work for root path', async () => {
const { enabled, app } = await initializeClient(options, repl, interpreter)
const { enabled, app, server } = await initializeClient(options, repl, interpreter)
expect(enabled).to.be.true
const result = await chai.request(app).get('/index.html')
expect(result).to.have.status(200)
server!.close()
})

it('should return a fake client if noDiagram is set', async () => {
const noDiagramOptions = {
...options,
noDiagram: true,
}
const { enabled, app } = await initializeClient(noDiagramOptions, repl, interpreter)
expect(enabled).to.be.false
expect(app).to.be.undefined
})

// testing failure cases are extremely complicated due to server listeners and async notifications

})

0 comments on commit 7952040

Please sign in to comment.