|
| 1 | +import { beforeEach, describe, expect, test, vi } from 'vitest' |
| 2 | + |
| 3 | +import { svelteTesting } from '../vite.js' |
| 4 | +import { IS_JEST } from './utils.js' |
| 5 | + |
| 6 | +describe.skipIf(IS_JEST)('vite plugin', () => { |
| 7 | + beforeEach(() => { |
| 8 | + vi.stubEnv('VITEST', '1') |
| 9 | + }) |
| 10 | + |
| 11 | + test('does not modify config if disabled', () => { |
| 12 | + const subject = svelteTesting({ |
| 13 | + resolveBrowser: false, |
| 14 | + autoCleanup: false, |
| 15 | + noExternal: false, |
| 16 | + }) |
| 17 | + |
| 18 | + const result = {} |
| 19 | + subject.config(result) |
| 20 | + |
| 21 | + expect(result).toEqual({}) |
| 22 | + }) |
| 23 | + |
| 24 | + test('does not modify config if not Vitest', () => { |
| 25 | + vi.stubEnv('VITEST', '') |
| 26 | + |
| 27 | + const subject = svelteTesting() |
| 28 | + |
| 29 | + const result = {} |
| 30 | + subject.config(result) |
| 31 | + |
| 32 | + expect(result).toEqual({}) |
| 33 | + }) |
| 34 | + |
| 35 | + test.each([ |
| 36 | + { |
| 37 | + config: () => ({ resolve: { conditions: ['node'] } }), |
| 38 | + expectedConditions: ['browser', 'node'], |
| 39 | + }, |
| 40 | + { |
| 41 | + config: () => ({ resolve: { conditions: ['svelte', 'node'] } }), |
| 42 | + expectedConditions: ['svelte', 'browser', 'node'], |
| 43 | + }, |
| 44 | + ])( |
| 45 | + 'adds browser condition if necessary', |
| 46 | + ({ config, expectedConditions }) => { |
| 47 | + const subject = svelteTesting({ |
| 48 | + resolveBrowser: true, |
| 49 | + autoCleanup: false, |
| 50 | + noExternal: false, |
| 51 | + }) |
| 52 | + |
| 53 | + const result = config() |
| 54 | + subject.config(result) |
| 55 | + |
| 56 | + expect(result).toEqual({ |
| 57 | + resolve: { |
| 58 | + conditions: expectedConditions, |
| 59 | + }, |
| 60 | + }) |
| 61 | + } |
| 62 | + ) |
| 63 | + |
| 64 | + test.each([ |
| 65 | + { |
| 66 | + config: () => ({}), |
| 67 | + expectedConditions: [], |
| 68 | + }, |
| 69 | + { |
| 70 | + config: () => ({ resolve: { conditions: [] } }), |
| 71 | + expectedConditions: [], |
| 72 | + }, |
| 73 | + { |
| 74 | + config: () => ({ resolve: { conditions: ['svelte'] } }), |
| 75 | + expectedConditions: ['svelte'], |
| 76 | + }, |
| 77 | + ])( |
| 78 | + 'skips browser condition if possible', |
| 79 | + ({ config, expectedConditions }) => { |
| 80 | + const subject = svelteTesting({ |
| 81 | + resolveBrowser: true, |
| 82 | + autoCleanup: false, |
| 83 | + noExternal: false, |
| 84 | + }) |
| 85 | + |
| 86 | + const result = config() |
| 87 | + subject.config(result) |
| 88 | + |
| 89 | + expect(result).toEqual({ |
| 90 | + resolve: { |
| 91 | + conditions: expectedConditions, |
| 92 | + }, |
| 93 | + }) |
| 94 | + } |
| 95 | + ) |
| 96 | + |
| 97 | + test.each([ |
| 98 | + { |
| 99 | + config: () => ({}), |
| 100 | + expectedSetupFiles: [expect.stringMatching(/src\/vitest.js$/u)], |
| 101 | + }, |
| 102 | + { |
| 103 | + config: () => ({ test: { setupFiles: [] } }), |
| 104 | + expectedSetupFiles: [expect.stringMatching(/src\/vitest.js$/u)], |
| 105 | + }, |
| 106 | + { |
| 107 | + config: () => ({ test: { setupFiles: 'other-file.js' } }), |
| 108 | + expectedSetupFiles: [ |
| 109 | + 'other-file.js', |
| 110 | + expect.stringMatching(/src\/vitest.js$/u), |
| 111 | + ], |
| 112 | + }, |
| 113 | + ])('adds cleanup', ({ config, expectedSetupFiles }) => { |
| 114 | + const subject = svelteTesting({ |
| 115 | + resolveBrowser: false, |
| 116 | + autoCleanup: true, |
| 117 | + noExternal: false, |
| 118 | + }) |
| 119 | + |
| 120 | + const result = config() |
| 121 | + subject.config(result) |
| 122 | + |
| 123 | + expect(result).toEqual({ |
| 124 | + test: { |
| 125 | + setupFiles: expectedSetupFiles, |
| 126 | + }, |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + test('skips cleanup in global mode', () => { |
| 131 | + const subject = svelteTesting({ |
| 132 | + resolveBrowser: false, |
| 133 | + autoCleanup: true, |
| 134 | + noExternal: false, |
| 135 | + }) |
| 136 | + |
| 137 | + const result = { test: { globals: true } } |
| 138 | + subject.config(result) |
| 139 | + |
| 140 | + expect(result).toEqual({ |
| 141 | + test: { |
| 142 | + globals: true, |
| 143 | + }, |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + test.each([ |
| 148 | + { |
| 149 | + config: () => ({ ssr: { noExternal: [] } }), |
| 150 | + expectedNoExternal: ['@testing-library/svelte'], |
| 151 | + }, |
| 152 | + { |
| 153 | + config: () => ({}), |
| 154 | + expectedNoExternal: ['@testing-library/svelte'], |
| 155 | + }, |
| 156 | + { |
| 157 | + config: () => ({ ssr: { noExternal: 'other-file.js' } }), |
| 158 | + expectedNoExternal: ['other-file.js', '@testing-library/svelte'], |
| 159 | + }, |
| 160 | + { |
| 161 | + config: () => ({ ssr: { noExternal: /other/u } }), |
| 162 | + expectedNoExternal: [/other/u, '@testing-library/svelte'], |
| 163 | + }, |
| 164 | + ])('adds noExternal rule', ({ config, expectedNoExternal }) => { |
| 165 | + const subject = svelteTesting({ |
| 166 | + resolveBrowser: false, |
| 167 | + autoCleanup: false, |
| 168 | + noExternal: true, |
| 169 | + }) |
| 170 | + |
| 171 | + const result = config() |
| 172 | + subject.config(result) |
| 173 | + |
| 174 | + expect(result).toEqual({ |
| 175 | + ssr: { |
| 176 | + noExternal: expectedNoExternal, |
| 177 | + }, |
| 178 | + }) |
| 179 | + }) |
| 180 | + |
| 181 | + test.each([ |
| 182 | + { |
| 183 | + config: () => ({ ssr: { noExternal: true } }), |
| 184 | + expectedNoExternal: true, |
| 185 | + }, |
| 186 | + { |
| 187 | + config: () => ({ ssr: { noExternal: '@testing-library/svelte' } }), |
| 188 | + expectedNoExternal: '@testing-library/svelte', |
| 189 | + }, |
| 190 | + { |
| 191 | + config: () => ({ ssr: { noExternal: /svelte/u } }), |
| 192 | + expectedNoExternal: /svelte/u, |
| 193 | + }, |
| 194 | + ])('skips noExternal if able', ({ config, expectedNoExternal }) => { |
| 195 | + const subject = svelteTesting({ |
| 196 | + resolveBrowser: false, |
| 197 | + autoCleanup: false, |
| 198 | + noExternal: true, |
| 199 | + }) |
| 200 | + |
| 201 | + const result = config() |
| 202 | + subject.config(result) |
| 203 | + |
| 204 | + expect(result).toEqual({ |
| 205 | + ssr: { |
| 206 | + noExternal: expectedNoExternal, |
| 207 | + }, |
| 208 | + }) |
| 209 | + }) |
| 210 | + |
| 211 | + test('bails on noExternal if input is unexpected', () => { |
| 212 | + const subject = svelteTesting({ |
| 213 | + resolveBrowser: false, |
| 214 | + autoCleanup: false, |
| 215 | + noExternal: true, |
| 216 | + }) |
| 217 | + |
| 218 | + const result = { ssr: { noExternal: false } } |
| 219 | + subject.config(result) |
| 220 | + |
| 221 | + expect(result).toEqual({ |
| 222 | + ssr: { |
| 223 | + noExternal: false, |
| 224 | + }, |
| 225 | + }) |
| 226 | + }) |
| 227 | +}) |
0 commit comments