From 20ce86d1142a876b2de07aaf81599d3f8a2f70d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hampus=20Sj=C3=B6berg?= Date: Fri, 31 May 2019 04:48:10 +0200 Subject: [PATCH] Fixing jest --- __tests__/App-test.tsx | 2 +- assetsTransformer.js | 8 ++++++++ jest.config.js | 11 +++++++++-- jestSetup.js | 16 ++++++++++++++++ mocks/{Camera.tsx => react-native-camera.tsx} | 0 package.json | 2 +- 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 assetsTransformer.js create mode 100644 jestSetup.js rename mocks/{Camera.tsx => react-native-camera.tsx} (100%) diff --git a/__tests__/App-test.tsx b/__tests__/App-test.tsx index 333ca6636..7d4d23418 100644 --- a/__tests__/App-test.tsx +++ b/__tests__/App-test.tsx @@ -5,7 +5,7 @@ import App from "../src/App"; import renderer from "react-test-renderer"; jest.mock("react-native-camera", () => { - return require("../mocks/Camera"); + return require("../mocks/react-native-camera"); }); diff --git a/assetsTransformer.js b/assetsTransformer.js new file mode 100644 index 000000000..4020bb046 --- /dev/null +++ b/assetsTransformer.js @@ -0,0 +1,8 @@ +// https://github.com/facebook/jest/issues/2663#issuecomment-317109798 +const path = require('path'); + +module.exports = { + process(src, filename, config, options) { + return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';'; + }, +}; diff --git a/jest.config.js b/jest.config.js index 4546a18a2..2f9559370 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,6 +2,13 @@ module.exports = { preset: 'react-native', moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], transformIgnorePatterns: [ - "node_modules/(?!react-native|@shoutem|react-clone-referenced-element|native-base-shoutem-theme|react-native-camera)" - ] + "node_modules/(?!react-native|@shoutem|react-clone-referenced-element|native-base-shoutem-theme|react-native-camera|@react-navigation|react-navigation-tabs)" + ], + "setupFiles": [ + "./jestSetup.js" + ], + "moduleNameMapper": { + "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "/assetsTransformer.js", + "\\.(css|less)$": "/assetsTransformer.js" + } } diff --git a/jestSetup.js b/jestSetup.js new file mode 100644 index 000000000..2d8ce66bb --- /dev/null +++ b/jestSetup.js @@ -0,0 +1,16 @@ +// https://github.com/kmagiera/react-native-gesture-handler/issues/344#issuecomment-450859200 +import { NativeModules as RNNativeModules } from "react-native"; + +RNNativeModules.UIManager = RNNativeModules.UIManager || {}; +RNNativeModules.UIManager.RCTView = RNNativeModules.UIManager.RCTView || {}; +RNNativeModules.RNGestureHandlerModule = RNNativeModules.RNGestureHandlerModule || { + State: { BEGAN: "BEGAN", FAILED: "FAILED", ACTIVE: "ACTIVE", END: "END" }, + attachGestureHandler: jest.fn(), + createGestureHandler: jest.fn(), + dropGestureHandler: jest.fn(), + updateGestureHandler: jest.fn(), + +}; +RNNativeModules.PlatformConstants = RNNativeModules.PlatformConstants || { + forceTouchAvailable: false +}; diff --git a/mocks/Camera.tsx b/mocks/react-native-camera.tsx similarity index 100% rename from mocks/Camera.tsx rename to mocks/react-native-camera.tsx diff --git a/package.json b/package.json index d8219ca73..125585a7e 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "scripts": { "start": "node node_modules/react-native/local-cli/cli.js start", "test": "jest", - "test-update-snapshot": "jest -u --transformIgnorePatterns \"node_modules/(?!react-native|native-base|native-base-shoutem-theme)\"" + "test-update-snapshot": "jest -u" }, "dependencies": { "@types/qrcode": "1.3.3",