Skip to content

Commit

Permalink
cleaning up unit test and jsdom setup
Browse files Browse the repository at this point in the history
  • Loading branch information
krpeacock committed Aug 5, 2024
1 parent bf3914e commit 20a3dc5
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
16 changes: 1 addition & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions packages/use-auth-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
build_info.json
node_modules/
dist/
**/*.js
**/*.js.map
**/*.d.ts

# generated docs
/docs/reference

# Cannot ignore .d.ts files in types/
!types/**/*.d.ts

# Cannot ignore setup files for webpack and jest, which are still JavaScript.
!webpack.config.js
!jest.config.js
!test-setup.js
11 changes: 11 additions & 0 deletions packages/use-auth-client/FixJSDOMEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import JSDOMEnvironment from 'jest-environment-jsdom';

// https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
export default class FixJSDOMEnvironment extends JSDOMEnvironment {
constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
super(...args);

// FIXME https://github.com/jsdom/jsdom/issues/3363
this.global.structuredClone = structuredClone;
}
}
2 changes: 1 addition & 1 deletion packages/use-auth-client/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
testEnvironment: './FixJSDOMEnvironment.ts',
roots: ['<rootDir>/test'],
setupFilesAfterEnv: ['<rootDir>/setupTests.ts'],
};
2 changes: 1 addition & 1 deletion packages/use-auth-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@babel/preset-typescript": "^7.24.7",
"@testing-library/jest-dom": "^6.4.8",
"@testing-library/react": "^16.0.0",
"@types/react": "16.8",
"@types/react": ">16.8",
"babel": "^6.23.0",
"babel-jest": "^29.7.0",
"fake-indexeddb": "^6.0.0",
Expand Down
13 changes: 13 additions & 0 deletions packages/use-auth-client/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@ import 'fake-indexeddb/auto';
import { TextEncoder } from 'util';
import '@testing-library/jest-dom';

import { Crypto } from '@peculiar/webcrypto';
const crypto = new Crypto();

Object.defineProperty(globalThis, 'crypto', {
value: crypto,
});

Object.defineProperty(global, 'console', {
writable: true,
value: { ...global.console, log: jest.fn(), warn: jest.fn(), error: jest.fn() },
});


global.TextEncoder = TextEncoder;
9 changes: 6 additions & 3 deletions packages/use-auth-client/test/use-auth-client.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { describe, it } from '@jest/globals';
describe('useAuthClient', () => {
it('should return an authClient object with the expected properties', async () => {
interface Props {
children: React.ReactNode;
children?: React.ReactNode;
}
const Component: React.FC<Props> = () => {
const { isAuthenticated, login, logout } = useAuthClient();
Expand All @@ -21,7 +21,10 @@ describe('useAuthClient', () => {
// disable typescript
render(<Component />);

const isAuthenticated = screen.getByTestId('isAuthenticated');
expect(isAuthenticated).toHaveTextContent('false');
(
expect(screen.getByTestId('isAuthenticated')) as unknown as {
toHaveTextContent: (str: string) => boolean;
}
).toHaveTextContent('false');
});
});

0 comments on commit 20a3dc5

Please sign in to comment.