From ff698abca38d8cb88e8c6b41ad33b4fb06c653de Mon Sep 17 00:00:00 2001 From: VickyStash Date: Tue, 19 Mar 2024 12:29:13 +0100 Subject: [PATCH 1/4] [TS migration] Migrate remaining Mock files --- __mocks__/@react-native-clipboard/clipboard.js | 3 --- __mocks__/@react-native-clipboard/clipboard.ts | 6 ++++++ ...icker.js => react-native-document-picker.ts} | 15 ++++++++++++--- src/types/modules/react-native-clipboard.d.ts | 17 ++++------------- 4 files changed, 22 insertions(+), 19 deletions(-) delete mode 100644 __mocks__/@react-native-clipboard/clipboard.js create mode 100644 __mocks__/@react-native-clipboard/clipboard.ts rename __mocks__/{react-native-document-picker.js => react-native-document-picker.ts} (64%) diff --git a/__mocks__/@react-native-clipboard/clipboard.js b/__mocks__/@react-native-clipboard/clipboard.js deleted file mode 100644 index e56e290c3cc9..000000000000 --- a/__mocks__/@react-native-clipboard/clipboard.js +++ /dev/null @@ -1,3 +0,0 @@ -import MockClipboard from '@react-native-clipboard/clipboard/jest/clipboard-mock'; - -export default MockClipboard; diff --git a/__mocks__/@react-native-clipboard/clipboard.ts b/__mocks__/@react-native-clipboard/clipboard.ts new file mode 100644 index 000000000000..c92f2be8a6e4 --- /dev/null +++ b/__mocks__/@react-native-clipboard/clipboard.ts @@ -0,0 +1,6 @@ +import type ClipboardModule from '@react-native-clipboard/clipboard'; +import MockClipboard from '@react-native-clipboard/clipboard/jest/clipboard-mock'; + +const Clipboard: typeof ClipboardModule = MockClipboard; + +export default Clipboard; diff --git a/__mocks__/react-native-document-picker.js b/__mocks__/react-native-document-picker.ts similarity index 64% rename from __mocks__/react-native-document-picker.js rename to __mocks__/react-native-document-picker.ts index 8cba2bc1eba4..6d26a0227fc3 100644 --- a/__mocks__/react-native-document-picker.js +++ b/__mocks__/react-native-document-picker.ts @@ -1,9 +1,16 @@ -export default { - getConstants: jest.fn(), +import type {pick, pickDirectory, releaseSecureAccess, types} from 'react-native-document-picker'; + +type ReactNativeDocumentPickerMock = { + pick: typeof pick; + releaseSecureAccess: typeof releaseSecureAccess; + pickDirectory: typeof pickDirectory; + types: typeof types; +}; + +const reactNativeDocumentPickerMock: ReactNativeDocumentPickerMock = { pick: jest.fn(), releaseSecureAccess: jest.fn(), pickDirectory: jest.fn(), - types: Object.freeze({ allFiles: 'public.item', audio: 'public.audio', @@ -21,3 +28,5 @@ export default { zip: 'public.zip-archive', }), }; + +export default reactNativeDocumentPickerMock; diff --git a/src/types/modules/react-native-clipboard.d.ts b/src/types/modules/react-native-clipboard.d.ts index 14f418a3f8b9..066ca89666fa 100644 --- a/src/types/modules/react-native-clipboard.d.ts +++ b/src/types/modules/react-native-clipboard.d.ts @@ -1,16 +1,7 @@ declare module '@react-native-clipboard/clipboard/jest/clipboard-mock' { - const mockClipboard: { - getString: jest.MockedFunction<() => Promise>; - getImagePNG: jest.MockedFunction<() => void>; - getImageJPG: jest.MockedFunction<() => void>; - setImage: jest.MockedFunction<() => void>; - setString: jest.MockedFunction<() => void>; - hasString: jest.MockedFunction<() => Promise>; - hasImage: jest.MockedFunction<() => Promise>; - hasURL: jest.MockedFunction<() => Promise>; - addListener: jest.MockedFunction<() => void>; - removeAllListeners: jest.MockedFunction<() => void>; - useClipboard: jest.MockedFunction<() => [string, jest.MockedFunction<() => void>]>; - }; + import type Clipboard from '@react-native-clipboard/clipboard'; + + const mockClipboard: typeof Clipboard; + export default mockClipboard; } From 710647b113fe21942644096865b275c33612fdd4 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Tue, 19 Mar 2024 15:08:06 +0100 Subject: [PATCH 2/4] [TS migration] Migrate release-profile script --- package.json | 4 ++-- .../{release-profile.js => release-profile.ts} | 17 +++++++++-------- tsconfig.json | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) rename scripts/{release-profile.js => release-profile.ts} (85%) diff --git a/package.json b/package.json index d1974b99b91e..a14f3880d042 100644 --- a/package.json +++ b/package.json @@ -50,8 +50,8 @@ "analyze-packages": "ANALYZE_BUNDLE=true webpack --config config/webpack/webpack.common.js --env envFile=.env.production", "symbolicate:android": "npx metro-symbolicate android/app/build/generated/sourcemaps/react/release/index.android.bundle.map", "symbolicate:ios": "npx metro-symbolicate main.jsbundle.map", - "symbolicate-release:ios": "scripts/release-profile.js --platform=ios", - "symbolicate-release:android": "scripts/release-profile.js --platform=android", + "symbolicate-release:ios": "scripts/release-profile.ts --platform=ios", + "symbolicate-release:android": "scripts/release-profile.ts --platform=android", "test:e2e": "ts-node tests/e2e/testRunner.ts --config ./config.local.ts", "test:e2e:dev": "ts-node tests/e2e/testRunner.ts --config ./config.dev.ts", "gh-actions-unused-styles": "./.github/scripts/findUnusedKeys.sh", diff --git a/scripts/release-profile.js b/scripts/release-profile.ts similarity index 85% rename from scripts/release-profile.js rename to scripts/release-profile.ts index 0f96232bcdca..2f82adebf21e 100755 --- a/scripts/release-profile.js +++ b/scripts/release-profile.ts @@ -1,13 +1,15 @@ -#!/usr/bin/env node +#!/usr/bin/env ts-node + /* eslint-disable no-console */ +import {execSync} from 'child_process'; +import fs from 'fs'; -const fs = require('fs'); -const {execSync} = require('child_process'); +type ArgsMap = Record; // Function to parse command-line arguments into a key-value object -function parseCommandLineArguments() { +function parseCommandLineArguments(): ArgsMap { const args = process.argv.slice(2); // Skip node and script paths - const argsMap = {}; + const argsMap: ArgsMap = {}; args.forEach((arg) => { const [key, value] = arg.split('='); if (key.startsWith('--')) { @@ -20,14 +22,13 @@ function parseCommandLineArguments() { // Function to find .cpuprofile files in the current directory function findCpuProfileFiles() { const files = fs.readdirSync(process.cwd()); - // eslint-disable-next-line rulesdir/prefer-underscore-method return files.filter((file) => file.endsWith('.cpuprofile')); } const argsMap = parseCommandLineArguments(); // Determine sourcemapPath based on the platform flag passed -let sourcemapPath; +let sourcemapPath: string | undefined; if (argsMap.platform === 'ios') { sourcemapPath = 'main.jsbundle.map'; } else if (argsMap.platform === 'android') { @@ -57,7 +58,7 @@ if (cpuProfiles.length === 0) { const output = execSync(command, {stdio: 'inherit'}); console.log(output.toString()); } catch (error) { - console.error(`Error executing command: ${error}`); + console.error(`Error executing command: ${error as string}`); process.exit(1); } } diff --git a/tsconfig.json b/tsconfig.json index 30708f63d12b..342846c11c29 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -48,6 +48,6 @@ } }, "exclude": ["**/node_modules/*", "**/dist/*", ".github/actions/**/index.js", "**/docs/*"], - "include": ["src", "desktop", "web", "website", "docs", "assets", "config", "tests", "jest", "__mocks__", ".github/**/*", ".storybook/**/*"], + "include": ["src", "desktop", "web", "website", "docs", "assets", "config", "tests", "jest", "__mocks__", ".github/**/*", ".storybook/**/*", "scripts"], "extends": "expo/tsconfig.base" } From a4f2d46efe1d8299a92116821b2701fd00e2185f Mon Sep 17 00:00:00 2001 From: VickyStash Date: Wed, 20 Mar 2024 15:38:17 +0100 Subject: [PATCH 3/4] Code improvements --- __mocks__/@react-native-clipboard/clipboard.ts | 7 ++----- scripts/release-profile.ts | 5 ++++- src/types/modules/react-native-clipboard.d.ts | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/__mocks__/@react-native-clipboard/clipboard.ts b/__mocks__/@react-native-clipboard/clipboard.ts index c92f2be8a6e4..75b6f09f5345 100644 --- a/__mocks__/@react-native-clipboard/clipboard.ts +++ b/__mocks__/@react-native-clipboard/clipboard.ts @@ -1,6 +1,3 @@ -import type ClipboardModule from '@react-native-clipboard/clipboard'; -import MockClipboard from '@react-native-clipboard/clipboard/jest/clipboard-mock'; +import clipboardMock from '@react-native-clipboard/clipboard/jest/clipboard-mock'; -const Clipboard: typeof ClipboardModule = MockClipboard; - -export default Clipboard; +export default clipboardMock; diff --git a/scripts/release-profile.ts b/scripts/release-profile.ts index 2f82adebf21e..7bc77fcbfba3 100755 --- a/scripts/release-profile.ts +++ b/scripts/release-profile.ts @@ -58,7 +58,10 @@ if (cpuProfiles.length === 0) { const output = execSync(command, {stdio: 'inherit'}); console.log(output.toString()); } catch (error) { - console.error(`Error executing command: ${error as string}`); + if (error instanceof Error) { + console.error(`Error executing command: ${error}`); + } + process.exit(1); } } diff --git a/src/types/modules/react-native-clipboard.d.ts b/src/types/modules/react-native-clipboard.d.ts index 066ca89666fa..c38b3b2ff7cc 100644 --- a/src/types/modules/react-native-clipboard.d.ts +++ b/src/types/modules/react-native-clipboard.d.ts @@ -1,7 +1,7 @@ declare module '@react-native-clipboard/clipboard/jest/clipboard-mock' { import type Clipboard from '@react-native-clipboard/clipboard'; - const mockClipboard: typeof Clipboard; + const clipboardMock: typeof Clipboard; - export default mockClipboard; + export default clipboardMock; } From 45bdd47737b9913e4e285e3787885a0cc7ffa367 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Wed, 20 Mar 2024 15:51:45 +0100 Subject: [PATCH 4/4] Fix lint error --- scripts/release-profile.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/release-profile.ts b/scripts/release-profile.ts index 7bc77fcbfba3..8ec0979f9f9e 100755 --- a/scripts/release-profile.ts +++ b/scripts/release-profile.ts @@ -59,7 +59,7 @@ if (cpuProfiles.length === 0) { console.log(output.toString()); } catch (error) { if (error instanceof Error) { - console.error(`Error executing command: ${error}`); + console.error(`Error executing command: ${error.toString()}`); } process.exit(1);