Skip to content

Commit 920adfb

Browse files
430 - Add testing framework (#456)
* Added testing framework with Sponsors tests * Added Jest for NodeJS backend * Updated CI GitHub Action * add mapping for @common directory to jest config * add simple backend test * Removed inclusion --------- Co-authored-by: Franco Reyes <franco.javier.reyes@gmail.com>
1 parent 5d9064b commit 920adfb

File tree

10 files changed

+12588
-3788
lines changed

10 files changed

+12588
-3788
lines changed

.github/workflows/ci.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ jobs:
2929
- name: Lint
3030
run: npm run lint
3131
working-directory: ${{ matrix.component }}
32-
build:
33-
name: "Build (${{ matrix.component }})"
32+
build-and-test:
33+
name: "Build and test (${{ matrix.component }})"
3434
runs-on: ubuntu-latest
3535
strategy:
3636
fail-fast: false
@@ -56,3 +56,6 @@ jobs:
5656
- name: Build
5757
run: npm run build
5858
working-directory: ${{ matrix.component }}
59+
- name: Test
60+
run: npm test
61+
working-directory: ${{ matrix.component }}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { calculateStatus } from "../src/helpers";
2+
3+
const TEST_DATE = new Date(2024, 2, 24, 12);
4+
5+
describe("calculateStatus", () => {
6+
it("is available all day if there are no bookings", () => {
7+
expect(calculateStatus(TEST_DATE, [], 0)).toEqual({
8+
status: "free",
9+
endtime: "",
10+
});
11+
});
12+
});

backend/jest.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
preset: "ts-jest",
3+
testEnvironment: "node",
4+
roots: ["<rootDir>/"],
5+
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
6+
moduleNameMapper: {
7+
"@common/(.*)": "<rootDir>/../common/$1",
8+
},
9+
};

backend/package-lock.json

+4,446-1,311
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
"description": "Freerooms backend",
55
"main": "dist/index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
87
"start": "nodemon src/index.ts",
98
"build": "tsc -p tsconfig.json && tsc-alias -p tsconfig.json",
109
"lint": "prettier --check **/*.ts && eslint **/*.ts",
11-
"lint:fix": "prettier --write **/*.ts && eslint --fix **/*.ts"
10+
"lint:fix": "prettier --write **/*.ts && eslint --fix **/*.ts",
11+
"test": "jest"
1212
},
1313
"repository": {
1414
"type": "git",
@@ -32,17 +32,20 @@
3232
"devDependencies": {
3333
"@types/cors": "2.8.13",
3434
"@types/express": "4.17.17",
35-
"@types/node": "20.11.30",
35+
"@types/jest": "^29.5.12",
36+
"@types/node": "^20.11.30",
3637
"@typescript-eslint/eslint-plugin": "^7.3.1",
3738
"@typescript-eslint/parser": "^7.3.1",
3839
"eslint": "^8.57.0",
3940
"eslint-config-prettier": "^9.1.0",
4041
"eslint-plugin-simple-import-sort": "^12.0.0",
42+
"jest": "^29.7.0",
4143
"nodemon": "^3.0.0",
4244
"prettier": "^3.2.5",
45+
"ts-jest": "^29.1.2",
4346
"ts-node": "10.9.1",
4447
"tsc-alias": "^1.8.7",
4548
"tsconfig-paths": "^4.2.0",
46-
"typescript": "5.4.3"
49+
"typescript": "^5.4.3"
4750
}
4851
}

backend/tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"baseUrl": ".",
1111
"paths": {
1212
"@common/*": ["../common/*"]
13-
}
13+
},
14+
"types": ["jest"]
1415
},
15-
"include": ["./src/*.ts"]
16+
"include": ["./src/*.ts", "./__tests__/*.ts"]
1617
}

frontend/__tests__/Sponsors.test.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import "@testing-library/jest-dom";
2+
3+
import { render, screen } from "@testing-library/react";
4+
5+
import Sponsors from "../components/Sponsors";
6+
7+
describe("Sponsors", () => {
8+
it("renders heading text", () => {
9+
render(<Sponsors />);
10+
11+
const heading = screen.getByRole("heading", {
12+
level: 1,
13+
name: "Our Sponsors",
14+
});
15+
16+
expect(heading).toBeInTheDocument();
17+
});
18+
19+
it("shows TikTok logo", () => {
20+
render(<Sponsors />);
21+
22+
const tiktok = screen.getByRole("img", { name: "TikTok" });
23+
24+
expect(tiktok).toBeInTheDocument();
25+
});
26+
27+
it("shows Jane Street logo", () => {
28+
render(<Sponsors />);
29+
30+
const janeStreet = screen.getByRole("img", { name: "Jane Street" });
31+
32+
expect(janeStreet).toBeInTheDocument();
33+
});
34+
});

frontend/jest.config.ts

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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: "/private/var/folders/j1/2t1df0x94rl734r2l3yt54y00000gn/T/jest_dx",
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: true,
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: "coverage",
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+
"@common/(.*)": "<rootDir>/../common/$1",
101+
},
102+
103+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
104+
// modulePathIgnorePatterns: [],
105+
106+
// Activates notifications for test results
107+
// notify: false,
108+
109+
// An enum that specifies notification mode. Requires { notify: true }
110+
// notifyMode: "failure-change",
111+
112+
// A preset that is used as a base for Jest's configuration
113+
// preset: undefined,
114+
115+
// Run tests from one or more projects
116+
// projects: undefined,
117+
118+
// Use this configuration option to add custom reporters to Jest
119+
// reporters: undefined,
120+
121+
// Automatically reset mock state before every test
122+
// resetMocks: false,
123+
124+
// Reset the module registry before running each individual test
125+
// resetModules: false,
126+
127+
// A path to a custom resolver
128+
// resolver: undefined,
129+
130+
// Automatically restore mock state and implementation before every test
131+
// restoreMocks: false,
132+
133+
// The root directory that Jest should scan for tests and modules within
134+
// rootDir: undefined,
135+
136+
// A list of paths to directories that Jest should use to search for files in
137+
// roots: [
138+
// "<rootDir>"
139+
// ],
140+
141+
// Allows you to use a custom runner instead of Jest's default test runner
142+
// runner: "jest-runner",
143+
144+
// The paths to modules that run some code to configure or set up the testing environment before each test
145+
// setupFiles: [],
146+
147+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
148+
// setupFilesAfterEnv: [],
149+
150+
// The number of seconds after which a test is considered as slow and reported as such in the results.
151+
// slowTestThreshold: 5,
152+
153+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
154+
// snapshotSerializers: [],
155+
156+
// The test environment that will be used for testing
157+
testEnvironment: "jsdom",
158+
159+
// Options that will be passed to the testEnvironment
160+
// testEnvironmentOptions: {},
161+
162+
// Adds a location field to test results
163+
// testLocationInResults: false,
164+
165+
// The glob patterns Jest uses to detect test files
166+
// testMatch: [
167+
// "**/__tests__/**/*.[jt]s?(x)",
168+
// "**/?(*.)+(spec|test).[tj]s?(x)"
169+
// ],
170+
171+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
172+
// testPathIgnorePatterns: [
173+
// "/node_modules/"
174+
// ],
175+
176+
// The regexp pattern or array of patterns that Jest uses to detect test files
177+
// testRegex: [],
178+
179+
// This option allows the use of a custom results processor
180+
// testResultsProcessor: undefined,
181+
182+
// This option allows use of a custom test runner
183+
// testRunner: "jest-circus/runner",
184+
185+
// A map from regular expressions to paths to transformers
186+
// transform: undefined,
187+
188+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
189+
// transformIgnorePatterns: [
190+
// "/node_modules/",
191+
// "\\.pnp\\.[^\\/]+$"
192+
// ],
193+
194+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
195+
// unmockedModulePathPatterns: undefined,
196+
197+
// Indicates whether each individual test should be reported during the run
198+
// verbose: undefined,
199+
200+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
201+
// watchPathIgnorePatterns: [],
202+
203+
// Whether to use watchman for file crawling
204+
// watchman: true,
205+
};
206+
207+
export default createJestConfig(config);

0 commit comments

Comments
 (0)