-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure tests do not log warning or errors
This commit increases strictnes of tests by failing on tests (even though they pass) if `console.warn` or `console.error` is used. This is used to fix warning outputs from Vue, cleaning up test output and preventing potential issues with tests. This commit fixes all of the failing tests, including refactoring in code to make them more testable through injecting Vue lifecycle hook function stubs. This removes `shallowMount`ing done on places, improving the speed of executing unit tests. It also reduces complexity and increases maintainability by removing `@vue/test-utils` dependency for these tests. Changes: - Register global hook for all tests to fail if console.error or console.warn is being used. - Fix all issues with failing tests. - Create test helper function for running code in a wrapper component to run code in reliable/unified way to surpress Vue warnings about code not running inside `setup`.
- Loading branch information
1 parent
a650558
commit ae0165f
Showing
26 changed files
with
359 additions
and
205 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
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
8 changes: 8 additions & 0 deletions
8
src/presentation/components/Shared/Hooks/Common/LifecycleHook.ts
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,8 @@ | ||
/* | ||
These types are used to abstract Vue Lifecycle injection APIs | ||
(e.g., onBeforeMount, onUnmount) for better testability. | ||
*/ | ||
|
||
export type LifecycleHook = (callback: LifecycleHookCallback) => void; | ||
|
||
export type LifecycleHookCallback = () => void; |
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
4 changes: 3 additions & 1 deletion
4
src/presentation/components/Shared/Hooks/UseAutoUnsubscribedEvents.ts
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
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,27 @@ | ||
import { shallowMount, type ComponentMountingOptions } from '@vue/test-utils'; | ||
import { defineComponent } from 'vue'; | ||
|
||
type MountOptions = ComponentMountingOptions<unknown>; | ||
|
||
/** | ||
* A test helper utility that provides a component `setup()` context. | ||
* This function allows running code that depends on Vue lifecycle hooks, | ||
* such as `onMounted`, within a component's `setup` function. | ||
*/ | ||
export function executeInComponentSetupContext(options: { | ||
readonly setupCallback: () => void; | ||
readonly disableAutoUnmount?: boolean; | ||
readonly mountOptions?: MountOptions, | ||
}): ReturnType<typeof shallowMount> { | ||
const componentWrapper = shallowMount(defineComponent({ | ||
setup() { | ||
options.setupCallback(); | ||
}, | ||
// Component requires a template or render function | ||
template: '<div>Test Component: setup context</div>', | ||
}), options.mountOptions); | ||
if (!options.disableAutoUnmount) { | ||
componentWrapper.unmount(); // Ensure cleanup of callback tasks | ||
} | ||
return componentWrapper; | ||
} |
File renamed without changes.
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 @@ | ||
import { | ||
beforeEach, afterEach, vi, expect, | ||
} from 'vitest'; | ||
import type { FunctionKeys } from '@/TypeHelpers'; | ||
|
||
export function failTestOnConsoleError() { | ||
const consoleMethodsToCheck: readonly FunctionKeys<Console>[] = [ | ||
'warn', | ||
'error', | ||
]; | ||
|
||
beforeEach(() => { | ||
consoleMethodsToCheck.forEach((methodName) => { | ||
vi.spyOn(console, methodName).mockClear(); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
consoleMethodsToCheck.forEach((methodName) => { | ||
expect(console[methodName]).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
} |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import { afterEach } from 'vitest'; | ||
import { enableAutoUnmount } from '@vue/test-utils'; | ||
import { polyfillBlob } from './BlobPolyfill'; | ||
import { failTestOnConsoleError } from './FailTestOnConsoleError'; | ||
|
||
enableAutoUnmount(afterEach); | ||
polyfillBlob(); | ||
failTestOnConsoleError(); |
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
Oops, something went wrong.