vitest-environment-nuxt
Advanced tools
Comparing version 0.10.2 to 0.10.3
import { EventHandler } from 'h3'; | ||
import * as vue from 'vue'; | ||
import { SetupContext, RenderFunction, ComputedOptions, MethodOptions, ComponentOptionsMixin, EmitsOptions, ComponentInjectOptions, ComponentOptionsWithoutProps, ComponentOptionsWithArrayProps, ComponentPropsOptions, ComponentOptionsWithObjectProps } from 'vue'; | ||
import * as _vue_test_utils from '@vue/test-utils'; | ||
import { ComponentMountingOptions } from '@vue/test-utils'; | ||
import { mount, ComponentMountingOptions } from '@vue/test-utils'; | ||
import { RouteLocationRaw } from 'vue-router'; | ||
import * as _testing_library_vue from '@testing-library/vue'; | ||
import { RenderOptions as RenderOptions$1 } from '@testing-library/vue'; | ||
@@ -112,4 +112,40 @@ type Awaitable<T> = T | Promise<T>; | ||
*/ | ||
declare function mountSuspended<T>(component: T, options?: MountSuspendedOptions<T>): Promise<_vue_test_utils.VueWrapper<globalThis.ComponentPublicInstance<T, {}, {}, {}, {}, Record<string, any>, vue.VNodeProps & T>> & Record<string, any>>; | ||
declare function mountSuspended<T>(component: T, options?: MountSuspendedOptions<T>): Promise<ReturnType<typeof mount<T>>>; | ||
export { mockComponent, mockNuxtImport, mountSuspended, registerEndpoint }; | ||
type RenderOptions = RenderOptions$1 & { | ||
route?: RouteLocationRaw; | ||
}; | ||
/** | ||
* `renderSuspended` allows you to mount any vue component within the Nuxt environment, allowing async setup and access to injections from your Nuxt plugins. | ||
* | ||
* This is a wrapper around the `render` function from @testing-libary/vue, and should be used together with | ||
* utilities from that package. | ||
* | ||
* ```ts | ||
* // tests/components/SomeComponents.nuxt.spec.ts | ||
* import { renderSuspended } from 'nuxt-vitest/utils' | ||
* | ||
* it('can render some component', async () => { | ||
* const { html } = await renderSuspended(SomeComponent) | ||
* expect(html()).toMatchInlineSnapshot( | ||
* 'This is an auto-imported component' | ||
* ) | ||
* | ||
* }) | ||
* | ||
* // tests/App.nuxt.spec.ts | ||
* import { renderSuspended } from 'nuxt-vitest/utils' | ||
* import { screen } from '@testing-library/vue' | ||
* | ||
* it('can also mount an app', async () => { | ||
* const { html } = await renderSuspended(App, { route: '/test' }) | ||
* expect(screen.getByRole('link', { name: 'Test Link' })).toBeVisible() | ||
* }) | ||
* ``` | ||
* | ||
* @param component the component to be tested | ||
* @param options optional options to set up your component | ||
*/ | ||
declare function renderSuspended<T>(component: T, options?: RenderOptions): Promise<_testing_library_vue.RenderResult>; | ||
export { mockComponent, mockNuxtImport, mountSuspended, registerEndpoint, renderSuspended }; |
{ | ||
"name": "vitest-environment-nuxt", | ||
"version": "0.10.2", | ||
"version": "0.10.3", | ||
"description": "An vitest environment with support for testing code that needs a Nuxt runtime environment.", | ||
@@ -18,9 +18,5 @@ "repository": "danielroe/nuxt-vitest", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "unbuild", | ||
"stub": "unbuild --stub", | ||
"prepublishOnly": "pnpm build" | ||
}, | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"exports": { | ||
@@ -48,15 +44,17 @@ "./utils": { | ||
"dependencies": { | ||
"@nuxt/kit": "^3.2.0", | ||
"@vue/test-utils": "^2.3.2", | ||
"@nuxt/kit": "^3.7.0", | ||
"@vue/test-utils": "^2.4.1", | ||
"defu": "^6.1.2", | ||
"estree-walker": "^3.0.3", | ||
"h3": "^1.6.5", | ||
"magic-string": "^0.30.0", | ||
"ofetch": "^1.0.0", | ||
"unenv": "^1.0.2", | ||
"whatwg-fetch": "3.6.17" | ||
"h3": "^1.8.1", | ||
"local-pkg": "^0.4.3", | ||
"magic-string": "^0.30.3", | ||
"ofetch": "^1.3.3", | ||
"ufo": "^1.3.0", | ||
"unenv": "^1.7.4" | ||
}, | ||
"devDependencies": { | ||
"@types/jsdom": "21.1.1", | ||
"happy-dom": "10.5.2", | ||
"@testing-library/vue": "7.0.0", | ||
"@types/jsdom": "21.1.2", | ||
"happy-dom": "10.11.1", | ||
"jsdom": "22.1.0", | ||
@@ -66,2 +64,3 @@ "vue": "3.3.4" | ||
"peerDependencies": { | ||
"@testing-library/vue": "7.0.0", | ||
"happy-dom": "^9.10.9 || ^10.0.0", | ||
@@ -74,2 +73,5 @@ "jsdom": "^22.0.0", | ||
"peerDependenciesMeta": { | ||
"@testing-library/vue": { | ||
"optional": true | ||
}, | ||
"happy-dom": { | ||
@@ -81,3 +83,8 @@ "optional": true | ||
} | ||
}, | ||
"scripts": { | ||
"build": "unbuild", | ||
"stub": "unbuild --stub", | ||
"release": "changelogen --release --no-commit --no-tag" | ||
} | ||
} | ||
} |
@@ -135,2 +135,39 @@ # nuxt-vitest | ||
### `renderSuspended` | ||
`renderSuspended` allows you to render any vue component within the Nuxt environment using `@testing-library/vue`, allowing async setup and access to injections from your Nuxt plugins. | ||
This should be used together with utilities from testing-library, e.g. `screen` and `fireEvent`. Install [@testing-library/vue](https://testing-library.com/docs/vue-testing-library/intro) in your project to use these. | ||
Additionally testing-library also relies on testing globals for cleanup. You should turn these on in your [Vitest config](https://vitest.dev/config/#globals). | ||
The passed in component will be rendered inside a `<div id="test-wrapper"></div>`. | ||
Examples: | ||
```ts | ||
// tests/components/SomeComponents.nuxt.spec.ts | ||
import { renderSuspended } from 'nuxt-vitest/utils' | ||
import { screen } from '@testing-library/vue' | ||
it('can render some component', async () => { | ||
await renderSuspended(SomeComponent) | ||
expect(screen.getByText('This is an auto-imported component')).toBeDefined() | ||
}) | ||
// tests/App.nuxt.spec.ts | ||
import { renderSuspended } from 'nuxt-vitest/utils' | ||
it('can also render an app', async () => { | ||
const html = await renderSuspended(App, { route: '/test' }) | ||
expect(html()).toMatchInlineSnapshot(` | ||
"<div id=\\"test-wrapper\\"> | ||
<div>This is an auto-imported component</div> | ||
<div> I am a global component </div> | ||
<div>Index page</div><a href=\\"/test\\"> Test link </a> | ||
</div>" | ||
`) | ||
}) | ||
``` | ||
### `mockNuxtImport` | ||
@@ -152,20 +189,21 @@ | ||
> **Note**: `mockNuxtImport` can only be used once per test file. It is actually a macro that gets transformed to `vi.mock` and `vi.mock` is hoisted, as described [here](https://vitest.dev/api/vi.html#vi-mock). | ||
> **Note**: `mockNuxtImport` can only be used once per mocked import per test file. It is actually a macro that gets transformed to `vi.mock` and `vi.mock` is hoisted, as described [here](https://vitest.dev/api/vi.html#vi-mock). | ||
If you need to mock a Nuxt import and provide different implementations between tests, you can do it by using a global variable as the returned value of your mock function and change its implementation within each test. Be careful to [restore mocks](https://vitest.dev/api/mock.html#mockrestore) before or after each test to undo mock state changes between runs. | ||
If you need to mock a Nuxt import and provide different implementations between tests, you can do it by creating and exposing your mocks using [`vi.hoisted`](https://vitest.dev/api/vi.html#vi-hoisted), and then use those mocks in `mockNuxtImport`. You then have access to the mocked imports, and can change the implementation between tests. Be careful to [restore mocks](https://vitest.dev/api/mock.html#mockrestore) before or after each test to undo mock state changes between runs. | ||
```ts | ||
// useStorageMock.ts | ||
let useStorageMock = vi.fn(() => { | ||
return { value: 'mocked storage' } | ||
}) | ||
export default useStorageMock | ||
``` | ||
```ts | ||
import useStorageMock from './useStorageMock' | ||
import { vi } from 'vitest' | ||
import { mockNuxtImport } from 'nuxt-vitest/utils' | ||
const { useStorageMock } = vi.hoisted(() => { | ||
return { | ||
useStorageMock: vi.fn().mockImplementation(() => { | ||
return { value: 'mocked storage'} | ||
}) | ||
} | ||
}) | ||
mockNuxtImport('useStorage', () => { | ||
return () => useStorageMock() | ||
return useStorageMock | ||
}) | ||
@@ -176,3 +214,3 @@ | ||
return { value: 'something else' } | ||
}) | ||
}) | ||
``` | ||
@@ -179,0 +217,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
77538
21
1428
340
1
16
5
+ Addedlocal-pkg@^0.4.3
+ Addedufo@^1.3.0
+ Added@babel/runtime@7.26.0(transitive)
+ Added@testing-library/dom@9.3.4(transitive)
+ Added@testing-library/vue@7.0.0(transitive)
+ Added@types/aria-query@5.0.4(transitive)
+ Addedaria-query@5.1.3(transitive)
+ Addedarray-buffer-byte-length@1.0.1(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcaniuse-lite@1.0.30001679(transitive)
+ Addedchalk@4.1.2(transitive)
+ Addeddeep-equal@2.2.3(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addeddom-accessibility-api@0.5.16(transitive)
+ Addedelectron-to-chromium@1.5.55(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-get-iterator@1.1.3(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-bigints@1.0.2(transitive)
+ Addedhas-flag@4.0.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinternal-slot@1.0.7(transitive)
+ Addedis-arguments@1.1.1(transitive)
+ Addedis-array-buffer@3.0.4(transitive)
+ Addedis-bigint@1.0.4(transitive)
+ Addedis-boolean-object@1.1.2(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-date-object@1.0.5(transitive)
+ Addedis-map@2.0.3(transitive)
+ Addedis-number-object@1.0.7(transitive)
+ Addedis-regex@1.1.4(transitive)
+ Addedis-set@2.0.3(transitive)
+ Addedis-shared-array-buffer@1.0.3(transitive)
+ Addedis-string@1.0.7(transitive)
+ Addedis-symbol@1.0.4(transitive)
+ Addedis-weakmap@2.0.2(transitive)
+ Addedis-weakset@2.0.3(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedlz-string@1.5.0(transitive)
+ Addedmlly@1.7.2(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedobject-is@1.1.6(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedpostcss@8.4.47(transitive)
+ Addedpretty-format@27.5.1(transitive)
+ Addedreact-is@17.0.2(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
+ Addedregexp.prototype.flags@1.5.3(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstop-iteration-iterator@1.0.0(transitive)
+ Addedsupports-color@7.2.0(transitive)
+ Addedwhich-boxed-primitive@1.0.2(transitive)
+ Addedwhich-collection@1.0.2(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)
- Removedwhatwg-fetch@3.6.17
- Removedcaniuse-lite@1.0.30001680(transitive)
- Removedelectron-to-chromium@1.5.57(transitive)
- Removedmlly@1.7.3(transitive)
- Removedpostcss@8.4.49(transitive)
- Removedwhatwg-fetch@3.6.17(transitive)
Updated@nuxt/kit@^3.7.0
Updated@vue/test-utils@^2.4.1
Updatedh3@^1.8.1
Updatedmagic-string@^0.30.3
Updatedofetch@^1.3.3
Updatedunenv@^1.7.4