Skip to content

Commit

Permalink
test: add unit test for define plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikg committed Mar 4, 2022
1 parent a4927c5 commit 2c61b02
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/vite/src/node/__tests__/plugins/define.spec.ts
@@ -0,0 +1,39 @@
import { definePlugin } from '../../plugins/define'
import { resolveConfig } from '../../config'

async function createDefinePluginTransform(
define: Record<string, any> = {},
build = true,
ssr = false
) {
const config = await resolveConfig({ define }, build ? 'build' : 'serve')
const instance = definePlugin(config)
return async (code: string) => {
const result = await instance.transform.call({}, code, 'foo.ts', { ssr })
return result?.code || result
}
}

describe('definePlugin', () => {
test('replaces custom define', async () => {
const transform = await createDefinePluginTransform({
__APP_VERSION__: JSON.stringify('1.0')
})
expect(await transform('const version = __APP_VERSION__ ;')).toBe(
'const version = "1.0" ;'
)
expect(await transform('const version = __APP_VERSION__;')).toBe(
'const version = "1.0";'
)
})

test('replaces import.meta.env.SSR with false', async () => {
const transform = await createDefinePluginTransform()
expect(await transform('const isSSR = import.meta.env.SSR ;')).toBe(
'const isSSR = false ;'
)
expect(await transform('const isSSR = import.meta.env.SSR;')).toBe(
'const isSSR = false;'
)
})
})

0 comments on commit 2c61b02

Please sign in to comment.