Skip to content

Commit

Permalink
test: mock api endpoints and let unit tests use those endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandenaardweg committed Dec 5, 2022
1 parent 1b658bb commit 6082fb3
Show file tree
Hide file tree
Showing 18 changed files with 1,036 additions and 34 deletions.
21 changes: 8 additions & 13 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,12 @@ export default {
// ],

// An array of file extensions your modules use
// moduleFileExtensions: [
// "js",
// "mjs",
// "cjs",
// "jsx",
// "ts",
// "tsx",
// "json",
// "node"
// ],
// moduleFileExtensions: ["js", "ts", "cjs", "json"],

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
Expand Down Expand Up @@ -135,7 +128,7 @@ export default {
// setupFiles: [],

// A list of paths to modules that run some code to configure or set up the testing framework before each test
// setupFilesAfterEnv: [],
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],

// The number of seconds after which a test is considered as slow and reported as such in the results.
// slowTestThreshold: 5,
Expand Down Expand Up @@ -168,7 +161,9 @@ export default {
// testRunner: "jest-circus/runner",

// A map from regular expressions to paths to transformers
// transform: undefined,
// transform: {
// "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/node_modules/ts-jest",
// },

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
transformIgnorePatterns: ["/node_modules/", "\\.pnp\\.[^\\/]+$"],
Expand Down
19 changes: 19 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { server } from "@/api/mocks/server";

beforeAll(() => {
// Establish API mocking before all tests.
server.listen();
});

afterEach(() => {
// Reset any MSW request handlers that we may add during the tests,
// so they don't affect other tests.
server.resetHandlers();

jest.clearAllMocks();
});

afterAll(() => {
// Clean up after the tests are finished.
server.close();
});

0 comments on commit 6082fb3

Please sign in to comment.