Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vitest-environment-nuxt

Package Overview
Dependencies
Maintainers
2
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vitest-environment-nuxt - npm Package Compare versions

Comparing version 0.10.2 to 0.10.3

./dist/index.cjs

46

dist/utils.d.ts
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 };

39

package.json
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc