Skip to content

Commit

Permalink
feat: add dynamicImportSettled utility function (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed May 29, 2022
1 parent 861b438 commit 1793937
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/api/index.md
Expand Up @@ -1585,6 +1585,10 @@ Vitest provides utility functions to help you out through it's **vi** helper. Yo

Removes all timers that are scheduled to run. These timers will never run in the future.

### vi.dynamicImportSettled

Wait for all imports to load. Useful, if you have a synchronous call that starts importing a module, that you cannot wait otherwise.

### vi.fn

- **Type:** `(fn: Function) => CallableMockInstance`
Expand Down
20 changes: 20 additions & 0 deletions examples/vue/components/AsAsync.vue
@@ -0,0 +1,20 @@
<script setup lang="ts">
import { defineAsyncComponent, ref } from 'vue'
const Hello = defineAsyncComponent({
loader: () => import('./Hello.vue'),
delay: 0,
})
const showHello = ref(false)
</script>

<template>
<button @click="showHello = true">
Show Hello
</button>
<div v-if="showHello">
<div>Async component</div>
<Hello :count="1" />
</div>
</template>
15 changes: 15 additions & 0 deletions examples/vue/test/as-async.test.ts
@@ -0,0 +1,15 @@
import { flushPromises, mount } from '@vue/test-utils'
import AsAsync from '../components/AsAsync.vue'

test('mount component', async () => {
expect(AsAsync).toBeTruthy()

const wrapper = mount(AsAsync)

await wrapper.find('button').trigger('click')

await flushPromises() // start loading, so vitest started loading
await vi.dynamicImportSettled()

expect(wrapper.html()).toContain('1 x 2 = 2')
})
4 changes: 3 additions & 1 deletion examples/vue/tsconfig.json
Expand Up @@ -2,6 +2,8 @@
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node"
"moduleResolution": "node",
"jsx": "preserve",
"types": ["vitest/globals"]
}
}
20 changes: 19 additions & 1 deletion packages/vitest/src/integrations/vi.ts
@@ -1,7 +1,7 @@
import type { FakeTimerInstallOpts } from '@sinonjs/fake-timers'
import { parseStacktrace } from '../utils/source-map'
import type { VitestMocker } from '../runtime/mocker'
import { getWorkerState, resetModules } from '../utils'
import { getWorkerState, resetModules, setTimeout } from '../utils'
import { FakeTimers } from './mock/timers'
import type { EnhancedSpy, MaybeMocked, MaybeMockedDeep } from './spy'
import { fn, isMockFunction, spies, spyOn } from './spy'
Expand Down Expand Up @@ -234,6 +234,24 @@ class VitestUtils {
resetModules()
return this
}

/**
* Wait for all imports to load.
* Useful, if you have a synchronous call that starts
* importing a module, that you cannot wait otherwise.
*/
public async dynamicImportSettled() {
const state = getWorkerState()
const promises: Promise<unknown>[] = []
for (const mod of state.moduleCache.values()) {
if (!mod.exports && mod.promise)
promises.push(mod.promise)
}
await Promise.all(promises)
// wait until the end of the loop, so `.then` on modules called,
// like in import('./example').then(...)
await new Promise(resolve => setTimeout(resolve))
}
}

export const vitest = new VitestUtils()
Expand Down
11 changes: 11 additions & 0 deletions test/core/test/vi.spec.ts
Expand Up @@ -28,4 +28,15 @@ describe('testing vi utils', () => {
const v2 = await import('vitest')
expect(v1).toBe(v2)
})

test('loads unloaded module', async () => {
let mod: any
import('../src/timeout').then(m => mod = m)

expect(mod).toBeUndefined()

await vi.dynamicImportSettled()

expect(mod.timeout).toBe(100)
})
})

0 comments on commit 1793937

Please sign in to comment.