Skip to content

Commit ec7e140

Browse files
authored
6 setup jest (#10)
- updated jest config - added example tests - added make target - added rtl config overrides and exposed as module
1 parent e708a0f commit ec7e140

File tree

9 files changed

+49
-11
lines changed

9 files changed

+49
-11
lines changed

.eslintignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
jest.config.ts
2-
jest.setup.ts
2+
jest.setup.ts
3+
react-testing-library.tsx

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/node_modules
2-
/dist
2+
/dist
3+
/coverage

.vscode/react.code-snippets

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"React Component Test": {
3535
"prefix": "retes",
3636
"body": [
37-
"//import { render, screen } from '@tests-unit-browser';",
37+
"// import { render, screen } from '@test-utils';",
3838
"import '@testing-library/jest-dom';",
3939
"",
4040
"describe('${TM_DIRECTORY/.*\\/(.*)$/$1/}', () => {",

jest.config.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ const compilerOptions = jsonc.parse(tsconfigContent).compilerOptions;
99

1010
const config = {
1111
testEnvironment: "jsdom",
12-
transformIgnorePatterns: [
13-
// @fluentui/react-icons
14-
"node_modules/(?!(@fluentui/react-icons)/)",
15-
],
12+
transformIgnorePatterns: ["node_modules/(?!(@fluentui/react-icons)/)"],
1613
verbose: true,
1714
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
1815
moduleFileExtensions: ["ts", "tsx", "js"],
@@ -21,6 +18,16 @@ const config = {
2118
}),
2219
testMatch: ["**/tests.{ts,tsx}"],
2320
collectCoverage: true,
21+
transform: {
22+
"^.+\\.tsx?$": [
23+
"ts-jest",
24+
{
25+
tsconfig: "./tsconfig.json",
26+
useESM: true,
27+
},
28+
],
29+
},
30+
preset: "ts-jest",
2431
};
2532

2633
export default config;

react-testing-library.tsx

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { ReactNode } from "react";
2+
3+
import { render } from "@testing-library/react";
4+
import type { RenderOptions } from "@testing-library/react";
5+
6+
import { ThemeProvider } from "./src/theme";
7+
8+
function AllTheProviders({ children }: { children: ReactNode }) {
9+
return <ThemeProvider>{children}</ThemeProvider>;
10+
}
11+
12+
const customRender = (
13+
ui: ReactNode,
14+
options?: Omit<RenderOptions, "wrapper">
15+
) => render(ui, { wrapper: AllTheProviders, ...options });
16+
17+
export * from "@testing-library/react";
18+
19+
export { customRender as render };

src/components/layout/Flex/tests.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// import { render, screen } from '@test-utils';
2+
import "@testing-library/jest-dom";
3+
4+
describe("Flex", () => {
5+
it("should render", () => {
6+
expect(true).toBe(true);
7+
});
8+
});

src/theme/Provider/func.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import { useClasses, useStaticStyles } from "@theme/Provider/styles";
66

77
export default function ThemeProvider({
88
children,
9-
theme,
9+
theme = "light",
1010
}: {
1111
children: ReactNode;
12-
theme: "light" | "dark";
12+
theme?: "light" | "dark";
1313
}) {
1414
useStaticStyles();
1515

src/theme/Provider/tests.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, screen } from "@tests-unit-browser";
1+
import { render, screen } from "@test-utils";
22
import "@testing-library/jest-dom";
33

44
import ThemeProvider from "@theme/Provider/func";

tsconfig.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"lib": ["ES2020", "DOM", "DOM.Iterable"],
1010
"module": "ESNext",
1111
"skipLibCheck": true,
12+
"esModuleInterop": true,
1213

1314
// Bundler mode
1415
"moduleResolution": "bundler",
@@ -23,7 +24,8 @@
2324
"@animations/*": ["src/animations/*"],
2425
"@components/*": ["src/components/*"],
2526
"@hooks/*": ["src/hooks/*"],
26-
"@theme/*": ["src/theme/*"]
27+
"@theme/*": ["src/theme/*"],
28+
"@test-utils": ["react-testing-library.tsx"]
2729
}
2830
},
2931
"include": ["src"]

0 commit comments

Comments
 (0)