Skip to content

Commit

Permalink
Merge pull request #4 from monsat/vitest
Browse files Browse the repository at this point in the history
vitest
  • Loading branch information
monsat authored Jul 31, 2022
2 parents 897673c + 3a08959 commit a707f88
Show file tree
Hide file tree
Showing 9 changed files with 430 additions and 4 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
],
"scripts": {
"prepack": "nuxt-module-build",
"test": "vitest",
"dev": "nuxi dev playground",
"dev:build": "nuxi build playground",
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground"
Expand All @@ -38,8 +39,11 @@
},
"devDependencies": {
"@nuxt/module-builder": "latest",
"@nuxt/test-utils-edge": "^3.0.0-rc.6-27651720.5dc864d",
"@nuxtjs/eslint-config-typescript": "latest",
"eslint": "latest",
"nuxt": "^3.0.0-rc.6"
"nuxt": "^3.0.0-rc.6",
"playwright": "^1.24.2",
"vitest": "^0.19.1"
}
}
24 changes: 24 additions & 0 deletions test/basic-with-enabled-false.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { fileURLToPath } from 'url'
import { describe, test, expect } from 'vitest'
import { setup, createPage } from '@nuxt/test-utils-edge'
import BasicAuth from '../src/module'

describe('Basic test: with module options { enabled: false }', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
browser: true,
nuxtConfig: {
modules: [
[BasicAuth, { enabled: false }],
],
},
})

test('allow if enabled option is false', async () => {
const page = await createPage('/')

const selector = await page.locator('div', { hasText: 'Nuxt Basic Auth Module' })
const count = await selector.count()
expect(count).not.toBe(0)
})
})
36 changes: 36 additions & 0 deletions test/basic-with-options.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { fileURLToPath } from 'url'
import { describe, test, expect } from 'vitest'
import { setup, createPage } from '@nuxt/test-utils-edge'
import BasicAuth from '../src/module'

describe('Basic test: with module options', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
browser: true,
nuxtConfig: {
modules: [
[BasicAuth, { enabled: true }],
],
},
})

test('access denied without http credentials', async () => {
const page = await createPage('/')

const selector = await page.locator('div', { hasText: 'Nuxt Basic Auth Module' })
const count = await selector.count()
expect(count).toBe(0)
})
test('allow with http credentials', async () => {
const page = await createPage('/', {
httpCredentials: {
username: 'admin',
password: 'admin',
},
})

const selector = await page.locator('div', { hasText: 'Nuxt Basic Auth Module' })
const count = await selector.count()
expect(count).not.toBe(0)
})
})
63 changes: 63 additions & 0 deletions test/basic-with-runtime-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { fileURLToPath } from 'url'
import { describe, test, expect } from 'vitest'
import { setup, createPage } from '@nuxt/test-utils-edge'
import BasicAuth from '../src/module'

describe('RuntimeConfig test: pairs', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
browser: true,
nuxtConfig: {
modules: [
[BasicAuth, { enabled: true }],
],
runtimeConfig: {
basicAuth: {
pairs: {
foo: 'bar',
baz: 'baz',
},
},
},
},
})

test('allow with http credentials', async () => {
const page = await createPage('/', {
httpCredentials: {
username: 'foo',
password: 'bar',
},
})

const selector = await page.locator('div', { hasText: 'Nuxt Basic Auth Module' })
const count = await selector.count()
expect(count).not.toBe(0)
})

test('allow with right pair', async () => {
const page = await createPage('/', {
httpCredentials: {
username: 'baz',
password: 'baz',
},
})

const selector = await page.locator('div', { hasText: 'Nuxt Basic Auth Module' })
const count = await selector.count()
expect(count).not.toBe(0)
})

test('access denied with wrong pair', async () => {
const page = await createPage('/', {
httpCredentials: {
username: 'wrong',
password: 'pair',
},
})

const selector = await page.locator('div', { hasText: 'Nuxt Basic Auth Module' })
const count = await selector.count()
expect(count).toBe(0)
})
})
36 changes: 36 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { fileURLToPath } from 'url'
import { describe, test, expect } from 'vitest'
import { setup, createPage } from '@nuxt/test-utils-edge'
import BasicAuth from '../src/module'

describe('Basic test: without module options', async () => {
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
browser: true,
nuxtConfig: {
modules: [
BasicAuth,
],
},
})

test('access denied without http credentials', async () => {
const page = await createPage('/')

const selector = await page.locator('div', { hasText: 'Nuxt Basic Auth Module' })
const count = await selector.count()
expect(count).toBe(0)
})
test('allow with http credentials', async () => {
const page = await createPage('/', {
httpCredentials: {
username: 'admin',
password: 'admin',
},
})

const selector = await page.locator('div', { hasText: 'Nuxt Basic Auth Module' })
const count = await selector.count()
expect(count).not.toBe(0)
})
})
8 changes: 8 additions & 0 deletions test/fixtures/basic/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<template>
<div>
Nuxt Basic Auth Module playground!
</div>
</template>

<script setup>
</script>
4 changes: 4 additions & 0 deletions test/fixtures/basic/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
})
11 changes: 11 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/// <reference types="vitest" />
import { defineConfig } from 'vite'

export default defineConfig({
test: {
testTimeout: 300000,
deps: {
inline: [/@nuxt\/test-utils-edge/]
},
},
})
Loading

0 comments on commit a707f88

Please sign in to comment.