Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
test(nuxt): add tests for import protection plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 11, 2022
1 parent 888bd7c commit 4eefca4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/nuxt/src/core/plugins/import-protection.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRequire } from 'node:module'
import { createUnplugin } from 'unplugin'
import { logger } from '@nuxt/kit'
import { isAbsolute, join, relative, resolve } from 'pathe'
import { isAbsolute, join, relative } from 'pathe'
import type { Nuxt } from '@nuxt/schema'
import escapeRE from 'escape-string-regexp'

Expand All @@ -21,7 +21,7 @@ export const vueAppPatterns = (nuxt: Nuxt) => [
[new RegExp(`^${escapeRE(m as string)}$`), 'Importing directly from module entry points is not allowed.']),
...[/(^|node_modules\/)@nuxt\/kit/, /^nitropack/]
.map(i => [i, 'This module cannot be imported in the Vue part of your app.']),
[new RegExp(escapeRE(resolve(nuxt.options.srcDir, (nuxt.options.dir as any).server || 'server')) + '\\/(api|routes|middleware|plugins)\\/'), 'Importing from server is not allowed in the Vue part of your app.']
[new RegExp(escapeRE(join(nuxt.options.srcDir, (nuxt.options.dir as any).server || 'server')) + '\\/(api|routes|middleware|plugins)\\/'), 'Importing from server is not allowed in the Vue part of your app.']
] as ImportProtectionOptions['patterns']

export const ImportProtectionPlugin = createUnplugin(function (options: ImportProtectionOptions) {
Expand Down
47 changes: 47 additions & 0 deletions packages/nuxt/test/import-protection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, expect, it } from 'vitest'
import { ImportProtectionPlugin, vueAppPatterns } from '../src/core/plugins/import-protection'

const testsToTriggerOn = [
['~/nuxt.config', 'app.vue', true],
['./nuxt.config', 'app.vue', true],
['./nuxt.config.ts', 'app.vue', true],
['nuxt.config.ts', 'app.vue', true],
['./.nuxt/nuxt.config', 'app.vue', false],
['.nuxt/nuxt.config', 'app.vue', false],
['nuxt', 'components/Component.vue', true],
['nuxt3', 'components/Component.vue', true],
['/root/node_modules/@vue/composition-api', 'components/Component.vue', true],
['@vue/composition-api', 'components/Component.vue', true],
['@nuxt/kit', 'components/Component.vue', true],
['/root/node_modules/@nuxt/kit', 'components/Component.vue', true],
['some-nuxt-module', 'components/Component.vue', true],
['/root/src/server/api/test.ts', 'components/Component.vue', true],
['src/server/api/test.ts', 'components/Component.vue', true]
] as const

describe('import protection', () => {
it.each(testsToTriggerOn)('should protect %s', async (id, importer, isProtected) => {
const result = await transformWithImportProtection(id, importer)
if (!isProtected) {
expect(result).toBeNull()
} else {
expect(result).toBeDefined()
expect(result).contains('unenv/runtime/mock/proxy')
}
})
})

const transformWithImportProtection = (id: string, importer: string) => {
const plugin = ImportProtectionPlugin.rollup({
rootDir: '/root',
patterns: vueAppPatterns({
options: {
modules: ['some-nuxt-module'],
srcDir: 'src/',
dir: { server: 'server' }
}
} as any)
})

return (plugin as any).resolveId(id, importer)
}

0 comments on commit 4eefca4

Please sign in to comment.