Skip to content

Commit

Permalink
Prevent Jest from injecting globals
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Vinnl committed Oct 6, 2020
1 parent 75c8607 commit bf9d268
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 65 deletions.
22 changes: 22 additions & 0 deletions jest.config.js
@@ -1,3 +1,24 @@
/**
* Copyright 2020 Inrupt Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

module.exports = {
preset: "ts-jest",
testEnvironment: "jsdom",
Expand All @@ -17,4 +38,5 @@ module.exports = {
// By default we only run unit tests:
"e2e.test.ts",
],
injectGlobals: false,
};
2 changes: 1 addition & 1 deletion src/acl/acl.test.ts
Expand Up @@ -19,7 +19,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { describe, it, expect } from "@jest/globals";
import { jest, describe, it, expect } from "@jest/globals";
jest.mock("../fetcher.ts", () => ({
fetch: jest.fn().mockImplementation(() =>
Promise.resolve(
Expand Down
2 changes: 1 addition & 1 deletion src/acp/policy.test.ts
Expand Up @@ -19,7 +19,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { describe, it, expect } from "@jest/globals";
import { jest, describe, it, expect } from "@jest/globals";
jest.mock("../fetcher.ts", () => ({
fetch: jest.fn().mockImplementation(() =>
Promise.resolve(
Expand Down
2 changes: 1 addition & 1 deletion src/fetcher.test.ts
Expand Up @@ -19,7 +19,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

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

import { fetch } from "./fetcher";
Expand Down
12 changes: 5 additions & 7 deletions src/resource/nonRdfData.test.ts
Expand Up @@ -19,7 +19,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { describe, it, expect } from "@jest/globals";
import { jest, describe, it, expect } from "@jest/globals";

jest.mock("../fetcher", () => ({
fetch: jest
Expand All @@ -31,6 +31,7 @@ jest.mock("../fetcher", () => ({
),
}));

import type { Mock } from "jest-mock";
import {
getFile,
deleteFile,
Expand Down Expand Up @@ -211,7 +212,7 @@ describe("getFileWithAcl", () => {
: undefined;
const init: ResponseInit & { url: string } = {
headers: headers,
url: url,
url: url as string,
};
return Promise.resolve(new Response(undefined, init));
});
Expand Down Expand Up @@ -470,7 +471,7 @@ describe("Non-RDF data deletion", () => {
describe("Write non-RDF data into a folder", () => {
const mockBlob = new Blob(["mock blob data"], { type: "binary" });

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

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

fetcher.fetch = setMockOnFetch(
Expand Down
47 changes: 21 additions & 26 deletions src/resource/resource.test.ts
Expand Up @@ -19,7 +19,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

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

type MockedFetch = jest.Mock<
ReturnType<typeof window.fetch>,
Parameters<typeof window.fetch>
>;

describe("fetchAcl", () => {
it("calls the included fetcher by default", async () => {
const mockedFetcher = jest.requireMock("../fetcher.ts") as {
fetch: jest.Mock<
ReturnType<typeof window.fetch>,
[RequestInfo, RequestInit?]
>;
fetch: MockedFetch;
};

const mockResourceInfo: WithResourceInfo = {
Expand Down Expand Up @@ -116,7 +118,7 @@ describe("fetchAcl", () => {
return Promise.resolve(
mockResponse(undefined, {
headers: headers,
url: url,
url: url as string,
})
);
});
Expand Down Expand Up @@ -163,7 +165,7 @@ describe("fetchAcl", () => {
return Promise.resolve(
mockResponse(undefined, {
headers: headers,
url: url,
url: url as string,
})
);
});
Expand Down Expand Up @@ -202,7 +204,7 @@ describe("getResourceInfoWithAcl", () => {
return Promise.resolve(
mockResponse(undefined, {
headers: headers,
url: url,
url: url as string,
})
);
});
Expand Down Expand Up @@ -232,10 +234,7 @@ describe("getResourceInfoWithAcl", () => {

it("calls the included fetcher by default", async () => {
const mockedFetcher = jest.requireMock("../fetcher.ts") as {
fetch: jest.Mock<
ReturnType<typeof window.fetch>,
[RequestInfo, RequestInit?]
>;
fetch: MockedFetch;
};

await getResourceInfoWithAcl("https://some.pod/resource");
Expand Down Expand Up @@ -338,10 +337,7 @@ describe("getResourceInfoWithAcl", () => {
describe("getResourceInfo", () => {
it("calls the included fetcher by default", async () => {
const mockedFetcher = jest.requireMock("../fetcher.ts") as {
fetch: jest.Mock<
ReturnType<typeof window.fetch>,
[RequestInfo, RequestInit?]
>;
fetch: MockedFetch;
};

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

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

const solidDatasetInfo = await getResourceInfo(
"https://some.pod/container/resource",
Expand Down
55 changes: 26 additions & 29 deletions src/resource/solidDataset.test.ts
Expand Up @@ -19,7 +19,9 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { describe, it, expect } from "@jest/globals";
import { jest, describe, it, expect } from "@jest/globals";
import type { Mock } from "jest-mock";

jest.mock("../fetcher.ts", () => ({
fetch: jest.fn().mockImplementation(() =>
Promise.resolve(
Expand Down Expand Up @@ -167,16 +169,15 @@ describe("getSolidDataset", () => {
});

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

const solidDataset = await getSolidDataset(
"https://some.pod/container/resource",
Expand Down Expand Up @@ -349,7 +350,7 @@ describe("getSolidDatasetWithAcl", () => {
return Promise.resolve(
mockResponse(undefined, {
headers: headers,
url: url,
url: url as string,
})
);
});
Expand Down Expand Up @@ -1232,16 +1233,15 @@ describe("createContainerAt", () => {
});

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

const solidDataset = await createContainerAt(
"https://some.pod/container/",
Expand Down Expand Up @@ -1565,9 +1565,9 @@ describe("createContainerAt", () => {
});

describe("saveSolidDatasetInContainer", () => {
type MockFetch = jest.Mock<
type MockFetch = Mock<
ReturnType<typeof window.fetch>,
[RequestInfo, RequestInit?]
Parameters<typeof window.fetch>
>;
function setMockOnFetch(
fetch: MockFetch,
Expand Down Expand Up @@ -1926,7 +1926,7 @@ describe("saveSolidDatasetInContainer", () => {
});

describe("createContainerInContainer", () => {
type MockFetch = jest.Mock<
type MockFetch = Mock<
ReturnType<typeof window.fetch>,
[RequestInfo, RequestInit?]
>;
Expand All @@ -1953,10 +1953,7 @@ describe("createContainerInContainer", () => {

it("calls the included fetcher by default", async () => {
const mockedFetcher = jest.requireMock("../fetcher.ts") as {
fetch: jest.Mock<
ReturnType<typeof window.fetch>,
[RequestInfo, RequestInit?]
>;
fetch: MockFetch;
};
mockedFetcher.fetch = setMockOnFetch(mockedFetcher.fetch);

Expand Down

0 comments on commit bf9d268

Please sign in to comment.