Skip to content

Commit 510e78e

Browse files
committed
Setup jest and testing library
1 parent 2749a4a commit 510e78e

8 files changed

+10525
-3455
lines changed

__mocks__/fileMock.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "test-file-stub";

__mocks__/nextFontMock.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = new Proxy(
2+
{},
3+
{
4+
get: function getter() {
5+
return () => ({
6+
className: "className",
7+
variable: "variable",
8+
style: { fontFamily: "fontFamily" },
9+
});
10+
},
11+
}
12+
);

__mocks__/styleMock.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = {};
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { getCategoryIdsFromLabels } from "@/helpers";
2+
import { Label } from "@/types";
3+
4+
describe("getCategoryIdsFromLabels", () => {
5+
const labels: Label[] = [
6+
{
7+
id: "1",
8+
name: "Label 1",
9+
user_id: "user1",
10+
category_ids: ["cat1", "cat2"],
11+
color: "#ffffff",
12+
},
13+
{
14+
id: "2",
15+
name: "Label 2",
16+
user_id: "user1",
17+
category_ids: ["cat2", "cat3"],
18+
color: "#ffffff",
19+
},
20+
{
21+
id: "3",
22+
name: "Label 3",
23+
user_id: "user2",
24+
category_ids: ["cat3", "cat4"],
25+
color: "#ffffff",
26+
},
27+
];
28+
29+
test("returns empty array when labelIds is empty", () => {
30+
const categoryIds = getCategoryIdsFromLabels({ labelIds: [], labels });
31+
expect(categoryIds).toEqual([]);
32+
});
33+
34+
test("returns empty array when no labels match labelIds", () => {
35+
const categoryIds = getCategoryIdsFromLabels({
36+
labelIds: ["4", "5"],
37+
labels,
38+
});
39+
expect(categoryIds).toEqual([]);
40+
});
41+
42+
test("returns unique categoryIds from matching labels", () => {
43+
const categoryIds = getCategoryIdsFromLabels({
44+
labelIds: ["1", "2"],
45+
labels,
46+
});
47+
expect(categoryIds).toEqual(["cat1", "cat2", "cat3"]);
48+
});
49+
50+
test("ignores labels without category_ids", () => {
51+
const labelsWithNoCategories: Label[] = [
52+
{ id: "4", name: "Label 4", user_id: "user3", color: "#ffffff" },
53+
{ id: "5", name: "Label 5", user_id: "user4", color: "#ffffff" },
54+
];
55+
const categoryIds = getCategoryIdsFromLabels({
56+
labelIds: ["4", "5"],
57+
labels: labelsWithNoCategories,
58+
});
59+
expect(categoryIds).toEqual([]);
60+
});
61+
});

jest.config.ts

