Skip to content

Commit 88e8bb0

Browse files
authored
feat: make @testing-library/dom dependency optional (#319)
1 parent a7ee93c commit 88e8bb0

File tree

3 files changed

+183
-2
lines changed

3 files changed

+183
-2
lines changed

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
"@testing-library/dom": "^8.0.0 || ^9.0.0",
5959
"eslint": "^6.8.0 || ^7.0.0 || ^8.0.0"
6060
},
61+
"peerDependenciesMeta": {
62+
"@testing-library/dom": {
63+
"optional": true
64+
}
65+
},
6166
"eslintConfig": {
6267
"extends": "./node_modules/kcd-scripts/eslint.js",
6368
"rules": {

src/__tests__/queries.test.js

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
const TestingLibraryDomRef = { throwWhenRequiring: false };
2+
3+
const requireQueries = (throwWhenRequiring) => {
4+
jest.resetModules();
5+
6+
TestingLibraryDomRef.throwWhenRequiring = throwWhenRequiring;
7+
8+
return require("../queries");
9+
};
10+
11+
jest.mock("@testing-library/dom", () => {
12+
if (TestingLibraryDomRef.throwWhenRequiring) {
13+
throw new (class extends Error {
14+
constructor(message) {
15+
super(message);
16+
this.code = "MODULE_NOT_FOUND";
17+
}
18+
})();
19+
}
20+
21+
return jest.requireActual("@testing-library/dom");
22+
});
23+
24+
describe("when @testing-library/dom is not available", () => {
25+
it("uses the default queries", () => {
26+
const { queries } = requireQueries(true);
27+
28+
expect([...queries].sort()).toStrictEqual([
29+
"findAllByAltText",
30+
"findAllByDisplayValue",
31+
"findAllByLabelText",
32+
"findAllByPlaceholderText",
33+
"findAllByRole",
34+
"findAllByTestId",
35+
"findAllByText",
36+
"findAllByTitle",
37+
"findByAltText",
38+
"findByDisplayValue",
39+
"findByLabelText",
40+
"findByPlaceholderText",
41+
"findByRole",
42+
"findByTestId",
43+
"findByText",
44+
"findByTitle",
45+
"getAllByAltText",
46+
"getAllByDisplayValue",
47+
"getAllByLabelText",
48+
"getAllByPlaceholderText",
49+
"getAllByRole",
50+
"getAllByTestId",
51+
"getAllByText",
52+
"getAllByTitle",
53+
"getByAltText",
54+
"getByDisplayValue",
55+
"getByLabelText",
56+
"getByPlaceholderText",
57+
"getByRole",
58+
"getByTestId",
59+
"getByText",
60+
"getByTitle",
61+
"queryAllByAltText",
62+
"queryAllByDisplayValue",
63+
"queryAllByLabelText",
64+
"queryAllByPlaceholderText",
65+
"queryAllByRole",
66+
"queryAllByTestId",
67+
"queryAllByText",
68+
"queryAllByTitle",
69+
"queryByAltText",
70+
"queryByDisplayValue",
71+
"queryByLabelText",
72+
"queryByPlaceholderText",
73+
"queryByRole",
74+
"queryByTestId",
75+
"queryByText",
76+
"queryByTitle",
77+
]);
78+
});
79+
});
80+
81+
describe("when @testing-library/dom is available", () => {
82+
it("returns the queries from the library", () => {
83+
const { queries } = requireQueries(false);
84+
85+
expect([...queries].sort()).toStrictEqual([
86+
"findAllByAltText",
87+
"findAllByDisplayValue",
88+
"findAllByLabelText",
89+
"findAllByPlaceholderText",
90+
"findAllByRole",
91+
"findAllByTestId",
92+
"findAllByText",
93+
"findAllByTitle",
94+
"findByAltText",
95+
"findByDisplayValue",
96+
"findByLabelText",
97+
"findByPlaceholderText",
98+
"findByRole",
99+
"findByTestId",
100+
"findByText",
101+
"findByTitle",
102+
"getAllByAltText",
103+
"getAllByDisplayValue",
104+
"getAllByLabelText",
105+
"getAllByPlaceholderText",
106+
"getAllByRole",
107+
"getAllByTestId",
108+
"getAllByText",
109+
"getAllByTitle",
110+
"getByAltText",
111+
"getByDisplayValue",
112+
"getByLabelText",
113+
"getByPlaceholderText",
114+
"getByRole",
115+
"getByTestId",
116+
"getByText",
117+
"getByTitle",
118+
"queryAllByAltText",
119+
"queryAllByDisplayValue",
120+
"queryAllByLabelText",
121+
"queryAllByPlaceholderText",
122+
"queryAllByRole",
123+
"queryAllByTestId",
124+
"queryAllByText",
125+
"queryAllByTitle",
126+
"queryByAltText",
127+
"queryByDisplayValue",
128+
"queryByLabelText",
129+
"queryByPlaceholderText",
130+
"queryByRole",
131+
"queryByTestId",
132+
"queryByText",
133+
"queryByTitle",
134+
]);
135+
});
136+
137+
it("re-throws unexpected errors", () => {
138+
jest.mock("@testing-library/dom", () => {
139+
throw new Error("oh noes!");
140+
});
141+
142+
jest.resetModules();
143+
144+
expect(() => require("../queries")).toThrow(/oh noes!/iu);
145+
});
146+
});

src/queries.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
1-
import { queries as allQueries } from "@testing-library/dom";
1+
let theQueries = [
2+
"findAllBy",
3+
"findBy",
4+
"getAllBy",
5+
"getBy",
6+
"queryAllBy",
7+
"queryBy",
8+
].flatMap((prefix) =>
9+
[
10+
"AltText",
11+
"DisplayValue",
12+
"LabelText",
13+
"PlaceholderText",
14+
"Role",
15+
"TestId",
16+
"Text",
17+
"Title",
18+
].map((element) => `${prefix}${element}`)
19+
);
220

3-
export const queries = Object.keys(allQueries);
21+
(() => {
22+
try {
23+
const { queries: allQueries } = require("@testing-library/dom");
24+
25+
theQueries = Object.keys(allQueries);
26+
} catch (error) {
27+
if (error.code !== "MODULE_NOT_FOUND") {
28+
throw error;
29+
}
30+
}
31+
})();
32+
33+
export const queries = theQueries;

0 commit comments

Comments
 (0)