Skip to content

Commit 7fe2a6a

Browse files
committed
fix: clarify the warning
1 parent 96e9be8 commit 7fe2a6a

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

packages/mocker/src/node/hoistMocksPlugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,9 +520,10 @@ export function hoistMocks(
520520

521521
for (const invalidNode of hoistedNodes) {
522522
console.warn(
523-
`Warning: The ${getNodeName(getNodeCall(invalidNode))} call in '${id}' is defined outside of the top level of the module. `
524-
+ `This gives the wrong impression of when it is executed - all hoisted methods are executed before imports, not in the scope they were defined in. `
525-
+ `Please move it to the top level of the module to avoid confusion. In future versions, this will be an error.`,
523+
`Warning: A ${getNodeName(getNodeCall(invalidNode))} call in "${id}" is not at the top level of the module. `
524+
+ `Although it appears nested, it will be hoisted and executed before any tests run. `
525+
+ `Move it to the top level to reflect its actual execution order. This will become an error in a future version.\n`
526+
+ `See: https://vitest.dev/guide/mocking/modules#how-it-works`,
526527
)
527528
}
528529
}

test/core/test/__snapshots__/injector-mock.test.ts.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`throws an error when nodes are incompatible > correctly throws an error if awaited assigned vi.hoisted is called inside vi.mock 1`] = `"Cannot call vi.hoisted() inside vi.mock(): both methods are hoisted to the top of the file and not actually called inside each other."`;
3+
exports[`throws an error when nodes are incompatible > correctly throws an error if awaited assigned vi.hoisted is called inside vi.mock 1`] = `"Cannot call vi.hoisted() inside vi.mock('./mocked'): both methods are hoisted to the top of the file and not actually called inside each other."`;
44

55
exports[`throws an error when nodes are incompatible > correctly throws an error if awaited assigned vi.hoisted is called inside vi.mock 2`] = `
66
" 2|
@@ -11,7 +11,7 @@ exports[`throws an error when nodes are incompatible > correctly throws an error
1111
6| "
1212
`;
1313

14-
exports[`throws an error when nodes are incompatible > correctly throws an error if awaited vi.hoisted is called inside vi.mock 1`] = `"Cannot call vi.hoisted() inside vi.mock(): both methods are hoisted to the top of the file and not actually called inside each other."`;
14+
exports[`throws an error when nodes are incompatible > correctly throws an error if awaited vi.hoisted is called inside vi.mock 1`] = `"Cannot call vi.hoisted() inside vi.mock('./mocked'): both methods are hoisted to the top of the file and not actually called inside each other."`;
1515

1616
exports[`throws an error when nodes are incompatible > correctly throws an error if awaited vi.hoisted is called inside vi.mock 2`] = `
1717
" 2|
@@ -44,7 +44,7 @@ exports[`throws an error when nodes are incompatible > correctly throws an error
4444
5| })"
4545
`;
4646

47-
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.hoisted is called inside vi.mock 1`] = `"Cannot call vi.hoisted() inside vi.mock(): both methods are hoisted to the top of the file and not actually called inside each other."`;
47+
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.hoisted is called inside vi.mock 1`] = `"Cannot call vi.hoisted() inside vi.mock('./mocked'): both methods are hoisted to the top of the file and not actually called inside each other."`;
4848

4949
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.hoisted is called inside vi.mock 2`] = `
5050
" 2|
@@ -77,7 +77,7 @@ exports[`throws an error when nodes are incompatible > correctly throws an error
7777
5| })"
7878
`;
7979

80-
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock inside vi.hoisted 1`] = `"Cannot call vi.mock() inside vi.hoisted(): both methods are hoisted to the top of the file and not actually called inside each other."`;
80+
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock inside vi.hoisted 1`] = `"Cannot call vi.mock('./mocked') inside vi.hoisted(): both methods are hoisted to the top of the file and not actually called inside each other."`;
8181

8282
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock inside vi.hoisted 2`] = `
8383
" 2|
@@ -88,7 +88,7 @@ exports[`throws an error when nodes are incompatible > correctly throws an error
8888
6| "
8989
`;
9090

91-
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock is called inside assigned awaited vi.hoisted 1`] = `"Cannot call vi.mock() inside vi.hoisted(): both methods are hoisted to the top of the file and not actually called inside each other."`;
91+
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock is called inside assigned awaited vi.hoisted 1`] = `"Cannot call vi.mock('./mocked') inside vi.hoisted(): both methods are hoisted to the top of the file and not actually called inside each other."`;
9292

9393
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock is called inside assigned awaited vi.hoisted 2`] = `
9494
" 2|
@@ -99,7 +99,7 @@ exports[`throws an error when nodes are incompatible > correctly throws an error
9999
6| "
100100
`;
101101

102-
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock is called inside assigned vi.hoisted 1`] = `"Cannot call vi.mock() inside vi.hoisted(): both methods are hoisted to the top of the file and not actually called inside each other."`;
102+
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock is called inside assigned vi.hoisted 1`] = `"Cannot call vi.mock('./mocked') inside vi.hoisted(): both methods are hoisted to the top of the file and not actually called inside each other."`;
103103

104104
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock is called inside assigned vi.hoisted 2`] = `
105105
" 2|
@@ -110,7 +110,7 @@ exports[`throws an error when nodes are incompatible > correctly throws an error
110110
6| "
111111
`;
112112

113-
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock is called inside awaited vi.hoisted 1`] = `"Cannot call vi.mock() inside vi.hoisted(): both methods are hoisted to the top of the file and not actually called inside each other."`;
113+
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock is called inside awaited vi.hoisted 1`] = `"Cannot call vi.mock('./mocked') inside vi.hoisted(): both methods are hoisted to the top of the file and not actually called inside each other."`;
114114

