Skip to content

Commit

Permalink
Ensure that mocked requests are matched
Browse files Browse the repository at this point in the history
This change uses fetch-mock's 'done' inspection to ensure that it matches the requests as expected to avoid tests passing for the wrong reason (i.e. we didn't mock a response).

I think I've found a bug in fetch-mock where it doesn't recognise that it matched a request when testing a query string independently of the URL. It works as expected when only partially matching the URL.

Refs #976, 047d979, https://www.wheresrhys.co.uk/fetch-mock/#api-inspectiondone, wheresrhys/fetch-mock#657
  • Loading branch information
thewilkybarkid committed Jun 15, 2023
1 parent 047d979 commit ceea33f
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 145 deletions.
16 changes: 10 additions & 6 deletions test/about-us.test.ts
Expand Up @@ -46,14 +46,17 @@ describe('about-us', () => {
fc.fetchResponse(),
fc.either(fc.constant('no-session' as const), fc.user()),
])('when the page cannot be loaded', async (connection, key, response, user) => {
const fetch = fetchMock.sandbox().getOnce(
{
url: 'begin:https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb14?',
query: { key },
},
response,
)

const actual = await runMiddleware(
_.aboutUs({
fetch: fetchMock
.sandbox()
.getOnce(
{ url: 'https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb14', query: { key } },
response,
),
fetch,
ghostApi: { key },
getUser: () => M.fromEither(user),
}),
Expand All @@ -68,5 +71,6 @@ describe('about-us', () => {
{ type: 'setBody', body: expect.anything() },
]),
)
expect(fetch.done()).toBeTruthy()
})
})
16 changes: 10 additions & 6 deletions test/code-of-conduct.test.ts
Expand Up @@ -46,14 +46,17 @@ describe('codeOfConduct', () => {
fc.fetchResponse(),
fc.either(fc.constant('no-session' as const), fc.user()),
])('when the page cannot be loaded', async (connection, key, response, user) => {
const fetch = fetchMock.sandbox().getOnce(
{
url: 'begin:https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb00?',
query: { key },
},
response,
)

const actual = await runMiddleware(
_.codeOfConduct({
fetch: fetchMock
.sandbox()
.getOnce(
{ url: 'https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb00', query: { key } },
response,
),
fetch,
ghostApi: { key },
getUser: () => M.fromEither(user),
}),
Expand All @@ -68,5 +71,6 @@ describe('codeOfConduct', () => {
{ type: 'setBody', body: expect.anything() },
]),
)
expect(fetch.done()).toBeTruthy()
})
})
16 changes: 10 additions & 6 deletions test/communities.test.ts
Expand Up @@ -46,14 +46,17 @@ describe('communities', () => {
fc.fetchResponse(),
fc.either(fc.constant('no-session' as const), fc.user()),
])('when the page cannot be loaded', async (connection, key, response, user) => {
const fetch = fetchMock.sandbox().getOnce(
{
url: 'begin:https://content.prereview.org/ghost/api/content/pages/64637b4c07fb34a92c7f84ec?',
query: { key },
},
response,
)

const actual = await runMiddleware(
_.communities({
fetch: fetchMock
.sandbox()
.getOnce(
{ url: 'https://content.prereview.org/ghost/api/content/pages/64637b4c07fb34a92c7f84ec', query: { key } },
response,
),
fetch,
ghostApi: { key },
getUser: () => M.fromEither(user),
}),
Expand All @@ -68,5 +71,6 @@ describe('communities', () => {
{ type: 'setBody', body: expect.anything() },
]),
)
expect(fetch.done()).toBeTruthy()
})
})
3 changes: 3 additions & 0 deletions test/crossref.test.ts
Expand Up @@ -2256,6 +2256,7 @@ describe('getPreprintFromCrossref', () => {
const actual = await _.getPreprintFromCrossref(id)({ fetch })()

expect(actual).toStrictEqual(E.left('not-found'))
expect(fetch.done()).toBeTruthy()
})