+205
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/**
2+
* For a detailed explanation regarding each configuration property, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
import type { Config } from "jest";
7+
import nextJest from "next/jest.js";
8+
9+
const createJestConfig = nextJest({
10+
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
11+
dir: "./",
12+
});
13+
14+
const config: Config = {
15+
// All imported modules in your tests should be mocked automatically
16+
// automock: false,
17+
18+
// Stop running tests after `n` failures
19+
// bail: 0,
20+
21+
// The directory where Jest should store its cached dependency information
22+
// cacheDirectory: "C:\\Users\\bohda\\AppData\\Local\\Temp\\jest",
23+
24+
// Automatically clear mock calls, instances, contexts and results before every test
25+
clearMocks: true,
26+
27+
// Indicates whether the coverage information should be collected while executing the test
28+
// collectCoverage: false,
29+
30+
// An array of glob patterns indicating a set of files for which coverage information should be collected
31+
// collectCoverageFrom: undefined,
32+
33+
// The directory where Jest should output its coverage files
34+
// coverageDirectory: undefined,
35+
36+
// An array of regexp pattern strings used to skip coverage collection
37+
// coveragePathIgnorePatterns: [
38+
// "\\\\node_modules\\\\"
39+
// ],
40+
41+
// Indicates which provider should be used to instrument code for coverage
42+
coverageProvider: "v8",
43+
44+
// A list of reporter names that Jest uses when writing coverage reports
45+
// coverageReporters: [
46+
// "json",
47+
// "text",
48+
// "lcov",
49+
// "clover"
50+
// ],
51+
52+
// An object that configures minimum threshold enforcement for coverage results
53+
// coverageThreshold: undefined,
54+
55+
// A path to a custom dependency extractor
56+
// dependencyExtractor: undefined,
57+
58+
// Make calling deprecated APIs throw helpful error messages
59+
// errorOnDeprecated: false,
60+
61+
// The default configuration for fake timers
62+
// fakeTimers: {
63+
// "enableGlobally": false
64+
// },
65+
66+
// Force coverage collection from ignored files using an array of glob patterns
67+
// forceCoverageMatch: [],
68+
69+
// A path to a module which exports an async function that is triggered once before all test suites
70+
// globalSetup: undefined,
71+
72+
// A path to a module which exports an async function that is triggered once after all test suites
73+
// globalTeardown: undefined,
74+
75+
// A set of global variables that need to be available in all test environments
76+
// globals: {},
77+
78+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
79+
// maxWorkers: "50%",
80+
81+
// An array of directory names to be searched recursively up from the requiring module's location
82+
// moduleDirectories: [
83+
// "node_modules"
84+
// ],
85+
86+
// An array of file extensions your modules use
87+
// moduleFileExtensions: [
88+
// "js",
89+
// "mjs",
90+
// "cjs",
91+
// "jsx",
92+
// "ts",
93+
// "tsx",
94+
// "json",
95+
// "node"
96+
// ],
97+
98+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
99+
// moduleNameMapper: {},
100+
101+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
102+
// modulePathIgnorePatterns: [],
103+
104+
// Activates notifications for test results
105+
// notify: false,
106+
107+
// An enum that specifies notification mode. Requires { notify: true }
108+
// notifyMode: "failure-change",
109+
110+
// A preset that is used as a base for Jest's configuration
111+
// preset: undefined,
112+
113+
// Run tests from one or more projects
114+
// projects: undefined,
115+
116+
// Use this configuration option to add custom reporters to Jest
117+
// reporters: undefined,
118+
119+
// Automatically reset mock state before every test
120+
// resetMocks: false,
121+
122+
// Reset the module registry before running each individual test
123+
// resetModules: false,
124+
125+
// A path to a custom resolver
126+
// resolver: undefined,
127+
128+
// Automatically restore mock state and implementation before every test
129+
// restoreMocks: false,
130+
131+
// The root directory that Jest should scan for tests and modules within
132+
// rootDir: undefined,
133+
134+
// A list of paths to directories that Jest should use to search for files in
135+
// roots: [
136+
// "<rootDir>"
137+
// ],
138+
139+
// Allows you to use a custom runner instead of Jest's default test runner
140+
// runner: "jest-runner",
141+
142+
// The paths to modules that run some code to configure or set up the testing environment before each test
143+
// setupFiles: [],
144+
145+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
146+
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
147+
148+
// The number of seconds after which a test is considered as slow and reported as such in the results.
149+
// slowTestThreshold: 5,
150+
151+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
152+
// snapshotSerializers: [],
153+
154+
// The test environment that will be used for testing
155+
// testEnvironment: "jest-environment-node",
156+
157+
// Options that will be passed to the testEnvironment
158+
// testEnvironmentOptions: {},
159+
160+
// Adds a location field to test results
161+
// testLocationInResults: false,
162+
163+
// The glob patterns Jest uses to detect test files
164+
// testMatch: [
165+
// "**/__tests__/**/*.[jt]s?(x)",
166+
// "**/?(*.)+(spec|test).[tj]s?(x)"
167+
// ],
168+
169+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
170+
// testPathIgnorePatterns: [
171+
// "\\\\node_modules\\\\"
172+
// ],
173+
174+
// The regexp pattern or array of patterns that Jest uses to detect test files
175+
// testRegex: [],
176+
177+
// This option allows the use of a custom results processor
178+
// testResultsProcessor: undefined,
179+
180+
// This option allows use of a custom test runner
181+
// testRunner: "jest-circus/runner",
182+
183+
// A map from regular expressions to paths to transformers
184+
// transform: undefined,
185+
186+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
187+
// transformIgnorePatterns: [
188+
// "\\\\node_modules\\\\",
189+
// "\\.pnp\\.[^\\\\]+$"
190+
// ],
191+
192+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
193+
// unmockedModulePathPatterns: undefined,
194+
195+
// Indicates whether each individual test should be reported during the run
196+
// verbose: undefined,
197+
198+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
199+
// watchPathIgnorePatterns: [],
200+
201+
// Whether to use watchman for file crawling
202+
// watchman: true,
203+
};
204+
205+
export default createJestConfig(config);

jest.setup.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "@testing-library/jest-dom";

0 commit comments

Comments
 (0)