Skip to content

Commit

Permalink
fixup: skip cleanup in globals mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous committed Oct 18, 2024
1 parent f435ff5 commit 9126fcd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
65 changes: 53 additions & 12 deletions src/__tests__/vite-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ describe.skipIf(IS_JEST)('vite plugin', () => {
])(
'adds browser condition if necessary',
({ config, expectedConditions }) => {
const subject = svelteTesting()
const subject = svelteTesting({
resolveBrowser: true,
autoCleanup: false,
noExternal: false,
})

const result = config()
subject.config(result)

expect(result).toMatchObject({
expect(result).toEqual({
resolve: {
conditions: expectedConditions,
},
Expand All @@ -73,12 +77,16 @@ describe.skipIf(IS_JEST)('vite plugin', () => {
])(
'skips browser condition if possible',
({ config, expectedConditions }) => {
const subject = svelteTesting()
const subject = svelteTesting({
resolveBrowser: true,
autoCleanup: false,
noExternal: false,
})

const result = config()
subject.config(result)

expect(result).toMatchObject({
expect(result).toEqual({
resolve: {
conditions: expectedConditions,
},
Expand All @@ -103,18 +111,39 @@ describe.skipIf(IS_JEST)('vite plugin', () => {
],
},
])('adds cleanup', ({ config, expectedSetupFiles }) => {
const subject = svelteTesting()
const subject = svelteTesting({
resolveBrowser: false,
autoCleanup: true,
noExternal: false,
})

const result = config()
subject.config(result)

expect(result).toMatchObject({
expect(result).toEqual({
test: {
setupFiles: expectedSetupFiles,
},
})
})

test('skips cleanup in global mode', () => {
const subject = svelteTesting({
resolveBrowser: false,
autoCleanup: true,
noExternal: false,
})

const result = { test: { globals: true } }
subject.config(result)

expect(result).toEqual({
test: {
globals: true,
},
})
})

test.each([
{
config: () => ({ ssr: { noExternal: [] } }),
Expand All @@ -133,12 +162,16 @@ describe.skipIf(IS_JEST)('vite plugin', () => {
expectedNoExternal: [/other/u, '@testing-library/svelte'],
},
])('adds noExternal rule', ({ config, expectedNoExternal }) => {
const subject = svelteTesting()
const subject = svelteTesting({
resolveBrowser: false,
autoCleanup: false,
noExternal: true,
})

const result = config()
subject.config(result)

expect(result).toMatchObject({
expect(result).toEqual({
ssr: {
noExternal: expectedNoExternal,
},
Expand All @@ -159,25 +192,33 @@ describe.skipIf(IS_JEST)('vite plugin', () => {
expectedNoExternal: /svelte/u,
},
])('skips noExternal if able', ({ config, expectedNoExternal }) => {
const subject = svelteTesting()
const subject = svelteTesting({
resolveBrowser: false,
autoCleanup: false,
noExternal: true,
})

const result = config()
subject.config(result)

expect(result).toMatchObject({
expect(result).toEqual({
ssr: {
noExternal: expectedNoExternal,
},
})
})

test('bails on noExternal if input is unexpected', () => {
const subject = svelteTesting()
const subject = svelteTesting({
resolveBrowser: false,
autoCleanup: false,
noExternal: true,
})

const result = { ssr: { noExternal: false } }
subject.config(result)

expect(result).toMatchObject({
expect(result).toEqual({
ssr: {
noExternal: false,
},
Expand Down
9 changes: 5 additions & 4 deletions src/vite.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ const addAutoCleanup = (config) => {
const test = config.test ?? {}
let setupFiles = test.setupFiles ?? []

if (test.globals) {
return
}

if (typeof setupFiles === 'string') {
setupFiles = [setupFiles]
}
Expand Down Expand Up @@ -106,10 +110,7 @@ const addNoExternal = (config) => {
return
}

if (
noExternal instanceof RegExp &&
noExternal.test('@testing-library/svelte')
) {
if (rule instanceof RegExp && rule.test('@testing-library/svelte')) {
return
}
}
Expand Down

0 comments on commit 9126fcd

Please sign in to comment.