Echo JS 0.11.0

<~>

tracker1 2691 days ago. link 1 point
Not sure where the down vote came from, it's definitely one of the better universal layouts I've seen...

A few points, would add in jest and tests for everything... would wrap the main server entry point so that it only runs when `!module.parent` and can be fully tested.
faceyspacey 2691 days ago. link 1 point
Thanks brother. Can you tell me more about what you mean by:

"would wrap the main server entry point so that it only runs when `!module.parent` and can be fully tested."

??
tracker1 2690 days ago. link 2 points
Here's an example:

    // server/index.js
    async function main(skip) {
      if (skip) return;

      ... rest of main ...
    }
    main(!!module.parent).catch(console.error);
    export default main;

module.parent exists when you require in a module, in this case index, say for testing... but not when you execute the module directly `node server/index` for example.

by wrapping your main logic in a function with a skip parameter, if you load the `./index.js` inside `index.test.js` it won't start your server process... in this way you can override whatever modules are used, and test that logic.
faceyspacey 2690 days ago. link 1 point
So it's another way to accomplish what you might do with supertest right:

*server/index.js:*
```
export default function startServer() {
  const app = express()

  // ... regular app.use stuff etc

  const server = http.createServer(app)

  if (process.env.NODE_ENV !== 'test') {
    server.listen(process.env.PORT || 80, () => {
      console.log('Listening on %j', server.address())
  })

  return server
}
```

*__tests__/server.js:*
```
import request from 'supertest'
import startServer from '../server'

it('REQUEST: /', async () => {
    const server = startServer()
    const path = '/'
    const res = await request(server).get(path)
    expect(res.status).toEqual(200)
})
```

??
tracker1 2687 days ago. link 1 point
No, it's a way to do unit tests without actually starting a server.  but could work for your supertest example for more integration like testing.