From 0dd1ddaac3c2017b81d88d469519ac6870953fed Mon Sep 17 00:00:00 2001 From: Kilian Finger Date: Mon, 21 Oct 2024 17:03:16 +0200 Subject: [PATCH] chore: configure jest to use with ts --- jest-setup.ts | 113 +++++++++++++++++++ jest.config.js | 8 ++ package.json | 15 --- packages/react-native-app/e2e/jest.config.js | 12 +- setup-jest.js | 108 ------------------ tsconfig.json | 4 +- 6 files changed, 129 insertions(+), 131 deletions(-) create mode 100644 jest-setup.ts create mode 100644 jest.config.js delete mode 100644 setup-jest.js diff --git a/jest-setup.ts b/jest-setup.ts new file mode 100644 index 000000000..646c151ff --- /dev/null +++ b/jest-setup.ts @@ -0,0 +1,113 @@ +/* eslint-disable no-undef, import/no-extraneous-dependencies */ +import "@testing-library/react-native/extend-expect"; +import { NativeModules } from "react-native"; + +// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing +jest.mock("react-native/Libraries/Animated/NativeAnimatedHelper"); + +function keyMirror(keys: string[]) { + const obj: Record = {}; + keys.forEach((key) => (obj[key] = key)); + return obj; +} + +// Mock of what the native code puts on the JS object +NativeModules.MLNModule = { + // constants + UserTrackingModes: keyMirror([ + "None", + "Follow", + "FollowWithCourse", + "FollowWithHeading", + ]), + StyleURL: keyMirror(["Default"]), + EventTypes: keyMirror([ + "MapClick", + "MapLongClick", + "RegionWillChange", + "RegionIsChanging", + "RegionDidChange", + "WillStartLoadingMap", + "DidFinishLoadingMap", + "DidFailLoadingMap", + "WillStartRenderingFrame", + "DidFinishRenderingFrame", + "DidFinishRenderingFrameFully", + "DidFinishLoadingStyle", + "SetCameraComplete", + ]), + CameraModes: keyMirror(["Flight", "Ease", "None"]), + StyleSource: keyMirror(["DefaultSourceID"]), + InterpolationMode: keyMirror([ + "Exponential", + "Categorical", + "Interval", + "Identity", + ]), + LineJoin: keyMirror(["Bevel", "Round", "Miter"]), + LineCap: keyMirror(["Butt", "Round", "Square"]), + LineTranslateAnchor: keyMirror(["Map", "Viewport"]), + CirclePitchScale: keyMirror(["Map", "Viewport"]), + CircleTranslateAnchor: keyMirror(["Map", "Viewport"]), + FillExtrusionTranslateAnchor: keyMirror(["Map", "Viewport"]), + FillTranslateAnchor: keyMirror(["Map", "Viewport"]), + IconRotationAlignment: keyMirror(["Auto", "Map", "Viewport"]), + IconTextFit: keyMirror(["None", "Width", "Height", "Both"]), + IconTranslateAnchor: keyMirror(["Map", "Viewport"]), + SymbolPlacement: keyMirror(["Line", "Point"]), + TextAnchor: keyMirror([ + "Center", + "Left", + "Right", + "Top", + "Bottom", + "TopLeft", + "TopRight", + "BottomLeft", + "BottomRight", + ]), + TextJustify: keyMirror(["Center", "Left", "Right"]), + TextPitchAlignment: keyMirror(["Auto", "Map", "Viewport"]), + TextRotationAlignment: keyMirror(["Auto", "Map", "Viewport"]), + TextTransform: keyMirror(["None", "Lowercase", "Uppercase"]), + TextTranslateAnchor: keyMirror(["Map", "Viewport"]), + LightAnchor: keyMirror(["Map", "Viewport"]), + OfflinePackDownloadState: keyMirror(["Inactive", "Active", "Complete"]), + OfflineCallbackName: keyMirror(["Progress", "Error"]), + + // methods + setAccessToken: jest.fn(), + getAccessToken: () => Promise.resolve("test-token"), + setConnected: jest.fn(), +}; + +NativeModules.MLNOfflineModule = { + createPack: (packOptions: any) => { + return Promise.resolve({ + bounds: packOptions.bounds, + metadata: JSON.stringify({ name: packOptions.name }), + }); + }, + getPacks: () => Promise.resolve([]), + deletePack: () => Promise.resolve(), + getPackStatus: () => Promise.resolve({}), + pausePackDownload: () => Promise.resolve(), + resumePackDownload: () => Promise.resolve(), + setPackObserver: () => Promise.resolve(), + setTileCountLimit: jest.fn(), + setProgressEventThrottle: jest.fn(), +}; + +NativeModules.MLNSnapshotModule = { + takeSnap: () => { + return Promise.resolve("file://test.png"); + }, +}; + +NativeModules.MLNLocationModule = { + getLastKnownLocation: jest.fn(), + setMinDisplacement: jest.fn(), + start: jest.fn(), + stop: jest.fn(), + pause: jest.fn(), +}; diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 000000000..b735c7cba --- /dev/null +++ b/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + preset: "@testing-library/react-native", + moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"], + setupFilesAfterEnv: ["./jest-setup.ts"], + setupFiles: ["./__tests__/__mocks__/react-native.mock.js"], + modulePathIgnorePatterns: ["__tests__/__mocks__", "fixtures"], + collectCoverageFrom: ["javascript/**/*.{ts,tsx,js,jsx}"], +}; diff --git a/package.json b/package.json index c8c3bda76..93d4d023a 100644 --- a/package.json +++ b/package.json @@ -103,21 +103,6 @@ "resolutions": { "typescript": "5.5.3" }, - "jest": { - "preset": "react-native", - "collectCoverageFrom": [ - "javascript/**/*.js" - ], - "setupFiles": [ - "./setup-jest.js", - "./__tests__/__mocks__/react-native.mock.js" - ], - "modulePathIgnorePatterns": [ - "example", - "__tests__/__mocks__", - "fixtures" - ] - }, "lint-staged": { "*.{js,jsx,ts,tsx}": "yarn lint" }, diff --git a/packages/react-native-app/e2e/jest.config.js b/packages/react-native-app/e2e/jest.config.js index be6efbfe7..9ae87b59b 100644 --- a/packages/react-native-app/e2e/jest.config.js +++ b/packages/react-native-app/e2e/jest.config.js @@ -1,12 +1,12 @@ /** @type {import('@jest/types').Config.InitialOptions} */ module.exports = { - rootDir: '..', - testMatch: ['/e2e/**/*.e2e.js'], + rootDir: "..", + testMatch: ["/e2e/**/*.e2e.js"], testTimeout: 120000, maxWorkers: 1, - globalSetup: 'detox/runners/jest/globalSetup', - globalTeardown: 'detox/runners/jest/globalTeardown', - reporters: ['detox/runners/jest/reporter'], - testEnvironment: 'detox/runners/jest/testEnvironment', + globalSetup: "detox/runners/jest/globalSetup", + globalTeardown: "detox/runners/jest/globalTeardown", + reporters: ["detox/runners/jest/reporter"], + testEnvironment: "detox/runners/jest/testEnvironment", verbose: true, }; diff --git a/setup-jest.js b/setup-jest.js deleted file mode 100644 index 6ce08f31d..000000000 --- a/setup-jest.js +++ /dev/null @@ -1,108 +0,0 @@ -import {NativeModules} from 'react-native'; - -function keyMirror(keys) { - const obj = {}; - keys.forEach(key => (obj[key] = key)); - return obj; -} - -// Mock of what the native code puts on the JS object -NativeModules.MLNModule = { - // constants - UserTrackingModes: keyMirror([ - 'None', - 'Follow', - 'FollowWithCourse', - 'FollowWithHeading', - ]), - StyleURL: keyMirror(['Default']), - EventTypes: keyMirror([ - 'MapClick', - 'MapLongClick', - 'RegionWillChange', - 'RegionIsChanging', - 'RegionDidChange', - 'WillStartLoadingMap', - 'DidFinishLoadingMap', - 'DidFailLoadingMap', - 'WillStartRenderingFrame', - 'DidFinishRenderingFrame', - 'DidFinishRenderingFrameFully', - 'DidFinishLoadingStyle', - 'SetCameraComplete', - ]), - CameraModes: keyMirror(['Flight', 'Ease', 'None']), - StyleSource: keyMirror(['DefaultSourceID']), - InterpolationMode: keyMirror([ - 'Exponential', - 'Categorical', - 'Interval', - 'Identity', - ]), - LineJoin: keyMirror(['Bevel', 'Round', 'Miter']), - LineCap: keyMirror(['Butt', 'Round', 'Square']), - LineTranslateAnchor: keyMirror(['Map', 'Viewport']), - CirclePitchScale: keyMirror(['Map', 'Viewport']), - CircleTranslateAnchor: keyMirror(['Map', 'Viewport']), - FillExtrusionTranslateAnchor: keyMirror(['Map', 'Viewport']), - FillTranslateAnchor: keyMirror(['Map', 'Viewport']), - IconRotationAlignment: keyMirror(['Auto', 'Map', 'Viewport']), - IconTextFit: keyMirror(['None', 'Width', 'Height', 'Both']), - IconTranslateAnchor: keyMirror(['Map', 'Viewport']), - SymbolPlacement: keyMirror(['Line', 'Point']), - TextAnchor: keyMirror([ - 'Center', - 'Left', - 'Right', - 'Top', - 'Bottom', - 'TopLeft', - 'TopRight', - 'BottomLeft', - 'BottomRight', - ]), - TextJustify: keyMirror(['Center', 'Left', 'Right']), - TextPitchAlignment: keyMirror(['Auto', 'Map', 'Viewport']), - TextRotationAlignment: keyMirror(['Auto', 'Map', 'Viewport']), - TextTransform: keyMirror(['None', 'Lowercase', 'Uppercase']), - TextTranslateAnchor: keyMirror(['Map', 'Viewport']), - LightAnchor: keyMirror(['Map', 'Viewport']), - OfflinePackDownloadState: keyMirror(['Inactive', 'Active', 'Complete']), - OfflineCallbackName: keyMirror(['Progress', 'Error']), - - // methods - setAccessToken: jest.fn(), - getAccessToken: () => Promise.resolve('test-token'), - setConnected: jest.fn(), -}; - -NativeModules.MLNOfflineModule = { - createPack: packOptions => { - return Promise.resolve({ - bounds: packOptions.bounds, - metadata: JSON.stringify({name: packOptions.name}), - }); - }, - getPacks: () => Promise.resolve([]), - deletePack: () => Promise.resolve(), - getPackStatus: () => Promise.resolve({}), - pausePackDownload: () => Promise.resolve(), - resumePackDownload: () => Promise.resolve(), - setPackObserver: () => Promise.resolve(), - setTileCountLimit: jest.fn(), - setProgressEventThrottle: jest.fn(), -}; - -NativeModules.MLNSnapshotModule = { - takeSnap: () => { - return Promise.resolve('file://test.png'); - }, -}; - -NativeModules.MLNLocationModule = { - getLastKnownLocation: jest.fn(), - setMinDisplacement: jest.fn(), - start: jest.fn(), - stop: jest.fn(), - pause: jest.fn(), -}; diff --git a/tsconfig.json b/tsconfig.json index 9cf78eaed..71f06c61d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,12 +10,12 @@ "isolatedModules": true, "skipLibCheck": true, "strict": true, - "types": ["node", "react-native", "geojson"], + "types": ["node", "react-native", "geojson", "jest"], "moduleResolution": "node", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "noUnusedLocals": true, - "noImplicitReturns": true, + "noImplicitReturns": true }, "include": [ "javascript/**/*", "index.ts"