From f2f0bc4254382926b7269e27b6106e40fdb7a6a7 Mon Sep 17 00:00:00 2001 From: BilligsterUser Date: Fri, 9 Jun 2023 21:08:47 +0200 Subject: [PATCH] update jest config --- config/jest.config.ts | 8 ++++---- test/components/Balance.test.tsx | 35 ++++++++------------------------ test/components/Txt.test.tsx | 14 ++++++------- test/setup.ts | 18 ++++++++++++---- 4 files changed, 34 insertions(+), 41 deletions(-) diff --git a/config/jest.config.ts b/config/jest.config.ts index 422aa45c..05285d4c 100644 --- a/config/jest.config.ts +++ b/config/jest.config.ts @@ -20,7 +20,7 @@ export default (): JestConfigWithTsJest => ({ }] }, resolver: 'ts-jest-resolver', - testRegex: 'test/.*\\.(test|spec)?\\.(ts)$', + testRegex: 'test/.*\\.(test|spec)?\\.(ts|tsx)$', // moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], preset: 'jest-expo', transformIgnorePatterns: [ @@ -36,7 +36,7 @@ export default (): JestConfigWithTsJest => ({ }, collectCoverage: false, collectCoverageFrom: [ - '**/src/**/*.ts', + '**/src/**/*.{ts,tsx}', '!**/coverage/**', '!**/report/**', '!**/assets/**', @@ -49,9 +49,9 @@ export default (): JestConfigWithTsJest => ({ '!app.config.js', '!metro.config.js', '!react-native.config.js', - '!**/src/styles/**', + // '!**/src/styles/**', '!**/src/consts/**', - '!**/src/components/**', + // '!**/src/components/**', '!**/src/AppEntry.ts', '!**/src/shim.ts', // '!**/src/storage/store/AsyncStore.ts', diff --git a/test/components/Balance.test.tsx b/test/components/Balance.test.tsx index a4ab7b2f..05ce9d16 100644 --- a/test/components/Balance.test.tsx +++ b/test/components/Balance.test.tsx @@ -1,39 +1,22 @@ import Balance from '@comps/Balance' -import { render, fireEvent } from '@testing-library/react-native' -import { Locale } from 'expo-localization' +import { initDb } from '@src/storage/db' +import { fireEvent,render,screen } from '@testing-library/react-native' + -// Mock the expo-sqlite openDatabase method -jest.mock('expo-sqlite', () => ({ - openDatabase: () => ({ - transaction: (callback: (tx: any) => void) => { - const mockExecuteSql = jest.fn() - const mockTx = { executeSql: mockExecuteSql } - callback(mockTx) - }, - }), -})) -// Mock the expo-localization -jest.mock('expo-localization', () => { - return { - locale: 'de', - getLgetLocales: () => { - const arr: Locale[] = [] - return arr - } - } -}) describe('Basic test of the Txt.tsx component', () => { + // eslint-disable-next-line no-return-await + beforeAll(async () => await initDb()) // Clear all mock function calls before each test beforeEach(() => jest.clearAllMocks()) // Start tests it('renders the expected string', () => { - const { getByText } = render() - const textElement = getByText('69') + render() + const textElement = screen.getByText('69') expect(textElement).toBeDefined() }) it('updates the balance format state on press', () => { - const { getByText } = render() - const touchableElement = getByText('69') + render() + const touchableElement = screen.getByText('69') // Simulate press event fireEvent.press(touchableElement) expect(touchableElement.props.children).toBe('0.00000069') diff --git a/test/components/Txt.test.tsx b/test/components/Txt.test.tsx index bced5d24..f7a26047 100644 --- a/test/components/Txt.test.tsx +++ b/test/components/Txt.test.tsx @@ -1,25 +1,25 @@ import Txt from '@comps/Txt' -import { render } from '@testing-library/react-native' +import { render, screen } from '@testing-library/react-native' describe('Basic test of the Txt.tsx component', () => { it('renders the expected string', () => { - const { getByText } = render() - const textElement = getByText('Hello World!') + render() + const textElement = screen.getByText('Hello World!') expect(textElement).toBeDefined() }) it('has the global styles of a text component', () => { - const { getByText } = render() - const textElement = getByText('Hello World!') + render() + const textElement = screen.getByText('Hello World!') expect(textElement.props.style[0]).toStrictEqual({ fontSize: 16, color: '#656565' }) }) it('has an additional specific style applied', () => { - const { getByText } = render( + render( ) - const textElement = getByText('Hello World!') + const textElement = screen.getByText('Hello World!') expect(textElement.props.style[1].marginTop).toBe(10) }) }) diff --git a/test/setup.ts b/test/setup.ts index 7803b789..28480924 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -1,9 +1,13 @@ // we always make sure 'react-native' gets included first -// import * as ReactNative from "react-native" +// eslint-disable-next-line simple-import-sort/imports +import * as ReactNative from 'react-native' + +import { Locale } from 'expo-localization' + import { getDatabase } from './wrapper/getTestDb' // libraries to mock -// jest.doMock("react-native", () => { return ReactNative }) +jest.doMock('react-native', () => ReactNative) jest.mock('expo-sqlite', () => ({ get openDatabase() { return (_: string) => getDatabase(':memory:') @@ -13,9 +17,15 @@ jest.mock('expo-sqlite', () => ({ jest.mock('expo-constants', () => ({})) jest.mock('expo-secure-store', () => ({})) jest.mock('@bugsnag/expo', () => ({})) -jest.mock('expo-localization', () => ({})) +jest.mock('expo-localization', () => ({ + locale: 'de', + getLgetLocales: () => { + const arr: Locale[] = [] + return arr + } +})) jest.mock('reactotron-react-native', () => ({})) -jest.mock('react-native', () => ({})) +// jest.mock('react-native', () => ({})) // jest.mock('@consts', () => ({})) jest.mock('expo/config', () => ({}))