-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(warn): detect global context on the server side #2983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+120
−86
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
833c569
fix: never use global context on the server side
ivansky a0f64fe
Improve error message
ivansky 922dfa8
Update rootStore.ts
ivansky 4bcc07a
refactor: review
posva 9b0bab5
Update packages/pinia/src/rootStore.ts [skip ci]
posva 11be687
Update packages/pinia/__tests__/ssr.spec.ts [skip ci]
posva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,131 +1,129 @@ | ||
| // https://github.com/posva/jest-mock-warn/blob/master/src/index.js | ||
|
|
||
| import type { MockInstance } from 'vitest' | ||
| import { afterEach, beforeEach, expect, vi } from 'vitest' | ||
|
|
||
| interface CustomMatchers<R = unknown> { | ||
| toHaveBeenWarned: () => R | ||
| toHaveBeenWarnedLast: () => R | ||
| toHaveBeenWarnedTimes: (n: number) => R | ||
| toHaveBeenErrored: () => R | ||
| toHaveBeenErroredLast: () => R | ||
| toHaveBeenErroredTimes: (n: number) => R | ||
| } | ||
|
|
||
| declare module 'vitest' { | ||
| interface Assertion<T = any> extends CustomMatchers<T> {} | ||
| interface AsymmetricMatchersContaining extends CustomMatchers {} | ||
| } | ||
|
|
||
| export function mockWarn() { | ||
| let warn: MockInstance<(typeof console)['log']> | ||
| function createMockConsoleMethod(method: 'warn' | 'error') { | ||
| let mockInstance: MockInstance<(typeof console)[typeof method]> | ||
| const asserted = new Map<string, string | RegExp>() | ||
|
|
||
| expect.extend({ | ||
| toHaveBeenWarned(received: string | RegExp) { | ||
| [`toHaveBeen${method.charAt(0).toUpperCase() + method.slice(1)}ed`]( | ||
| received: string | RegExp | ||
| ) { | ||
| asserted.set(received.toString(), received) | ||
| const passed = warn.mock.calls.some((args) => | ||
| const passed = mockInstance.mock.calls.some((args) => | ||
| typeof received === 'string' | ||
| ? args[0].includes(received) | ||
| : received.test(args[0]) | ||
| ? String(args[0]).includes(received) | ||
| : received.test(String(args[0])) | ||
| ) | ||
| if (passed) { | ||
| return { | ||
| pass: true, | ||
| message: () => `expected "${received}" not to have been warned.`, | ||
| } | ||
| } else { | ||
| const msgs = warn.mock.calls.map((args) => args[0]).join('\n - ') | ||
| return { | ||
| pass: false, | ||
| message: () => | ||
| `expected "${received}" to have been warned.\n\nActual messages:\n\n - ${msgs}`, | ||
| } | ||
| } | ||
|
|
||
| return passed | ||
| ? { | ||
| pass: true, | ||
| message: () => | ||
| `expected "${received}" not to have been ${method}ed.`, | ||
| } | ||
| : { | ||
| pass: false, | ||
| message: () => `expected "${received}" to have been ${method}ed.`, | ||
| } | ||
| }, | ||
|
|
||
| toHaveBeenWarnedLast(received: string | RegExp) { | ||
| [`toHaveBeen${method.charAt(0).toUpperCase() + method.slice(1)}edLast`]( | ||
| received: string | RegExp | ||
| ) { | ||
| asserted.set(received.toString(), received) | ||
| const lastCall = warn.mock.calls[warn.mock.calls.length - 1][0] | ||
| const lastCall = String(mockInstance.mock.calls.at(-1)?.[0]) | ||
| const passed = | ||
| typeof received === 'string' | ||
| ? lastCall.includes(received) | ||
| ? lastCall?.includes(received) | ||
| : received.test(lastCall) | ||
| if (passed) { | ||
| return { | ||
| pass: true, | ||
| message: () => `expected "${received}" not to have been warned last.`, | ||
| } | ||
| } else { | ||
| const msgs = warn.mock.calls.map((args) => args[0]).join('\n - ') | ||
| return { | ||
| pass: false, | ||
| message: () => | ||
| `expected "${received}" to have been warned last.\n\nActual messages:\n\n - ${msgs}`, | ||
| } | ||
| } | ||
|
|
||
| return passed | ||
| ? { | ||
| pass: true, | ||
| message: () => | ||
| `expected "${received}" not to have been ${method}ed last.`, | ||
| } | ||
| : { | ||
| pass: false, | ||
| message: () => | ||
| `expected "${received}" to have been ${method}ed last.`, | ||
| } | ||
| }, | ||
|
|
||
| toHaveBeenWarnedTimes(received: string | RegExp, n: number) { | ||
| [`toHaveBeen${method.charAt(0).toUpperCase() + method.slice(1)}edTimes`]( | ||
| received: string | RegExp, | ||
| n: number | ||
| ) { | ||
| asserted.set(received.toString(), received) | ||
| let found = 0 | ||
| warn.mock.calls.forEach((args) => { | ||
| const isFound = | ||
| typeof received === 'string' | ||
| ? args[0].includes(received) | ||
| : received.test(args[0]) | ||
| if (isFound) { | ||
| found++ | ||
| } | ||
| }) | ||
| const count = mockInstance.mock.calls.filter((args) => | ||
| typeof received === 'string' | ||
| ? String(args[0]).includes(received) | ||
| : received.test(String(args[0])) | ||
| ).length | ||
|
|
||
| if (found === n) { | ||
| return { | ||
| pass: true, | ||
| message: () => | ||
| `expected "${received}" to have been warned ${n} times.`, | ||
| } | ||
| } else { | ||
| return { | ||
| pass: false, | ||
| message: () => | ||
| `expected "${received}" to have been warned ${n} times but got ${found}.`, | ||
| } | ||
| } | ||
| return count === n | ||
| ? { | ||
| pass: true, | ||
| message: () => | ||
| `expected "${received}" to have been ${method}ed ${n} times.`, | ||
| } | ||
| : { | ||
| pass: false, | ||
| message: () => | ||
| `expected "${received}" to have been ${method}ed ${n} times but got ${count}.`, | ||
| } | ||
| }, | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| asserted.clear() | ||
| warn = vi.spyOn(console, 'warn') | ||
| warn.mockImplementation(() => {}) | ||
| mockInstance = vi.spyOn(console, method).mockImplementation(() => {}) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| const assertedArray = Array.from(asserted) | ||
| const nonAssertedWarnings = warn.mock.calls | ||
| .map((args) => args[0]) | ||
| .filter((received) => { | ||
| return !assertedArray.some(([_key, assertedMsg]) => { | ||
| return typeof assertedMsg === 'string' | ||
| ? received.includes(assertedMsg) | ||
| : assertedMsg.test(received) | ||
| }) | ||
| }) | ||
| warn.mockRestore() | ||
| if (nonAssertedWarnings.length) { | ||
| nonAssertedWarnings.forEach((warning) => { | ||
| console.warn(warning) | ||
| const unassertedLogs = mockInstance.mock.calls | ||
| .map((args) => String(args[0])) | ||
| .filter( | ||
| (msg) => | ||
| !assertedArray.some(([_key, assertedMsg]) => | ||
| typeof assertedMsg === 'string' | ||
| ? msg.includes(assertedMsg) | ||
| : assertedMsg.test(msg) | ||
| ) | ||
| ) | ||
|
|
||
| mockInstance.mockRestore() | ||
|
|
||
| if (unassertedLogs.length) { | ||
| unassertedLogs.forEach((msg) => console[method](msg)) | ||
| throw new Error(`Test case threw unexpected ${method}s.`, { | ||
| cause: unassertedLogs, | ||
| }) | ||
| throw new Error(`test case threw unexpected warnings.`) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| interface CustomMatchers<R = unknown> { | ||
| toHaveBeenWarned: () => R | ||
| toHaveBeenWarnedLast: () => R | ||
| toHaveBeenWarnedTimes: (n: number) => R | ||
| export function mockWarn() { | ||
| createMockConsoleMethod('warn') | ||
| } | ||
|
|
||
| declare module 'vitest' { | ||
| interface Assertion<T = any> extends CustomMatchers<T> {} | ||
| interface AsymmetricMatchersContaining extends CustomMatchers {} | ||
| export function mockConsoleError() { | ||
| createMockConsoleMethod('error') | ||
| } |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.