-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✅ Enable jest and react-testing-library for unit testing
Added a simple test for the Tile component and auth utility function. Page testing is problematic due to the i18n configuration, its do-able, just was taking too much time.
- Loading branch information
1 parent
d2cee28
commit f3260ff
Showing
13 changed files
with
1,937 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,5 +32,5 @@ jobs: | |
yarn install --frozen-lockfile | ||
yarn lint | ||
yarn build | ||
yarn test | ||
yarn test:ci | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const nextJest = require('next/jest') | ||
|
||
const createJestConfig = nextJest({ | ||
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment | ||
dir: './' | ||
}) | ||
|
||
// Add any custom config to be passed to Jest | ||
const customJestConfig = { | ||
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'], | ||
moduleNameMapper: { | ||
// Handle module aliases (this will be automatically configured for you soon) | ||
'^@/components/(.*)$': '<rootDir>/src/components/$1', | ||
|
||
'^@/pages/(.*)$': '<rootDir>/pages/$1', | ||
'^~/(.*)$': '<rootDir>/src/$1' | ||
}, | ||
testEnvironment: 'jest-environment-jsdom', | ||
testPathIgnorePatterns: ['__tests__/e2e', '__tests__/old_tests'] | ||
} | ||
|
||
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | ||
module.exports = createJestConfig(customJestConfig) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import '@testing-library/jest-dom/extend-expect' | ||
import { setConfig } from 'next/config' | ||
import config from './next.config' | ||
|
||
// Make sure you can use "publicRuntimeConfig" within tests. | ||
setConfig(config.publicRuntimeConfig) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import { noop } from '~/lib/test-helpers' | ||
import { Tile } from '../Tile' | ||
|
||
describe('Title', () => { | ||
it('renders a heading', () => { | ||
render(<Tile title='Some Text' onClick={noop} />) | ||
|
||
const heading = screen.getByRole('heading', { | ||
name: /Some Text/i | ||
}) | ||
|
||
expect(heading).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
export const noop = () => {} | ||
export const identity = <T>(t: T) => t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getAuthCredentialsHeaders } from '../auth' | ||
|
||
describe('auth', () => { | ||
it('returns a headers authorization object when passed a token', () => { | ||
const fakeToken = 'some-token-here' | ||
const result = getAuthCredentialsHeaders(fakeToken) | ||
|
||
expect(result).toHaveProperty('headers') | ||
expect(result.headers).toHaveProperty('Authorization', fakeToken) | ||
}) | ||
|
||
it('returns cookie auth when no token is provided', () => { | ||
const result = getAuthCredentialsHeaders() | ||
|
||
expect(result).toHaveProperty('withCredentials', true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.