From f58700e68635424c09512522874dc0fa1059d0ee Mon Sep 17 00:00:00 2001 From: Darren Ackers Date: Fri, 24 Apr 2020 16:57:47 +0100 Subject: [PATCH] tests: start integration of Jest tests (#3529) [skip ci] --- .github/workflows/tests_jest.yml | 33 +++++++++++++++++++ babel.config.js | 14 ++++++++ jest.config.js | 8 +++++ jest.setup.js | 26 +++++++++++++++ package.json | 26 +++++++++------ packages/functions/.npmignore | 2 ++ .../functions/__tests__/functions.test.js | 12 +++++++ packages/perf/.npmignore | 2 ++ packages/perf/__tests__/perf.test.js | 9 +++++ 9 files changed, 122 insertions(+), 10 deletions(-) create mode 100644 .github/workflows/tests_jest.yml create mode 100644 jest.config.js create mode 100644 jest.setup.js create mode 100644 packages/functions/__tests__/functions.test.js create mode 100644 packages/perf/__tests__/perf.test.js diff --git a/.github/workflows/tests_jest.yml b/.github/workflows/tests_jest.yml new file mode 100644 index 00000000..fcec7667 --- /dev/null +++ b/.github/workflows/tests_jest.yml @@ -0,0 +1,33 @@ +name: Testing + +on: + pull_request: + branches: + - '**' + +jobs: + jest: + name: Jest + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + with: + fetch-depth: 1 + - uses: actions/setup-node@v1 + with: + node-version: 12 + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "::set-output name=dir::$(yarn cache dir)" + - uses: actions/cache@v1 + name: Yarn Cache + id: yarn-cache + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/package.json') }} + restore-keys: | + ${{ runner.os }}-yarn- + - name: Yarn Install + run: yarn --no-audit --prefer-offline + - name: Jest + run: yarn run tests:jest-coverage diff --git a/babel.config.js b/babel.config.js index eaf678de..5bc27df5 100644 --- a/babel.config.js +++ b/babel.config.js @@ -10,6 +10,20 @@ module.exports = { ], ], }, + test: { + presets: [ + [ + '@babel/preset-env', + { + targets: { + node: 'current', + }, + }, + ], + 'module:./tests/node_modules/metro-react-native-babel-preset', + '@babel/preset-flow', + ], + }, publish: { presets: [ [ diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 00000000..fc8252fd --- /dev/null +++ b/jest.config.js @@ -0,0 +1,8 @@ +module.exports = { + maxConcurrency: 10, + preset: './tests/node_modules/react-native/jest-preset.js', + setupFiles: ['./jest.setup.js'], + testMatch: ['**/packages/**/__tests__/**/*.test.js'], + modulePaths: ['node_modules', './tests/node_modules'], + testPathIgnorePatterns: ['./packages/template'], +}; diff --git a/jest.setup.js b/jest.setup.js new file mode 100644 index 00000000..ebf3db47 --- /dev/null +++ b/jest.setup.js @@ -0,0 +1,26 @@ +import * as ReactNative from 'react-native'; + +jest.doMock('react-native', () => { + return Object.setPrototypeOf( + { + Platform: { + OS: 'android', + }, + NativeModules: { + ...ReactNative.NativeModules, + RNFBAppModule: { + NATIVE_FIREBASE_APPS: [ + { + appConfig: { + name: '[DEFAULT]', + }, + options: {}, + }, + ], + }, + RNFBPerfModule: {}, + }, + }, + ReactNative, + ); +}); diff --git a/package.json b/package.json index 76cd39ec..40ce2e3e 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,9 @@ "lerna:bootstrap": "lerna bootstrap", "lerna:link": "lerna link", "lerna:clean": "lerna clean", + "tests:jest": "jest", + "tests:jest-watch": "jest --watch", + "tests:jest-coverage": "jest --coverage", "gen:reference": "node scripts/generate-typedoc.js", "tests:packager:chrome": "cd tests && node_modules/.bin/react-native start --reset-cache", "tests:packager:jet": "cd tests && cross-env REACT_DEBUGGER=\"echo nope\" node_modules/.bin/react-native start --no-interactive", @@ -31,23 +34,26 @@ "format:markdown": "prettier --write \"docs/**/*.md\"" }, "devDependencies": { - "inquirer": "^7.1.0", - "shelljs": "^0.8.3", - "codecov": "^3.6.5", - "cross-env": "^7.0.2", - "genversion": "^2.2.0", - "lerna": "3.20.2", - "rimraf": "^3.0.2", - "typedoc": "^0.15.0", - "typescript": "^3.8.3", "@types/react-native": "^0.62.0", "@typescript-eslint/eslint-plugin": "^2.18.0", "@typescript-eslint/parser": "^2.18.0", + "codecov": "^3.6.5", + "cross-env": "^7.0.2", "eslint": "^6.8.0", "eslint-config-prettier": "^6.5.0", "eslint-plugin-prettier": "^3.1.1", "eslint-plugin-react": "^7.19.0", - "prettier": "^1.19.1" + "genversion": "^2.2.0", + "inquirer": "^7.1.0", + "lerna": "3.20.2", + "prettier": "^1.19.1", + "rimraf": "^3.0.2", + "shelljs": "^0.8.3", + "typedoc": "^0.15.0", + "typescript": "^3.8.3", + "jest": "^24.9.0", + "@babel/preset-env": "7.9.5", + "@babel/preset-flow": "7.9.0" }, "workspaces": { "packages": [ diff --git a/packages/functions/.npmignore b/packages/functions/.npmignore index 29e5aa19..22e1f1ec 100644 --- a/packages/functions/.npmignore +++ b/packages/functions/.npmignore @@ -64,3 +64,5 @@ android/.settings .circleci .eslintignore type-test.ts + +__tests__ diff --git a/packages/functions/__tests__/functions.test.js b/packages/functions/__tests__/functions.test.js new file mode 100644 index 00000000..bc3d987a --- /dev/null +++ b/packages/functions/__tests__/functions.test.js @@ -0,0 +1,12 @@ +import functions from '../lib'; + +describe('Cloud Functions', () => { + describe('useFunctionsEmulator()', () => { + it('useFunctionsEmulator -> uses 10.0.2.2', () => { + functions().useFunctionsEmulator('http://localhost'); + expect(functions()._useFunctionsEmulatorOrigin).toBe('http://10.0.2.2'); + functions().useFunctionsEmulator('http://127.0.0.1'); + expect(functions()._useFunctionsEmulatorOrigin).toBe('http://10.0.2.2'); + }); + }); +}); diff --git a/packages/perf/.npmignore b/packages/perf/.npmignore index 29e5aa19..22e1f1ec 100644 --- a/packages/perf/.npmignore +++ b/packages/perf/.npmignore @@ -64,3 +64,5 @@ android/.settings .circleci .eslintignore type-test.ts + +__tests__ diff --git a/packages/perf/__tests__/perf.test.js b/packages/perf/__tests__/perf.test.js new file mode 100644 index 00000000..6f8ed8a3 --- /dev/null +++ b/packages/perf/__tests__/perf.test.js @@ -0,0 +1,9 @@ +import perf from '../lib'; + +describe('Performance Monitoring', () => { + describe('setPerformanceCollectionEnabled', () => { + it('errors if not boolean', async () => { + expect(() => perf().setPerformanceCollectionEnabled()).toThrow('must be a boolean'); + }); + }); +});