service.js 970 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import {fork} from 'child_process';
  2. import tape from 'tape';
  3. import _test from 'tape-promise';
  4. import fetch from 'isomorphic-fetch';
  5. const tapetest = _test(tape);
  6. const randomPort = Math.floor(Math.random() * 10000) + 20000,
  7. server = fork('build/service.js', [], {env: {port: randomPort}}),
  8. connstring = `http://localhost:${randomPort}`;
  9. tapetest('service', async function testService(assert) {
  10. try {
  11. await waitPro();
  12. const ret = await fetch(connstring + '/'),
  13. data = await ret.text();
  14. console.log('data', data);
  15. assert.equal(ret.status, 200, 'root should work');
  16. }
  17. catch(e) {
  18. console.log('error in', assert.name, e);
  19. assert.fail(`error in ${assert.name}`);
  20. }
  21. assert.end();
  22. });
  23. tapetest.onFinish(async function cleanup() {
  24. server.kill();
  25. });
  26. function waitPro(n = 1000) {
  27. return new Promise(function pro(resolve, reject) {
  28. setTimeout(resolve, n);
  29. });
  30. }