Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test example with 'before' starting server #36

Closed
Morriz opened this issue May 18, 2016 · 4 comments
Closed

test example with 'before' starting server #36

Morriz opened this issue May 18, 2016 · 4 comments

Comments

@Morriz
Copy link

Morriz commented May 18, 2016

Hi, I was wondering if you can provide an example in the docs how to start a service from a test environment for integration testing.

@rauchg
Copy link
Member

rauchg commented May 18, 2016

I wrote test-listen to make the job of passing a http server (as the one that could be returned by serve(async (req, res) => {}) from micro), and make it listen on a random ephemeral port.

Using ava:

import test from 'ava';
import listen from 'test-listen';

test('it works', async t => {
  // initialize `srv` using `serve` from `micro` first!
  const url = await listen(srv);
  t.ok(/http:\/\/localhost:\d+/.test(url));
  const data = await request(url);
  t.same('1', data);
});

Example:
https://github.com/zeit/test-listen/blob/master/test/index.js#L9

@rauchg rauchg closed this as completed May 18, 2016
@Morriz
Copy link
Author

Morriz commented May 19, 2016

Tnx! But than I would have to rewrite my code to fit the test...that's not what you meant, right?

Your example shows a simple export:

import {send} from 'micro-core'

export default async function(req, res) {
  send(res, 200, 'Ready!')
}

Which is served using the micro cli. So why use the micro cli at all if we have to write tests that consume serve'd functions?

I am disliking the convenience abstraction more and more...it obfuscates stuff and does not educate people, and it leads to maintenance you don't want

Why not just put out a great manual/stack on how to set it up without micro?

On 18 mei 2016, at 18:50, Guillermo Rauch notifications@github.com wrote:

I wrote test-listen to make the job of passing a http server (as the one that could be returned by serve(async (req, res) => {}) from micro), and make it listen on a random ephemeral port.

Using ava:

import test from 'ava';
import listen from 'test-listen';

test('it works', async t => {
// initialize srv using serve from micro first!
const url = await listen(srv);
t.ok(/http://localhost:\d+/.test(url));
const data = await request(url);
t.same('1', data);
});
Example:
https://github.com/zeit/test-listen/blob/master/test/index.js#L9 https://github.com/zeit/test-listen/blob/master/test/index.js#L9

You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub #36 (comment)

@Morriz
Copy link
Author

Morriz commented May 19, 2016

Nevermind, I figured it out...I can just start it with serve(listen(... in the test itself...

would be nice to have it in the readme tho ;)

@rauchg
Copy link
Member

rauchg commented May 19, 2016

@Morriz I agree it would be nice. Also having a complete project example repository with tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants