Skip to content

Commit bf9d268

Browse files
committed
Prevent Jest from injecting globals
Since 9cc1379, Jest supports disabling the injection of globals. This makes tests easier to follow, and ensures that all types can be properly checked as well. (Unfortunately this also exposed a flaw in Jest's type definitions, which I've fixed here but which isn't released yet: jestjs/jest#10600)
1 parent 75c8607 commit bf9d268

File tree

7 files changed

+77
-65
lines changed

7 files changed

+77
-65
lines changed

jest.config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
/**
2+
* Copyright 2020 Inrupt Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to use,
7+
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8+
* Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
122
module.exports = {
223
preset: "ts-jest",
324
testEnvironment: "jsdom",
@@ -17,4 +38,5 @@ module.exports = {
1738
// By default we only run unit tests:
1839
"e2e.test.ts",
1940
],
41+
injectGlobals: false,
2042
};

src/acl/acl.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
*/
2121

22-
import { describe, it, expect } from "@jest/globals";
22+
import { jest, describe, it, expect } from "@jest/globals";
2323
jest.mock("../fetcher.ts", () => ({
2424
fetch: jest.fn().mockImplementation(() =>
2525
Promise.resolve(

src/acp/policy.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
*/
2121

22-
import { describe, it, expect } from "@jest/globals";
22+
import { jest, describe, it, expect } from "@jest/globals";
2323
jest.mock("../fetcher.ts", () => ({
2424
fetch: jest.fn().mockImplementation(() =>
2525
Promise.resolve(

src/fetcher.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
*/
2121

22-
import { it, expect } from "@jest/globals";
22+
import { jest, it, expect } from "@jest/globals";
2323
jest.mock("cross-fetch");
2424

2525
import { fetch } from "./fetcher";

src/resource/nonRdfData.test.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
*/
2121

22-
import { describe, it, expect } from "@jest/globals";
22+
import { jest, describe, it, expect } from "@jest/globals";
2323

2424
jest.mock("../fetcher", () => ({
2525
fetch: jest
@@ -31,6 +31,7 @@ jest.mock("../fetcher", () => ({
3131
),
3232
}));
3333

34+
import type { Mock } from "jest-mock";
3435
import {
3536
getFile,
3637
deleteFile,
@@ -211,7 +212,7 @@ describe("getFileWithAcl", () => {
211212
: undefined;
212213
const init: ResponseInit & { url: string } = {
213214
headers: headers,
214-
url: url,
215+
url: url as string,
215216
};
216217
return Promise.resolve(new Response(undefined, init));
217218
});
@@ -470,7 +471,7 @@ describe("Non-RDF data deletion", () => {
470471
describe("Write non-RDF data into a folder", () => {
471472
const mockBlob = new Blob(["mock blob data"], { type: "binary" });
472473

473-
type MockFetch = jest.Mock<
474+
type MockFetch = Mock<
474475
ReturnType<typeof window.fetch>,
475476
[RequestInfo, RequestInit?]
476477
>;
@@ -577,10 +578,7 @@ describe("Write non-RDF data into a folder", () => {
577578

578579
it("returns null if the current user does not have Read access to the new file", async () => {
579580
const fetcher = jest.requireMock("../fetcher") as {
580-
fetch: jest.Mock<
581-
ReturnType<typeof window.fetch>,
582-
[RequestInfo, RequestInit?]
583-
>;
581+
fetch: MockFetch;
584582
};
585583

586584
fetcher.fetch = setMockOnFetch(

src/resource/resource.test.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
*/
2121

22-
import { describe, it, expect } from "@jest/globals";
22+
import { jest, describe, it, expect } from "@jest/globals";
2323
jest.mock("../fetcher.ts", () => ({
2424
fetch: jest.fn().mockImplementation(() =>
2525
Promise.resolve(
@@ -56,13 +56,15 @@ function mockResponse(
5656
return new Response(body, init);
5757
}
5858

59+
type MockedFetch = jest.Mock<
60+
ReturnType<typeof window.fetch>,
61+
Parameters<typeof window.fetch>
62+
>;
63+
5964
describe("fetchAcl", () => {
6065
it("calls the included fetcher by default", async () => {
6166
const mockedFetcher = jest.requireMock("../fetcher.ts") as {
62-
fetch: jest.Mock<
63-
ReturnType<typeof window.fetch>,
64-
[RequestInfo, RequestInit?]
65-
>;
67+
fetch: MockedFetch;
6668
};
6769

6870
const mockResourceInfo: WithResourceInfo = {
@@ -116,7 +118,7 @@ describe("fetchAcl", () => {
116118
return Promise.resolve(
117119
mockResponse(undefined, {
118120
headers: headers,
119-
url: url,
121+
url: url as string,
120122
})
121123
);
122124
});
@@ -163,7 +165,7 @@ describe("fetchAcl", () => {
163165
return Promise.resolve(
164166
mockResponse(undefined, {
165167
headers: headers,
166-
url: url,
168+
url: url as string,
167169
})
168170
);
169171
});
@@ -202,7 +204,7 @@ describe("getResourceInfoWithAcl", () => {
202204
return Promise.resolve(
203205
mockResponse(undefined, {
204206
headers: headers,
205-
url: url,
207+
url: url as string,
206208
})
207209
);
208210
});
@@ -232,10 +234,7 @@ describe("getResourceInfoWithAcl", () => {
232234

233235
it("calls the included fetcher by default", async () => {
234236
const mockedFetcher = jest.requireMock("../fetcher.ts") as {
235-
fetch: jest.Mock<
236-
ReturnType<typeof window.fetch>,
237-
[RequestInfo, RequestInit?]
238-
>;
237+
fetch: MockedFetch;
239238
};
240239

241240
await getResourceInfoWithAcl("https://some.pod/resource");
@@ -338,10 +337,7 @@ describe("getResourceInfoWithAcl", () => {
338337
describe("getResourceInfo", () => {
339338
it("calls the included fetcher by default", async () => {
340339
const mockedFetcher = jest.requireMock("../fetcher.ts") as {
341-
fetch: jest.Mock<
342-
ReturnType<typeof window.fetch>,
343-
[RequestInfo, RequestInit?]
344-
>;
340+
fetch: MockedFetch;
345341
};
346342

347343
await getResourceInfo("https://some.pod/resource");
@@ -547,16 +543,15 @@ describe("getResourceInfo", () => {
547543
});
548544

549545
it("does not provide an IRI to an ACL resource if not provided one by the server", async () => {
550-
const mockFetch = jest.fn(window.fetch).mockResolvedValue(
551-
new Response(undefined, {
552-
headers: {
553-
Link: '<arbitrary-resource>; rel="not-acl"',
554-
},
555-
url: "https://arbitrary.pod",
556-
// We need the type assertion because in non-mock situations,
557-
// you cannot set the URL manually:
558-
} as ResponseInit)
559-
);
546+
const mockResponse = new Response(undefined, {
547+
headers: {
548+
Link: '<arbitrary-resource>; rel="not-acl"',
549+
},
550+
url: "https://arbitrary.pod",
551+
// We need the type assertion because in non-mock situations,
552+
// you cannot set the URL manually:
553+
} as ResponseInit);
554+
const mockFetch = jest.fn(window.fetch).mockResolvedValue(mockResponse);
560555

561556
const solidDatasetInfo = await getResourceInfo(
562557
"https://some.pod/container/resource",

src/resource/solidDataset.test.ts

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
*/
2121

22-
import { describe, it, expect } from "@jest/globals";
22+
import { jest, describe, it, expect } from "@jest/globals";
23+
import type { Mock } from "jest-mock";
24+
2325
jest.mock("../fetcher.ts", () => ({
2426
fetch: jest.fn().mockImplementation(() =>
2527
Promise.resolve(
@@ -167,16 +169,15 @@ describe("getSolidDataset", () => {
167169
});
168170

169171
it("does not provide an IRI to an ACL resource if not provided one by the server", async () => {
170-
const mockFetch = jest.fn(window.fetch).mockResolvedValue(
171-
new Response(undefined, {
172-
headers: {
173-
Link: '<arbitrary-resource>; rel="not-acl"',
174-
},
175-
url: "https://arbitrary.pod",
176-
// We need the type assertion because in non-mock situations,
177-
// you cannot set the URL manually:
178-
} as ResponseInit)
179-
);
172+
const mockResponse = new Response(undefined, {
173+
headers: {
174+
Link: '<arbitrary-resource>; rel="not-acl"',
175+
},
176+
url: "https://arbitrary.pod",
177+
// We need the type assertion because in non-mock situations,
178+
// you cannot set the URL manually:
179+
} as ResponseInit);
180+
const mockFetch = jest.fn(window.fetch).mockResolvedValue(mockResponse);
180181

181182
const solidDataset = await getSolidDataset(
182183
"https://some.pod/container/resource",
@@ -349,7 +350,7 @@ describe("getSolidDatasetWithAcl", () => {
349350
return Promise.resolve(
350351
mockResponse(undefined, {
351352
headers: headers,
352-
url: url,
353+
url: url as string,
353354
})
354355
);
355356
});
@@ -1232,16 +1233,15 @@ describe("createContainerAt", () => {
12321233
});
12331234

12341235
it("does not provide an IRI to an ACL resource if not provided one by the server", async () => {
1235-
const mockFetch = jest.fn(window.fetch).mockResolvedValue(
1236-
new Response(undefined, {
1237-
headers: {
1238-
Link: '<arbitrary-resource>; rel="not-acl"',
1239-
},
1240-
url: "https://arbitrary.pod",
1241-
// We need the type assertion because in non-mock situations,
1242-
// you cannot set the URL manually:
1243-
} as ResponseInit)
1244-
);
1236+
const mockResponse = new Response(undefined, {
1237+
headers: {
1238+
Link: '<arbitrary-resource>; rel="not-acl"',
1239+
},
1240+
url: "https://arbitrary.pod",
1241+
// We need the type assertion because in non-mock situations,
1242+
// you cannot set the URL manually:
1243+
} as ResponseInit);
1244+
const mockFetch = jest.fn(window.fetch).mockResolvedValue(mockResponse);
12451245

12461246
const solidDataset = await createContainerAt(
12471247
"https://some.pod/container/",
@@ -1565,9 +1565,9 @@ describe("createContainerAt", () => {
15651565
});
15661566

15671567
describe("saveSolidDatasetInContainer", () => {
1568-
type MockFetch = jest.Mock<
1568+
type MockFetch = Mock<
15691569
ReturnType<typeof window.fetch>,
1570-
[RequestInfo, RequestInit?]
1570+
Parameters<typeof window.fetch>
15711571
>;
15721572
function setMockOnFetch(
15731573
fetch: MockFetch,
@@ -1926,7 +1926,7 @@ describe("saveSolidDatasetInContainer", () => {
19261926
});
19271927

19281928
describe("createContainerInContainer", () => {
1929-
type MockFetch = jest.Mock<
1929+
type MockFetch = Mock<
19301930
ReturnType<typeof window.fetch>,
19311931
[RequestInfo, RequestInit?]
19321932
>;
@@ -1953,10 +1953,7 @@ describe("createContainerInContainer", () => {
19531953

19541954
it("calls the included fetcher by default", async () => {
19551955
const mockedFetcher = jest.requireMock("../fetcher.ts") as {
1956-
fetch: jest.Mock<
1957-
ReturnType<typeof window.fetch>,
1958-
[RequestInfo, RequestInit?]
1959-
>;
1956+
fetch: MockFetch;
19601957
};
19611958
mockedFetcher.fetch = setMockOnFetch(mockedFetcher.fetch);
19621959

0 commit comments

Comments
 (0)