Skip to content

Commit

Permalink
Convert to TypeScript (#655)
Browse files Browse the repository at this point in the history
* Convert some files to TS

* Convert files to TS

* Fix build

* Convert some tests

* Convert files

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* Fix test

* Fix parsing

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* WIP

* Rebuild yarn.lock

* Pin firebase-admin, fix tests

* WIP

* WIP

* Make cookies types more generalized

* WIP

* WIP

* Import type from correct file

* WIP
  • Loading branch information
kmjennison authored Jul 2, 2023
1 parent 522185a commit 06ff195
Show file tree
Hide file tree
Showing 77 changed files with 4,569 additions and 3,923 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = {
extends: [
'airbnb',
'prettier',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'plugin:import/typescript',
],
Expand Down Expand Up @@ -59,9 +60,13 @@ module.exports = {
{
files: [
'**/*.test.ts',
'**/*.test.tsx',
'**/__mocks__/**/*.ts',
'**/__mocks__/**/*.tsx',
'**/*.test.js',
'**/*.test.jsx',
'**/__mocks__/**/*.js',
'**/__mocks__/**/*.jsx',
],
extends: ['plugin:jest/recommended'],
env: {
Expand Down
2 changes: 2 additions & 0 deletions __mocks__/firebase-admin/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export {}

const firebaseAdminApp = jest.createMockFromModule('firebase-admin/app')

firebaseAdminApp.getApps = jest.fn(() => [])
Expand Down
10 changes: 0 additions & 10 deletions __mocks__/firebase-admin/auth.js

This file was deleted.

15 changes: 15 additions & 0 deletions __mocks__/firebase-admin/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export {}

const { Auth } = jest.requireActual('firebase-admin/auth')

const firebaseAdminAuthMock: jest.Mocked<typeof Auth> =
jest.createMockFromModule('firebase-admin/auth')

const auth = {
createCustomToken: jest.fn(async () => null) as jest.Mock,
verifyIdToken: jest.fn(async () => null) as jest.Mock,
}

firebaseAdminAuthMock.getAuth = jest.fn(() => auth)

module.exports = firebaseAdminAuthMock
9 changes: 8 additions & 1 deletion __mocks__/firebase/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
export {}

const { FirebaseError } = jest.requireActual('firebase/app')

const firebaseAppMock = jest.createMockFromModule('firebase/app')

firebaseAppMock.getApps = jest.fn(() => [])

module.exports = firebaseAppMock
module.exports = {
...firebaseAppMock,
FirebaseError,
}
2 changes: 2 additions & 0 deletions __mocks__/firebase/auth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export {}

const firebaseAuthMock = jest.createMockFromModule('firebase/auth')

const mockOnIdTokenChangedUnsubscribe = jest.fn()
Expand Down
122 changes: 0 additions & 122 deletions index.tests.ts

This file was deleted.

10 changes: 8 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ module.exports = {
// Support absolute imports; e.g. from 'src/*'
// https://stackoverflow.com/a/72437265/1332513
moduleDirectories: ['node_modules', '<rootDir>'],
testPathIgnorePatterns: ['/node_modules/', '/.next/', '/.yalc/', '/.git/'],
testPathIgnorePatterns: [
'/node_modules/',
'/.next/',
'/.yalc/',
'/.git/',
'build/',
],
transform: {
'^.+\\.(js|jsx)$': '<rootDir>/node_modules/babel-jest',
'^.+\\.(ts|tsx)$': 'ts-jest',
},
transformIgnorePatterns: [
'/node_modules/(?!(firebase|@firebase)/)',
'/node_modules/(?!(firebase|@firebase|jose)/)',
'/.yalc/',
'^.+\\.module\\.(css|sass|scss)$',
],
Expand Down
17 changes: 17 additions & 0 deletions jestSetup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* globals jest */
/* eslint-disable import/no-extraneous-dependencies */
import 'core-js/stable'
import 'regenerator-runtime/runtime'
import crypto from 'crypto'
import { TextEncoder, TextDecoder } from 'util'

// Force warnings to fail Jest tests.
// https://github.com/facebook/jest/issues/6121#issuecomment-444269677
Expand All @@ -11,3 +14,17 @@ console.error = function (message, ...args) {
error.apply(console, args) // keep default behaviour
throw message instanceof Error ? message : new Error(message)
}

// Next.js polyfills fetch
global.fetch = jest.fn()

// https://github.com/jsdom/jsdom/issues/1612
Object.defineProperty(global.self, 'crypto', {
value: {
subtle: crypto.webcrypto.subtle,
},
})

// https://github.com/inrupt/solid-client-authn-js/issues/1676#issuecomment-917016646
global.TextEncoder = TextEncoder
global.TextDecoder = TextDecoder
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@
"@testing-library/react": "^13.3.0",
"@testing-library/react-hooks": "^8.0.1",
"@types/cookies": "^0.7.7",
"@types/hoist-non-react-statics": "^3.3.1",
"@types/jest": "^28",
"@types/keygrip": "^1.0.2",
"@types/set-cookie-parser": "^2.4.2",
"@typescript-eslint/eslint-plugin": "^5.30.7",
"@typescript-eslint/parser": "^5.30.7",
"babel-jest": "^28.1.3",
Expand All @@ -67,7 +70,7 @@
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"firebase": "^9.9.1",
"firebase-admin": "^11.0.0",
"firebase-admin": "11.0.0",
"jest": "^28.1.3",
"jest-environment-jsdom": "^28.1.3",
"jscodeshift": "^0.14.0",
Expand All @@ -86,6 +89,7 @@
"source-map-loader": "^4.0.1",
"ts-jest": "^28",
"ts-loader": "^9.4.3",
"tsconfig-paths-webpack-plugin": "^4.0.1",
"typescript": "^4.7.4",
"webpack": "^5.73.0",
"webpack-bundle-analyzer": "^4.5.0",
Expand Down
11 changes: 0 additions & 11 deletions src/AuthAction.js

This file was deleted.

12 changes: 12 additions & 0 deletions src/AuthAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Different behaviors when the user's auth status is pending
// or mismatches the page requirements.

enum AuthAction {
RENDER = 'render',
SHOW_LOADER = 'showLoader',
RETURN_NULL = 'returnNull',
REDIRECT_TO_LOGIN = 'redirectToLogin',
REDIRECT_TO_APP = 'redirectToApp',
}

export default AuthAction
11 changes: 0 additions & 11 deletions src/__mocks__/config.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/__mocks__/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export {}

let config: any

const mock: any = jest.createMockFromModule('../config')

mock.setConfig = jest.fn((newConfig) => {
config = newConfig
})

mock.getConfig = jest.fn(() => config)

module.exports = mock
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export {}

const mockUseFirebaseUser = jest.fn(() => ({
user: undefined,
claims: {},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
describe('index.js: AuthAction', () => {
// eslint-disable-next-line jest/no-export
export {}

describe('index.ts: AuthAction', () => {
it('defines the expected constants', () => {
expect.assertions(1)
const AuthAction = require('src/AuthAction').default
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { setConfig } from 'src/config'
import { ConfigInput } from 'src/configTypes'
import createMockConfig from 'src/testHelpers/createMockConfig'

jest.mock('src/config')

beforeEach(() => {
const mockConfig = createMockConfig()
const mockConfig = createMockConfig() as ConfigInput
setConfig({
...mockConfig,
cookies: {
Expand Down
File renamed without changes.
Loading

0 comments on commit 06ff195

Please sign in to comment.