Skip to content

Commit 7767661

Browse files
committed
fix: handle enabled: false mocks properly
Fixes harlan-zw/nuxt-seo#501
1 parent 84e1537 commit 7767661

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

src/module.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ export default defineNuxtModule<ModuleOptions>({
232232
logger.debug('The module is disabled, skipping setup.')
233233
// need to mock the composables to allow module still to work when disabled
234234
addImports({ name: 'useRobotsRule', from: resolve(`./runtime/app/composables/mock`) })
235+
addImports({ name: 'useBotDetection', from: resolve(`./runtime/app/composables/mock`) })
235236
nuxt.options.nitro = nuxt.options.nitro || {}
236237
nuxt.options.nitro.imports = nuxt.options.nitro.imports || {}
237238
nuxt.options.nitro.imports.presets = nuxt.options.nitro.imports.presets || []
@@ -240,6 +241,9 @@ export default defineNuxtModule<ModuleOptions>({
240241
imports: [
241242
'getPathRobotConfig',
242243
'getSiteRobotConfig',
244+
'getBotDetection',
245+
'isBot',
246+
'getBotInfo',
243247
],
244248
})
245249
return

test/e2e/disabled.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createResolver } from '@nuxt/kit'
2+
import { $fetch, setup } from '@nuxt/test-utils'
3+
import { describe, expect, it } from 'vitest'
4+
5+
const { resolve } = createResolver(import.meta.url)
6+
7+
process.env.NODE_ENV = 'production'
8+
9+
await setup({
10+
rootDir: resolve('../fixtures/disabled'),
11+
build: true,
12+
})
13+
14+
describe('disabled module', () => {
15+
it('should not crash when module is disabled', async () => {
16+
const html = await $fetch('/')
17+
expect(html).toContain('Disabled Module Test')
18+
expect(html).toContain('Module is disabled')
19+
})
20+
21+
it('should not have robots.txt when disabled', async () => {
22+
// The module should not add the robots.txt route when disabled
23+
await expect($fetch('/robots.txt')).rejects.toThrow()
24+
})
25+
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import NuxtRobots from '../../../src/module'
2+
3+
export default defineNuxtConfig({
4+
modules: [
5+
NuxtRobots,
6+
],
7+
robots: {
8+
enabled: false,
9+
},
10+
site: {
11+
url: 'https://example.com',
12+
},
13+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<template>
2+
<div>
3+
<h1>Disabled Module Test</h1>
4+
<p>Module is disabled</p>
5+
</div>
6+
</template>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../../.nuxt/tsconfig.json"
3+
}

test/unit/mocks.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
describe('mock composables', () => {
4+
it('app mocks should export correctly', async () => {
5+
const mocks = await import('../../src/runtime/app/composables/mock')
6+
7+
expect(mocks.useRobotsRule).toBeDefined()
8+
expect(mocks.useBotDetection).toBeDefined()
9+
10+
// Test that they return expected mock values
11+
const robotRule = mocks.useRobotsRule()
12+
expect(robotRule.value).toBe('')
13+
14+
const botDetection = mocks.useBotDetection()
15+
expect(botDetection.isBot.value).toBe(false)
16+
expect(botDetection.botName.value).toBeUndefined()
17+
})
18+
19+
it('server mocks should export correctly', async () => {
20+
const mocks = await import('../../src/runtime/server/mock-composables')
21+
22+
expect(mocks.getPathRobotConfig).toBeDefined()
23+
expect(mocks.getSiteRobotConfig).toBeDefined()
24+
expect(mocks.getBotDetection).toBeDefined()
25+
expect(mocks.isBot).toBeDefined()
26+
expect(mocks.getBotInfo).toBeDefined()
27+
28+
// Test that they return expected mock values
29+
const pathConfig = mocks.getPathRobotConfig({} as any)
30+
expect(pathConfig).toEqual({ indexable: true, rule: '' })
31+
32+
const siteConfig = mocks.getSiteRobotConfig({} as any)
33+
expect(siteConfig).toEqual({ indexable: true, hints: [] })
34+
35+
const botDetection = mocks.getBotDetection({} as any)
36+
expect(botDetection).toEqual({ isBot: false })
37+
38+
const isBotResult = mocks.isBot({} as any)
39+
expect(isBotResult).toBe(false)
40+
41+
const botInfo = mocks.getBotInfo({} as any)
42+
expect(botInfo).toBeNull()
43+
})
44+
})

0 commit comments

Comments
 (0)