1
I have an api that in the file that imports the routes to then link them to the express app I use fast-glob to search for files with the extension *.route.ts
and then import these methods, like this:
app.ts
(file where the express server is)
import express from 'express'
import setupRoutes from './routes'
const app = express()
setupRoutes(app)
export default app
routes.ts
(archive where I import route dynamics)
import { Express, Router } from 'express'
import fg from 'fast-glob'
export default (app: Express): void => {
const router = Router()
app.use('/api', router)
fg.sync('**/src/routes/**route.ts')
.map(async file => (await import(`../../../${file}`)).default(router))
}
routes/sample.route.ts
(an example file of any route)
import { Router, Request, Response } from 'express'
export default (router: Router): void => {
router.post('/sample', (_req: Request, res: Response) => {
res.send('sample!')
})
}
My problem is that when checking my code coverage I can’t check the file line 9 routes.ts
which is where I do a map and then import the files, calling the default function and passing the router (as you can see in the above examples).
I need to know how to check this line since I have to validate import’s. The intention is to improve Code coverage. I know I can force jest to ignore this file but it’s not good practice... Today the test file for the routes is like this:
__tests__/route.test.ts
(test archive for the route archive)
import express from 'express'
import fs from 'fast-glob'
import setupRoutes from '../routes'
describe('routes', () => {
it('should call fast-glob.sync to search all route files', () => {
const syncSpy = jest.spyOn(fs, 'sync')
const app = express()
setupRoutes(app)
expect(syncSpy).toHaveBeenCalled()
})
})
Dude, to be honest with you, there’s no point in testing the
await import
. You simply need to trust that they work, since they are part of the language. : -) Use the magic comment/* istanbul ignore next */
to prevent that line from appearing on report of Overage and be happy.– Luiz Felipe
The intention is not exactly to test this, but rather if the dynamic Imports are correct, to have 100% coverage
– LeandroLuk