Browse Source

refactor(friend-support): implement suggested changes

- remove unused import statements and variables
- improve variables naming
- deconstruct objects used as inputs
pull/1526/head
nprimo 1 year ago committed by Niccolò Primo
parent
commit
446924e348
  1. 19
      js/tests/friend-support_test.mjs

19
js/tests/friend-support_test.mjs

@ -1,7 +1,6 @@
import { once } from 'node:events'
import * as cp from 'node:child_process'
import { mkdir, writeFile, chmod } from 'fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'path'
export const tests = []
@ -25,7 +24,7 @@ export const setup = async ({ randStr }) => {
const sendRequest = async (path, options) => {
const response = await fetch(`http://localhost:${port}${path}`, options)
const { status, statusText, ok } = response
const { status } = response
const headers = Object.fromEntries(response.headers)
let body = ''
try {
@ -39,14 +38,14 @@ export const setup = async ({ randStr }) => {
return { tmpPath: dir, createFilesIn, sendRequest }
}
tests.push(async ({ path, ctx }) => {
ctx.server = cp.spawn('node', [`${path}`])
tests.push(async ({ path, ctx: { server } }) => {
server = cp.spawn('node', [`${path}`])
const message = await Promise.race([
once(ctx.server.stdout, 'data'),
once(server.stdout, 'data'),
Promise.race([
once(ctx.server.stderr, 'data').then(String).then(Error),
once(ctx.server, 'error'),
]).then(x => Promise.reject(x)),
once(server.stderr, 'data').then(String).then(Error),
once(server, 'error'),
]).then(result => Promise.reject(result)),
])
return message[0].toString().includes(port)
})
@ -55,7 +54,7 @@ tests.push(async ({ eq, ctx, randStr }) => {
// test for one guest
const expBody = { message: 'ciao' }
const files = [[`mario_${ctx.randLastName}.json`, expBody]]
const dirName = `guests`
const dirName = 'guests'
const dirPath = join(ctx.tmpPath, dirName)
await ctx.createFilesIn({ dirPath, files })
@ -79,7 +78,7 @@ tests.push(async ({ eq, ctx, randStr }) => {
)
})
tests.push(async ({ eq, ctx, randStr }) => {
tests.push(async ({ eq, ctx }) => {
// test server failed
// change permission for existing file
await chmod(`${ctx.tmpPath}/guests/mario_${ctx.randLastName}.json`, 0)

Loading…
Cancel
Save