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 9ada34a commit 511daba
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 97 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
20 changes: 16 additions & 4 deletions 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 Expand Up @@ -56,7 +56,11 @@ const policyUrl = "https://some.pod/policy-resource";

describe("savePolicyDatasetAt", () => {
it("sets the type of acp:AccessPolicy if not set yet", async () => {
const mockFetch = jest.fn(window.fetch).mockResolvedValue(new Response());
// The type assertion to any is necessary due to invalid Jest type definitions:
// https://github.com/facebook/jest/pull/10600
const mockFetch = jest
.fn(window.fetch)
.mockResolvedValue(new Response() as any);
const newDataset = createSolidDataset();

const savedDataset = await savePolicyDatasetAt(policyUrl, newDataset, {
Expand All @@ -69,7 +73,11 @@ describe("savePolicyDatasetAt", () => {
});

it("overwrites an existing type that might be set", async () => {
const mockFetch = jest.fn(window.fetch).mockResolvedValue(new Response());
// The type assertion to any is necessary due to invalid Jest type definitions:
// https://github.com/facebook/jest/pull/10600
const mockFetch = jest
.fn(window.fetch)
.mockResolvedValue(new Response() as any);
let newDatasetThing = createThing({ url: policyUrl });
newDatasetThing = setUrl(
newDatasetThing,
Expand Down Expand Up @@ -103,7 +111,11 @@ describe("savePolicyDatasetAt", () => {
});

it("uses the given fetcher if provided", async () => {
const mockFetch = jest.fn(window.fetch).mockResolvedValue(new Response());
// The type assertion to any is necessary due to invalid Jest type definitions:
// https://github.com/facebook/jest/pull/10600
const mockFetch = jest
.fn(window.fetch)
.mockResolvedValue(new Response() as any);

await savePolicyDatasetAt(policyUrl, createSolidDataset(), {
fetch: mockFetch,
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
18 changes: 9 additions & 9 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 @@ -472,7 +473,7 @@ describe("Write non-RDF data into a folder", () => {
type: "binary",
} as Blob;

type MockFetch = jest.Mock<
type MockFetch = Mock<
ReturnType<typeof window.fetch>,
[RequestInfo, RequestInit?]
>;
Expand All @@ -489,8 +490,10 @@ describe("Write non-RDF data into a folder", () => {
} as ResponseInit)
): MockFetch {
fetch
.mockResolvedValueOnce(saveResponse)
.mockResolvedValueOnce(headResponse);
// The type assertions to any are necessary due to invalid Jest type definitions:
// https://github.com/facebook/jest/pull/10600
.mockResolvedValueOnce(saveResponse as any)
.mockResolvedValueOnce(headResponse as any);
return fetch;
}

Expand Down Expand Up @@ -579,10 +582,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
55 changes: 28 additions & 27 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 @@ -54,13 +54,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 @@ -114,7 +116,7 @@ describe("fetchAcl", () => {
return Promise.resolve(
mockResponse(undefined, {
headers: headers,
url: url,
url: url as string,
})
);
});
Expand Down Expand Up @@ -161,7 +163,7 @@ describe("fetchAcl", () => {
return Promise.resolve(
mockResponse(undefined, {
headers: headers,
url: url,
url: url as string,
})
);
});
Expand Down Expand Up @@ -200,7 +202,7 @@ describe("getResourceInfoWithAcl", () => {
return Promise.resolve(
mockResponse(undefined, {
headers: headers,
url: url,
url: url as string,
})
);
});
Expand Down Expand Up @@ -230,10 +232,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 @@ -336,10 +335,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 @@ -533,7 +529,9 @@ describe("getResourceInfo", () => {
const mockFetch = jest.fn(window.fetch).mockResolvedValue(
mockResponse(undefined, {
url: "https://arbitrary.pod",
})
// The type assertion to any is necessary due to invalid Jest type definitions:
// https://github.com/facebook/jest/pull/10600
}) as any
);

const solidDatasetInfo = await getResourceInfo(
Expand All @@ -545,16 +543,19 @@ 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);
// The type assertion to any is necessary due to invalid Jest type definitions:
// https://github.com/facebook/jest/pull/10600
const mockFetch = jest
.fn(window.fetch)
.mockResolvedValue(mockResponse as any);

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

0 comments on commit 511daba

Please sign in to comment.