Skip to content

Commit

Permalink
chore: Warn if mocks aren't running (#1534)
Browse files Browse the repository at this point in the history
It's not obvious that the mocks need to be running for tests to run. Now
the output will warn you and tell you how to run them.
  • Loading branch information
Peeja authored Aug 30, 2024
1 parent 07970ef commit fb2fd66
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
7 changes: 5 additions & 2 deletions packages/upload-client/test/helpers/receipts-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ const server = createServer(async (req, res) => {
res.setHeader('Access-Control-Allow-Methods', '*')
res.setHeader('Access-Control-Allow-Headers', '*')

const taskCid = req.url?.split('/')[1] ?? ''
if (taskCid === 'unavailable') {
const taskCid = req.url?.split('/')[1]
if (!taskCid) {
res.writeHead(204)
res.end()
} else if (taskCid === 'unavailable') {
res.writeHead(404)
res.end()
} else if (taskCid === 'failed') {
Expand Down
26 changes: 21 additions & 5 deletions packages/upload-client/test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ import { randomCAR } from './random.js'
export const validateAuthorization = () => ({ ok: {} })

export const receiptsEndpoint = 'http://localhost:9201'
export const bucket200Endpoint = 'http://localhost:9200'
export const bucket400Endpoint = 'http://localhost:9400'
export const bucket500Endpoint = 'http://localhost:9500'

Object.entries({
receiptsEndpoint,
bucket200Endpoint,
bucket400Endpoint,
bucket500Endpoint,
}).forEach(([name, url]) => {
fetch(url).catch((error) => {
console.warn(
`${name} is unreachable at ${url}. If tests are failing, try running \`pnpm --filter=@web3-storage/upload-client mock\`.`
)
})
})

export const setupBlobAddSuccessResponse = async function (
// @ts-ignore
Expand All @@ -20,7 +36,7 @@ export const setupBlobAddSuccessResponse = async function (
invocation
) {
return setupBlobAddResponse(
'http://localhost:9200',
bucket200Endpoint,
options,
invocation,
false,
Expand All @@ -35,7 +51,7 @@ export const setupBlobAdd4xxResponse = async function (
invocation
) {
return setupBlobAddResponse(
'http://localhost:9400',
bucket400Endpoint,
options,
invocation,
false,
Expand All @@ -50,7 +66,7 @@ export const setupBlobAdd5xxResponse = async function (
invocation
) {
return setupBlobAddResponse(
'http://localhost:9500',
bucket500Endpoint,
options,
invocation,
false,
Expand All @@ -65,7 +81,7 @@ export const setupBlobAddWithAcceptReceiptSuccessResponse = async function (
invocation
) {
return setupBlobAddResponse(
'http://localhost:9200',
bucket200Endpoint,
options,
invocation,
false,
Expand All @@ -80,7 +96,7 @@ export const setupBlobAddWithHttpPutReceiptSuccessResponse = async function (
invocation
) {
return setupBlobAddResponse(
'http://localhost:9200',
bucket200Endpoint,
options,
invocation,
true,
Expand Down

0 comments on commit fb2fd66

Please sign in to comment.