test.prop([
Expand Down Expand Up @@ -2320,6 +2321,7 @@ describe('getPreprintFromCrossref', () => {
const actual = await _.getPreprintFromCrossref(id)({ fetch })()

expect(actual).toStrictEqual(E.left('not-a-preprint'))
expect(fetch.done()).toBeTruthy()
})

test.prop([fc.crossrefPreprintId(), fc.record({ status: fc.integer(), body: fc.string() })])(
Expand All @@ -2332,6 +2334,7 @@ describe('getPreprintFromCrossref', () => {
const actual = await _.getPreprintFromCrossref(id)({ fetch })()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
},
)
})
2 changes: 2 additions & 0 deletions test/datacite.test.ts
Expand Up @@ -888,6 +888,7 @@ describe('getPreprintFromDatacite', () => {
const actual = await _.getPreprintFromDatacite(id)({ fetch })()

expect(actual).toStrictEqual(E.left('not-found'))
expect(fetch.done()).toBeTruthy()
})

test.prop([fc.arxivPreprintId(), fc.instant(), fc.string()])(
Expand Down Expand Up @@ -1065,6 +1066,7 @@ describe('getPreprintFromDatacite', () => {
const actual = await _.getPreprintFromDatacite(id)({ fetch })()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
},
)
})
16 changes: 10 additions & 6 deletions test/funding.test.ts
Expand Up @@ -46,14 +46,17 @@ describe('funding', () => {
fc.fetchResponse(),
fc.either(fc.constant('no-session' as const), fc.user()),
])('when the page cannot be loaded', async (connection, key, response, user) => {
const fetch = fetchMock.sandbox().getOnce(
{
url: 'begin:https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb12?',
query: { key },
},
response,
)

const actual = await runMiddleware(
_.funding({
fetch: fetchMock
.sandbox()
.getOnce(
{ url: 'https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb12', query: { key } },
response,
),
fetch,
ghostApi: { key },
getUser: () => M.fromEither(user),
}),
Expand All @@ -68,5 +71,6 @@ describe('funding', () => {
{ type: 'setBody', body: expect.anything() },
]),
)
expect(fetch.done()).toBeTruthy()
})
})
6 changes: 4 additions & 2 deletions test/ghost.test.ts
Expand Up @@ -87,14 +87,15 @@ describe('getPage', () => {
])('when the response cannot be decoded', async (id, key, response) => {
const fetch = fetchMock
.sandbox()
.getOnce({ url: `https://content.prereview.org/ghost/api/content/pages/${id}`, query: { key } }, response)
.getOnce({ url: `begin:https://content.prereview.org/ghost/api/content/pages/${id}?`, query: { key } }, response)

const actual = await _.getPage(id)({
fetch,
ghostApi: { key },
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})

test.prop([fc.stringOf(fc.alphanumeric(), { minLength: 1 }), fc.stringOf(fc.alphanumeric(), { minLength: 1 })])(
Expand Down Expand Up @@ -123,14 +124,15 @@ describe('getPage', () => {
])('when the response has a non-200/404 status code', async (id, key, status) => {
const fetch = fetchMock
.sandbox()
.getOnce({ url: `https://content.prereview.org/ghost/api/content/pages/${id}`, query: { key } }, status)
.getOnce({ url: `begin:https://content.prereview.org/ghost/api/content/pages/${id}?`, query: { key } }, status)

const actual = await _.getPage(id)({
fetch,
ghostApi: { key },
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})

test.prop([
Expand Down
29 changes: 20 additions & 9 deletions test/legacy-prereview.test.ts
Expand Up @@ -68,6 +68,7 @@ describe('getPreprintDoiFromLegacyPreviewUuid', () => {
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})

test.prop([fc.uuid(), fc.string(), fc.string(), fc.origin(), fc.boolean()])(
Expand Down Expand Up @@ -100,6 +101,7 @@ describe('getPreprintDoiFromLegacyPreviewUuid', () => {
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})

test.prop([fc.uuid(), fc.string(), fc.string(), fc.origin(), fc.boolean(), fc.error()])(
Expand Down Expand Up @@ -173,6 +175,7 @@ describe('getProfileIdFromLegacyPreviewUuid', () => {
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})

test.prop([fc.uuid(), fc.string(), fc.string(), fc.origin(), fc.boolean()])(
Expand Down Expand Up @@ -205,6 +208,7 @@ describe('getProfileIdFromLegacyPreviewUuid', () => {
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})

test.prop([fc.uuid(), fc.string(), fc.string(), fc.origin(), fc.boolean(), fc.error()])(
Expand Down Expand Up @@ -278,6 +282,7 @@ describe('getPseudonymFromLegacyPrereview', () => {
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})

describe('when the response has a 404 status code', () => {
Expand Down Expand Up @@ -332,6 +337,7 @@ describe('getPseudonymFromLegacyPrereview', () => {
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})

test.prop([
Expand Down Expand Up @@ -361,6 +367,7 @@ describe('getPseudonymFromLegacyPrereview', () => {
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})
})

Expand All @@ -381,6 +388,7 @@ describe('getPseudonymFromLegacyPrereview', () => {
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})

test.prop([fc.orcid(), fc.string(), fc.string(), fc.string(), fc.origin(), fc.boolean(), fc.error()])(
Expand Down Expand Up @@ -603,16 +611,17 @@ describe('getRapidPreviewsFromLegacyPrereview', () => {
fc.preprintIdWithDoi(),
fc.integer({ min: 400, max: 599 }).filter(status => status !== Status.NotFound),
])('when the Rapid PREreviews cannot be loaded', async (app, key, url, update, preprintId, status) => {
const actual = await _.getRapidPreviewsFromLegacyPrereview(preprintId)({
fetch: fetchMock
.sandbox()
const fetch = fetchMock
.sandbox()
.getOnce(
`${url}api/v2/preprints/doi-${encodeURIComponent(
preprintId.value.toLowerCase().replaceAll('-', '+').replaceAll('/', '-'),
)}/rapid-reviews`,
{ status },
)

.getOnce(
`${url}api/v2/preprints/doi-${encodeURIComponent(
preprintId.value.toLowerCase().replaceAll('-', '+').replaceAll('/', '-'),
)}/rapid-reviews`,
{ status },
),
const actual = await _.getRapidPreviewsFromLegacyPrereview(preprintId)({
fetch,
legacyPrereviewApi: {
app,
key,
Expand All @@ -622,6 +631,7 @@ describe('getRapidPreviewsFromLegacyPrereview', () => {
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
})
})

Expand Down Expand Up @@ -746,6 +756,7 @@ describe('createPrereviewOnLegacyPrereview', () => {
})()

expect(actual).toStrictEqual(E.left(expect.anything()))
expect(fetch.done()).toBeTruthy()
},
)

Expand Down
10 changes: 6 additions & 4 deletions test/log-in.test.ts
Expand Up @@ -268,17 +268,18 @@ describe('authenticate', () => {
'when a pseudonym cannot be retrieved',
async (code, [referer], oauth, accessToken, secret, sessionCookie, connection) => {
const sessionStore = new Keyv()
const fetch = fetchMock.sandbox().postOnce(oauth.tokenUrl.href, {
status: Status.OK,
body: accessToken,
})

const actual = await runMiddleware(
_.authenticate(
code,
referer.href,
)({
clock: SystemClock,
fetch: fetchMock.sandbox().postOnce(oauth.tokenUrl.href, {
status: Status.OK,
body: accessToken,
}),
fetch,
getPseudonym: () => TE.left('unavailable'),
logger: () => IO.of(undefined),
oauth,
Expand All @@ -300,6 +301,7 @@ describe('authenticate', () => {
{ type: 'setBody', body: expect.anything() },
]),
)
expect(fetch.done()).toBeTruthy()
},
)

Expand Down
5 changes: 4 additions & 1 deletion test/orcid.test.ts
Expand Up @@ -82,13 +82,16 @@ describe('getNameFromOrcid', () => {
test.prop([fc.orcid(), fc.integer({ min: 100, max: 599 }).filter(status => status !== Status.OK)])(
'when the request fails',
async (orcid, status) => {
const fetch = fetchMock.sandbox().get('*', { status })

const actual = await _.getNameFromOrcid(orcid)({
clock: SystemClock,
fetch: fetchMock.sandbox().get('*', { status }),
fetch,
logger: () => IO.of(undefined),
})()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
},
)

Expand Down
1 change: 1 addition & 0 deletions test/philsci.test.ts
Expand Up @@ -321,6 +321,7 @@ describe('getPreprintFromPhilsci', () => {
const actual = await _.getPreprintFromPhilsci(id)({ fetch })()

expect(actual).toStrictEqual(E.left('unavailable'))
expect(fetch.done()).toBeTruthy()
},
)
})
16 changes: 10 additions & 6 deletions test/preprint-journal-clubs.test.ts
Expand Up @@ -46,14 +46,17 @@ describe('preprintJournalClubs', () => {
fc.fetchResponse(),
fc.either(fc.constant('no-session' as const), fc.user()),
])('when the page cannot be loaded', async (connection, key, response, user) => {
const fetch = fetchMock.sandbox().getOnce(
{
url: 'begin:https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb10?',
query: { key },
},
response,
)

const actual = await runMiddleware(
_.preprintJournalClubs({
fetch: fetchMock
.sandbox()
.getOnce(
{ url: 'https://content.prereview.org/ghost/api/content/pages/6154aa157741400e8722bb10', query: { key } },
response,
),
fetch,
ghostApi: { key },
getUser: () => M.fromEither(user),
}),
Expand All @@ -68,5 +71,6 @@ describe('preprintJournalClubs', () => {
{ type: 'setBody', body: expect.anything() },
]),
)
expect(fetch.done()).toBeTruthy()
})
})

0 comments on commit ceea33f

Please sign in to comment.