115115
exports[`throws an error when nodes are incompatible > correctly throws an error if vi.mock is called inside awaited vi.hoisted 2`] = `
116116
" 2|

test/core/test/injector-mock.test.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,7 @@ console.log(path.mocked);
12961296
`)
12971297
})
12981298

1299-
test.only('correctly hoists when import.meta is used', () => {
1299+
test('correctly hoists when import.meta is used', () => {
13001300
expect(hoistSimpleCode(`
13011301
import { calc } from './calc'
13021302
function sum(a, b) {
@@ -1572,25 +1572,32 @@ describe('some suite', () => {
15721572
[MockFunction warn] {
15731573
"calls": [
15741574
[
1575-
"Warning: The vi.mock('./hello-world-2') call in '/test.js' is defined outside of the top level of the module. This gives the wrong impression of when it is executed - all hoisted methods are executed before imports, not in the scope they were defined in. Please move it to the top level of the module to avoid confusion. In future versions, this will be an error.",
1575+
"Warning: A vi.mock('./hello-world-2') call in "/test.js" is not at the top level of the module. Although it appears nested, it will be hoisted and executed before any tests run. Move it to the top level to reflect its actual execution order. This will become an error in a future version.
1576+
See: https://vitest.dev/guide/mocking/modules#how-it-works",
15761577
],
15771578
[
1578-
"Warning: The vi.mock('./hello-world-3') call in '/test.js' is defined outside of the top level of the module. This gives the wrong impression of when it is executed - all hoisted methods are executed before imports, not in the scope they were defined in. Please move it to the top level of the module to avoid confusion. In future versions, this will be an error.",
1579+
"Warning: A vi.mock('./hello-world-3') call in "/test.js" is not at the top level of the module. Although it appears nested, it will be hoisted and executed before any tests run. Move it to the top level to reflect its actual execution order. This will become an error in a future version.
1580+
See: https://vitest.dev/guide/mocking/modules#how-it-works",
15791581
],
15801582
[
1581-
"Warning: The vi.mock('./hello-world-4') call in '/test.js' is defined outside of the top level of the module. This gives the wrong impression of when it is executed - all hoisted methods are executed before imports, not in the scope they were defined in. Please move it to the top level of the module to avoid confusion. In future versions, this will be an error.",
1583+
"Warning: A vi.mock('./hello-world-4') call in "/test.js" is not at the top level of the module. Although it appears nested, it will be hoisted and executed before any tests run. Move it to the top level to reflect its actual execution order. This will become an error in a future version.
1584+
See: https://vitest.dev/guide/mocking/modules#how-it-works",
15821585
],
15831586
[
1584-
"Warning: The vi.hoisted() call in '/test.js' is defined outside of the top level of the module. This gives the wrong impression of when it is executed - all hoisted methods are executed before imports, not in the scope they were defined in. Please move it to the top level of the module to avoid confusion. In future versions, this will be an error.",
1587+
"Warning: A vi.hoisted() call in "/test.js" is not at the top level of the module. Although it appears nested, it will be hoisted and executed before any tests run. Move it to the top level to reflect its actual execution order. This will become an error in a future version.
1588+
See: https://vitest.dev/guide/mocking/modules#how-it-works",
15851589
],
15861590
[
1587-
"Warning: The vi.mock(import('./hello-world-5')) call in '/test.js' is defined outside of the top level of the module. This gives the wrong impression of when it is executed - all hoisted methods are executed before imports, not in the scope they were defined in. Please move it to the top level of the module to avoid confusion. In future versions, this will be an error.",
1591+
"Warning: A vi.mock(import('./hello-world-5')) call in "/test.js" is not at the top level of the module. Although it appears nested, it will be hoisted and executed before any tests run. Move it to the top level to reflect its actual execution order. This will become an error in a future version.
1592+
See: https://vitest.dev/guide/mocking/modules#how-it-works",
15881593
],
15891594
[
1590-
"Warning: The vi.hoisted() call in '/test.js' is defined outside of the top level of the module. This gives the wrong impression of when it is executed - all hoisted methods are executed before imports, not in the scope they were defined in. Please move it to the top level of the module to avoid confusion. In future versions, this will be an error.",
1595+
"Warning: A vi.hoisted() call in "/test.js" is not at the top level of the module. Although it appears nested, it will be hoisted and executed before any tests run. Move it to the top level to reflect its actual execution order. This will become an error in a future version.
1596+
See: https://vitest.dev/guide/mocking/modules#how-it-works",
15911597
],
15921598
[
1593-
"Warning: The vi.mock('./hello-world-6') call in '/test.js' is defined outside of the top level of the module. This gives the wrong impression of when it is executed - all hoisted methods are executed before imports, not in the scope they were defined in. Please move it to the top level of the module to avoid confusion. In future versions, this will be an error.",
1599+
"Warning: A vi.mock('./hello-world-6') call in "/test.js" is not at the top level of the module. Although it appears nested, it will be hoisted and executed before any tests run. Move it to the top level to reflect its actual execution order. This will become an error in a future version.
1600+
See: https://vitest.dev/guide/mocking/modules#how-it-works",
15941601
],
15951602
],
15961603
"results": [

0 commit comments

Comments
 (0)