From ebc392e8fe3756dd0dee05a5a15e21553e0516cd Mon Sep 17 00:00:00 2001 From: Darren Ackers Date: Wed, 29 Apr 2020 16:00:14 +0100 Subject: [PATCH] tests: switch to ts-jest (#3578) [skip-ci] --- .eslintrc.js | 1 + jest.config.js | 10 ++++++++-- jest.setup.js => jest.setup.ts | 0 package.json | 10 ++++++---- .../{functions.test.js => functions.test.ts} | 7 +++++++ .../__tests__/{perf.test.js => perf.test.ts} | 16 +++++++++++++++- 6 files changed, 37 insertions(+), 7 deletions(-) rename jest.setup.js => jest.setup.ts (100%) rename packages/functions/__tests__/{functions.test.js => functions.test.ts} (94%) rename packages/perf/__tests__/{perf.test.js => perf.test.ts} (91%) diff --git a/.eslintrc.js b/.eslintrc.js index 8b98d04c..8ad07749 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -38,6 +38,7 @@ module.exports = { '@typescript-eslint/explicit-function-return-type': 'off', '@typescript-eslint/camelcase': 'off', '@typescript-eslint/no-empty-function': 'off', + '@typescript-eslint/ban-ts-ignore': 'warn', }, globals: { __DEV__: true, diff --git a/jest.config.js b/jest.config.js index fc8252fd..8c2c2459 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,8 +1,14 @@ module.exports = { maxConcurrency: 10, preset: './tests/node_modules/react-native/jest-preset.js', - setupFiles: ['./jest.setup.js'], - testMatch: ['**/packages/**/__tests__/**/*.test.js'], + transform: { + '^.+\\.(js)$': '/node_modules/babel-jest', + '\\.(ts|tsx)$': 'ts-jest', + }, + setupFiles: ['./jest.setup.ts'], + testMatch: ['**/packages/**/__tests__/**/*.test.(ts|js)'], modulePaths: ['node_modules', './tests/node_modules'], testPathIgnorePatterns: ['./packages/template'], + moduleDirectories: ['node_modules', './tests/node_modules'], + moduleFileExtensions: ['ts', 'tsx', 'js'], }; diff --git a/jest.setup.js b/jest.setup.ts similarity index 100% rename from jest.setup.js rename to jest.setup.ts diff --git a/package.json b/package.json index 40ce2e3e..c191c899 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,9 @@ "format:markdown": "prettier --write \"docs/**/*.md\"" }, "devDependencies": { + "@babel/preset-env": "7.9.5", + "@babel/preset-flow": "7.9.0", + "@types/jest": "^25.2.1", "@types/react-native": "^0.62.0", "@typescript-eslint/eslint-plugin": "^2.18.0", "@typescript-eslint/parser": "^2.18.0", @@ -45,15 +48,14 @@ "eslint-plugin-react": "^7.19.0", "genversion": "^2.2.0", "inquirer": "^7.1.0", + "jest": "^25.5.1", "lerna": "3.20.2", "prettier": "^1.19.1", "rimraf": "^3.0.2", "shelljs": "^0.8.3", + "ts-jest": "^25.4.0", "typedoc": "^0.15.0", - "typescript": "^3.8.3", - "jest": "^24.9.0", - "@babel/preset-env": "7.9.5", - "@babel/preset-flow": "7.9.0" + "typescript": "^3.8.3" }, "workspaces": { "packages": [ diff --git a/packages/functions/__tests__/functions.test.js b/packages/functions/__tests__/functions.test.ts similarity index 94% rename from packages/functions/__tests__/functions.test.js rename to packages/functions/__tests__/functions.test.ts index ca2001e7..b3be7692 100644 --- a/packages/functions/__tests__/functions.test.js +++ b/packages/functions/__tests__/functions.test.ts @@ -12,8 +12,13 @@ describe('Cloud Functions', () => { describe('useFunctionsEmulator()', () => { it('useFunctionsEmulator -> uses 10.0.2.2', () => { functions().useFunctionsEmulator('http://localhost'); + + // @ts-ignore expect(functions()._useFunctionsEmulatorOrigin).toBe('http://10.0.2.2'); + functions().useFunctionsEmulator('http://127.0.0.1'); + + // @ts-ignore expect(functions()._useFunctionsEmulatorOrigin).toBe('http://10.0.2.2'); }); }); @@ -22,6 +27,8 @@ describe('Cloud Functions', () => { it('throws an error with an incorrect timeout', () => { try { const app = firebase.app(); + + // @ts-ignore app.functions().httpsCallable('example', { timeout: 'test' }); return Promise.reject(new Error('Did not throw')); } catch (e) { diff --git a/packages/perf/__tests__/perf.test.js b/packages/perf/__tests__/perf.test.ts similarity index 91% rename from packages/perf/__tests__/perf.test.js rename to packages/perf/__tests__/perf.test.ts index b08d589c..85720362 100644 --- a/packages/perf/__tests__/perf.test.js +++ b/packages/perf/__tests__/perf.test.ts @@ -11,7 +11,10 @@ describe('Performance Monitoring', () => { describe('setPerformanceCollectionEnabled', () => { it('errors if not boolean', () => { - expect(() => perf().setPerformanceCollectionEnabled()).toThrow('must be a boolean'); + expect(() => { + // @ts-ignore + perf().setPerformanceCollectionEnabled(); + }).toThrow('must be a boolean'); }); }); @@ -19,11 +22,14 @@ describe('Performance Monitoring', () => { it('returns an instance of Trace', () => { const trace = perf().newTrace('invertase'); expect(trace.constructor.name).toEqual('Trace'); + + // @ts-ignore expect(trace._identifier).toEqual('invertase'); }); it('errors if identifier not a string', () => { try { + // @ts-ignore perf().newTrace(1337); return Promise.reject(new Error('Did not throw')); } catch (e) { @@ -51,12 +57,17 @@ describe('Performance Monitoring', () => { it('returns an instance of HttpMetric', async () => { const metric = perf().newHttpMetric('https://invertase.io', 'GET'); expect(metric.constructor.name).toEqual('HttpMetric'); + + // @ts-ignore expect(metric._url).toEqual('https://invertase.io'); + + // @ts-ignore expect(metric._httpMethod).toEqual('GET'); }); it('errors if url not a string', async () => { try { + // @ts-ignore perf().newHttpMetric(1337, 7331); return Promise.reject(new Error('Did not throw')); } catch (e) { @@ -67,6 +78,7 @@ describe('Performance Monitoring', () => { it('errors if httpMethod not a string', async () => { try { + // @ts-ignore perf().newHttpMetric('https://invertase.io', 1337); return Promise.reject(new Error('Did not throw')); } catch (e) { @@ -79,6 +91,7 @@ describe('Performance Monitoring', () => { it('errors if httpMethod not a valid type', async () => { try { + // @ts-ignore perf().newHttpMetric('https://invertase.io', 'FIRE'); return Promise.reject(new Error('Did not throw')); } catch (e) { @@ -93,6 +106,7 @@ describe('Performance Monitoring', () => { describe('setPerformanceCollectionEnabled()', () => { it('errors if not boolean', async () => { try { + // @ts-ignore firebase.perf().setPerformanceCollectionEnabled(); return Promise.reject(new Error('Did not throw')); } catch (e) {