1
0

smoke-tests.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const http = require('http');
  2. const healthUrl = 'http://localhost:3000/health';
  3. const shopUrl = 'http://localhost:3000/shop-api';
  4. const adminUrl = 'http://localhost:3000/admin-api';
  5. /**
  6. * This script is run as part of the "publish & install" workflow, and performs some very simple
  7. * tests of the Vendure API to make sure it is running and responding in the expected way.
  8. */
  9. awaitServerStartup()
  10. .then(() => runTests())
  11. .catch(e => {
  12. console.log('Tests failed!');
  13. console.log(e);
  14. process.exit(1);
  15. });
  16. function awaitServerStartup() {
  17. console.log('Checking for availability of Vendure Server...');
  18. let attempts = 0;
  19. return new Promise((resolve, reject) => {
  20. async function poll() {
  21. try {
  22. let result = await request(healthUrl, 'GET');
  23. if (result && result.status === 'ok') {
  24. console.log('Server is running!');
  25. resolve();
  26. return;
  27. }
  28. } catch (e) {
  29. //
  30. }
  31. attempts++;
  32. if (attempts < 30) {
  33. console.log('Server not yet available, waiting 1s...');
  34. setTimeout(poll, 2000);
  35. } else {
  36. reject('Unable to establish connection to Vendure server!');
  37. }
  38. }
  39. poll();
  40. });
  41. }
  42. async function runTests() {
  43. console.log('Running some smoke tests...');
  44. const result1 = await gqlRequest(
  45. shopUrl,
  46. `
  47. { "query": "{ products(options: { take: 3 }) { items { id name } } }" }
  48. `,
  49. );
  50. assertEquals(result1.data.products.items, [
  51. { id: '1', name: 'Laptop' },
  52. { id: '2', name: 'Tablet' },
  53. { id: '3', name: 'Wireless Optical Mouse' },
  54. ]);
  55. const result2 = await gqlRequest(
  56. adminUrl,
  57. `
  58. { "query": "mutation { login(username: \\"superadmin\\" password: \\"superadmin\\") { ...on CurrentUser { id } } }" }
  59. `,
  60. );
  61. assertEquals(result2.data.login, { id: '1' });
  62. console.log(`All tests passed!`);
  63. process.exit(0);
  64. }
  65. function gqlRequest(url, body) {
  66. return request(url, 'POST', body).catch(e => console.log(e));
  67. }
  68. /**
  69. *
  70. * @param url
  71. * @param body
  72. * @return {Promise<unknown>}
  73. */
  74. function request(url, method, body) {
  75. const options = {
  76. method,
  77. headers: {
  78. 'Content-Type': 'application/json',
  79. },
  80. };
  81. return new Promise((resolve, reject) => {
  82. const req = http.request(url, options, res => {
  83. var response = '';
  84. res.setEncoding('utf8');
  85. res.on('data', chunk => {
  86. return (response += chunk);
  87. });
  88. res.on('end', () => {
  89. resolve(JSON.parse(response));
  90. });
  91. });
  92. req.on('error', e => {
  93. reject(e);
  94. });
  95. // Write data to request body
  96. req.write(body || '');
  97. req.end();
  98. });
  99. }
  100. function assertEquals(actual, expected) {
  101. if (JSON.stringify(actual) !== JSON.stringify(expected)) {
  102. console.log(`Expected [${JSON.stringify(actual)}] to equal [${JSON.stringify(expected)}]`);
  103. process.exit(1);
  104. }
  105. }