From bf9d26889f649ff0367aa3a80d76eb994b7af864 Mon Sep 17 00:00:00 2001 From: Vincent Date: Tue, 6 Oct 2020 12:01:34 +0200 Subject: [PATCH] Prevent Jest from injecting globals Since 9cc1379d07d627cb2baa58f6c7a19c8de3df1e3b, 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: https://github.com/facebook/jest/pull/10600) --- jest.config.js | 22 +++++++++++++ src/acl/acl.test.ts | 2 +- src/acp/policy.test.ts | 2 +- src/fetcher.test.ts | 2 +- src/resource/nonRdfData.test.ts | 12 +++---- src/resource/resource.test.ts | 47 ++++++++++++-------------- src/resource/solidDataset.test.ts | 55 +++++++++++++++---------------- 7 files changed, 77 insertions(+), 65 deletions(-) diff --git a/jest.config.js b/jest.config.js index 1bc73fa000..b62b7ab130 100644 --- a/jest.config.js +++ b/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", @@ -17,4 +38,5 @@ module.exports = { // By default we only run unit tests: "e2e.test.ts", ], + injectGlobals: false, }; diff --git a/src/acl/acl.test.ts b/src/acl/acl.test.ts index a08dc68963..07a48a637b 100644 --- a/src/acl/acl.test.ts +++ b/src/acl/acl.test.ts @@ -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( diff --git a/src/acp/policy.test.ts b/src/acp/policy.test.ts index 48d0930c83..6a74825e75 100644 --- a/src/acp/policy.test.ts +++ b/src/acp/policy.test.ts @@ -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( diff --git a/src/fetcher.test.ts b/src/fetcher.test.ts index 0b3a108573..d850667725 100644 --- a/src/fetcher.test.ts +++ b/src/fetcher.test.ts @@ -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"; diff --git a/src/resource/nonRdfData.test.ts b/src/resource/nonRdfData.test.ts index b90a96b40b..a170855f80 100644 --- a/src/resource/nonRdfData.test.ts +++ b/src/resource/nonRdfData.test.ts @@ -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 @@ -31,6 +31,7 @@ jest.mock("../fetcher", () => ({ ), })); +import type { Mock } from "jest-mock"; import { getFile, deleteFile, @@ -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)); }); @@ -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, [RequestInfo, RequestInit?] >; @@ -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, - [RequestInfo, RequestInit?] - >; + fetch: MockFetch; }; fetcher.fetch = setMockOnFetch( diff --git a/src/resource/resource.test.ts b/src/resource/resource.test.ts index 94f58361b1..4a1acf8d72 100644 --- a/src/resource/resource.test.ts +++ b/src/resource/resource.test.ts @@ -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( @@ -56,13 +56,15 @@ function mockResponse( return new Response(body, init); } +type MockedFetch = jest.Mock< + ReturnType, + Parameters +>; + describe("fetchAcl", () => { it("calls the included fetcher by default", async () => { const mockedFetcher = jest.requireMock("../fetcher.ts") as { - fetch: jest.Mock< - ReturnType, - [RequestInfo, RequestInit?] - >; + fetch: MockedFetch; }; const mockResourceInfo: WithResourceInfo = { @@ -116,7 +118,7 @@ describe("fetchAcl", () => { return Promise.resolve( mockResponse(undefined, { headers: headers, - url: url, + url: url as string, }) ); }); @@ -163,7 +165,7 @@ describe("fetchAcl", () => { return Promise.resolve( mockResponse(undefined, { headers: headers, - url: url, + url: url as string, }) ); }); @@ -202,7 +204,7 @@ describe("getResourceInfoWithAcl", () => { return Promise.resolve( mockResponse(undefined, { headers: headers, - url: url, + url: url as string, }) ); }); @@ -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, - [RequestInfo, RequestInit?] - >; + fetch: MockedFetch; }; await getResourceInfoWithAcl("https://some.pod/resource"); @@ -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, - [RequestInfo, RequestInit?] - >; + fetch: MockedFetch; }; await getResourceInfo("https://some.pod/resource"); @@ -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: '; 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: '; 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", diff --git a/src/resource/solidDataset.test.ts b/src/resource/solidDataset.test.ts index 27ceda3859..cbd367ba98 100644 --- a/src/resource/solidDataset.test.ts +++ b/src/resource/solidDataset.test.ts @@ -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( @@ -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: '; 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: '; 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", @@ -349,7 +350,7 @@ describe("getSolidDatasetWithAcl", () => { return Promise.resolve( mockResponse(undefined, { headers: headers, - url: url, + url: url as string, }) ); }); @@ -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: '; 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: '; 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/", @@ -1565,9 +1565,9 @@ describe("createContainerAt", () => { }); describe("saveSolidDatasetInContainer", () => { - type MockFetch = jest.Mock< + type MockFetch = Mock< ReturnType, - [RequestInfo, RequestInit?] + Parameters >; function setMockOnFetch( fetch: MockFetch, @@ -1926,7 +1926,7 @@ describe("saveSolidDatasetInContainer", () => { }); describe("createContainerInContainer", () => { - type MockFetch = jest.Mock< + type MockFetch = Mock< ReturnType, [RequestInfo, RequestInit?] >; @@ -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, - [RequestInfo, RequestInit?] - >; + fetch: MockFetch; }; mockedFetcher.fetch = setMockOnFetch(mockedFetcher.fetch);