Skip to content

feat: make @testing-library/dom dependency optional #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
"@testing-library/dom": "^8.0.0 || ^9.0.0",
"eslint": "^6.8.0 || ^7.0.0 || ^8.0.0"
},
"peerDependenciesMeta": {
"@testing-library/dom": {
"optional": true
}
},
"eslintConfig": {
"extends": "./node_modules/kcd-scripts/eslint.js",
"rules": {
Expand Down
146 changes: 146 additions & 0 deletions src/__tests__/queries.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
const TestingLibraryDomRef = { throwWhenRequiring: false };

const requireQueries = (throwWhenRequiring) => {
jest.resetModules();

TestingLibraryDomRef.throwWhenRequiring = throwWhenRequiring;

return require("../queries");
};

jest.mock("@testing-library/dom", () => {
if (TestingLibraryDomRef.throwWhenRequiring) {
throw new (class extends Error {
constructor(message) {
super(message);
this.code = "MODULE_NOT_FOUND";
}
})();
}

return jest.requireActual("@testing-library/dom");
});

describe("when @testing-library/dom is not available", () => {
it("uses the default queries", () => {
const { queries } = requireQueries(true);

expect([...queries].sort()).toStrictEqual([
"findAllByAltText",
"findAllByDisplayValue",
"findAllByLabelText",
"findAllByPlaceholderText",
"findAllByRole",
"findAllByTestId",
"findAllByText",
"findAllByTitle",
"findByAltText",
"findByDisplayValue",
"findByLabelText",
"findByPlaceholderText",
"findByRole",
"findByTestId",
"findByText",
"findByTitle",
"getAllByAltText",
"getAllByDisplayValue",
"getAllByLabelText",
"getAllByPlaceholderText",
"getAllByRole",
"getAllByTestId",
"getAllByText",
"getAllByTitle",
"getByAltText",
"getByDisplayValue",
"getByLabelText",
"getByPlaceholderText",
"getByRole",
"getByTestId",
"getByText",
"getByTitle",
"queryAllByAltText",
"queryAllByDisplayValue",
"queryAllByLabelText",
"queryAllByPlaceholderText",
"queryAllByRole",
"queryAllByTestId",
"queryAllByText",
"queryAllByTitle",
"queryByAltText",
"queryByDisplayValue",
"queryByLabelText",
"queryByPlaceholderText",
"queryByRole",
"queryByTestId",
"queryByText",
"queryByTitle",
]);
});
});

describe("when @testing-library/dom is available", () => {
it("returns the queries from the library", () => {
const { queries } = requireQueries(false);

expect([...queries].sort()).toStrictEqual([
"findAllByAltText",
"findAllByDisplayValue",
"findAllByLabelText",
"findAllByPlaceholderText",
"findAllByRole",
"findAllByTestId",
"findAllByText",
"findAllByTitle",
"findByAltText",
"findByDisplayValue",
"findByLabelText",
"findByPlaceholderText",
"findByRole",
"findByTestId",
"findByText",
"findByTitle",
"getAllByAltText",
"getAllByDisplayValue",
"getAllByLabelText",
"getAllByPlaceholderText",
"getAllByRole",
"getAllByTestId",
"getAllByText",
"getAllByTitle",
"getByAltText",
"getByDisplayValue",
"getByLabelText",
"getByPlaceholderText",
"getByRole",
"getByTestId",
"getByText",
"getByTitle",
"queryAllByAltText",
"queryAllByDisplayValue",
"queryAllByLabelText",
"queryAllByPlaceholderText",
"queryAllByRole",
"queryAllByTestId",
"queryAllByText",
"queryAllByTitle",
"queryByAltText",
"queryByDisplayValue",
"queryByLabelText",
"queryByPlaceholderText",
"queryByRole",
"queryByTestId",
"queryByText",
"queryByTitle",
]);
});

it("re-throws unexpected errors", () => {
jest.mock("@testing-library/dom", () => {
throw new Error("oh noes!");
});

jest.resetModules();

expect(() => require("../queries")).toThrow(/oh noes!/iu);
});
});
34 changes: 32 additions & 2 deletions src/queries.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
import { queries as allQueries } from "@testing-library/dom";
let theQueries = [
"findAllBy",
"findBy",
"getAllBy",
"getBy",
"queryAllBy",
"queryBy",
].flatMap((prefix) =>
[
"AltText",
"DisplayValue",
"LabelText",
"PlaceholderText",
"Role",
"TestId",
"Text",
"Title",
].map((element) => `${prefix}${element}`)
);

export const queries = Object.keys(allQueries);
(() => {
try {
const { queries: allQueries } = require("@testing-library/dom");

theQueries = Object.keys(allQueries);
} catch (error) {
if (error.code !== "MODULE_NOT_FOUND") {
throw error;
}
}
})();

export const queries = theQueries;