You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Substitutes all imported modules from provided `path` with another module. You can use configured Vite aliases inside a path. The call to `vi.mock` is hoisted, so it doesn't matter where you call it. It will always be executed before all imports. If you need to reference some variables outside of its scope, you can define them inside [`vi.hoisted`](#vi-hoisted) and reference them inside `vi.mock`.
Since 2.0.0, Vitest supports a module promise instead of a string in `vi.mock` method for better IDE support (when file is moved, path will be updated, `importOriginal` also inherits the type automatically).
const mod =awaitimportOriginal() // type is inferred
73
+
return {
74
+
...mod,
75
+
// replace some exports
76
+
namedExport: vi.fn(),
77
+
}
78
+
})
79
+
```
80
+
81
+
Under the hood, Vitest still operates on a string and not a module object.
82
+
67
83
::: warning
68
84
`vi.mock` is hoisted (in other words, _moved_) to **top of the file**. It means that whenever you write it (be it inside `beforeEach` or `test`), it will actually be called before that.
Copy file name to clipboardexpand all lines: test/core/test/injector-mock.test.ts
+98
Original file line number
Diff line number
Diff line change
@@ -1201,6 +1201,104 @@ await vi
1201
1201
1234;"
1202
1202
`)
1203
1203
})
1204
+
1205
+
test('handles dynamic import as the first argument',()=>{
1206
+
expect(
1207
+
hoistSimpleCode(`
1208
+
vi.mock(import('./path'))
1209
+
vi.mock(import(somePath))
1210
+
vi.mock(import(\`./path\`))
1211
+
1212
+
vi.mock(import('./path'));
1213
+
vi.mock(import(somePath));
1214
+
vi.mock(import(\`./path\`));
1215
+
1216
+
vi.mock(await import('./path'))
1217
+
vi.mock(await import(somePath))
1218
+
vi.mock(await import(\`./path\`))
1219
+
1220
+
vi.mock(await import('./path'));
1221
+
vi.mock(await import(somePath));
1222
+
vi.mock(await import(\`./path\`));
1223
+
1224
+
vi.mock(import('./path'), () => {})
1225
+
vi.mock(import(somePath), () => {})
1226
+
vi.mock(import(\`./path\`), () => {})
1227
+
1228
+
vi.mock(await import('./path'), () => {})
1229
+
vi.mock(await import(somePath), () => {})
1230
+
vi.mock(await import(\`./path\`), () => {})
1231
+
1232
+
vi.mock(import('./path'), () => {});
1233
+
vi.mock(import(somePath), () => {});
1234
+
vi.mock(import(\`./path\`), () => {});
1235
+
1236
+
vi.mock(await import('./path'), () => {});
1237
+
vi.mock(await import(somePath), () => {});
1238
+
vi.mock(await import(\`./path\`), () => {});
1239
+
`),
1240
+
).toMatchInlineSnapshot(`
1241
+
"if (typeof globalThis.vi === "undefined" && typeof globalThis.vitest === "undefined") { throw new Error("There are some problems in resolving the mocks API.\\nYou may encounter this issue when importing the mocks API from another module other than 'vitest'.\\nTo fix this issue you can either:\\n- import the mocks API directly from 'vitest'\\n- enable the 'globals' options") }
1242
+
vi.mock('./path')
1243
+
vi.mock(somePath)
1244
+
vi.mock(\`./path\`)
1245
+
vi.mock('./path');
1246
+
vi.mock(somePath);
1247
+
vi.mock(\`./path\`);
1248
+
vi.mock('./path')
1249
+
vi.mock(somePath)
1250
+
vi.mock(\`./path\`)
1251
+
vi.mock('./path');
1252
+
vi.mock(somePath);
1253
+
vi.mock(\`./path\`);
1254
+
vi.mock('./path', () => {})
1255
+
vi.mock(somePath, () => {})
1256
+
vi.mock(\`./path\`, () => {})
1257
+
vi.mock('./path', () => {})
1258
+
vi.mock(somePath, () => {})
1259
+
vi.mock(\`./path\`, () => {})
1260
+
vi.mock('./path', () => {});
1261
+
vi.mock(somePath, () => {});
1262
+
vi.mock(\`./path\`, () => {});
1263
+
vi.mock('./path', () => {});
1264
+
vi.mock(somePath, () => {});
1265
+
vi.mock(\`./path\`, () => {});"
1266
+
`)
1267
+
})
1268
+
1269
+
test.only('handles import in vi.do* methods',()=>{
1270
+
expect(
1271
+
hoistSimpleCode(`
1272
+
vi.doMock(import('./path'))
1273
+
vi.doMock(import(\`./path\`))
1274
+
vi.doMock(import('./path'));
1275
+
1276
+
beforeEach(() => {
1277
+
vi.doUnmock(import('./path'))
1278
+
vi.doMock(import('./path'))
1279
+
})
1280
+
1281
+
test('test', async () => {
1282
+
vi.doMock(import(dynamicName))
1283
+
await import(dynamicName)
1284
+
})
1285
+
`),
1286
+
).toMatchInlineSnapshot(`
1287
+
"vi.doMock('./path')
1288
+
vi.doMock(\`./path\`)
1289
+
vi.doMock('./path');
1290
+
1291
+
beforeEach(() => {
1292
+
vi.doUnmock('./path')
1293
+
vi.doMock('./path')
1294
+
})
1295
+
1296
+
test('test', async () => {
1297
+
vi.doMock(dynamicName)
1298
+
await import(dynamicName)
1299
+
})"
1300
+
`)
1301
+
})
1204
1302
})
1205
1303
1206
1304
describe('throws an error when nodes are incompatible',()=>{
0 commit comments