-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(#22): add vitest snapshots for name inference
- Loading branch information
1 parent
304911d
commit b0d8435
Showing
6 changed files
with
167 additions
and
6 deletions.
There are no files selected for viewing
Binary file not shown.
53 changes: 53 additions & 0 deletions
53
crates/fervid_napi/__tests__/__snapshots__/compileScript.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`SFC analyze <script> bindings > auto name inference > basic 1`] = ` | ||
"import { toDisplayString as _toDisplayString } from "vue"; | ||
export default { | ||
__name: "FooBar", | ||
render (_ctx, _cache, $props, $setup, $data, $options) { | ||
return _toDisplayString(a); | ||
}, | ||
setup () { | ||
const a = 1; | ||
return { | ||
a | ||
}; | ||
} | ||
}; | ||
" | ||
`; | ||
exports[`SFC analyze <script> bindings > auto name inference > do not overwrite manual name (call) 1`] = ` | ||
"import { defineComponent } from 'vue'; | ||
import { toDisplayString as _toDisplayString } from "vue"; | ||
export default { | ||
name: 'Baz', | ||
render (_ctx, _cache, $props, $setup, $data, $options) { | ||
return _toDisplayString(a); | ||
}, | ||
setup () { | ||
const a = 1; | ||
return { | ||
a | ||
}; | ||
} | ||
}; | ||
" | ||
`; | ||
exports[`SFC analyze <script> bindings > auto name inference > do not overwrite manual name (object) 1`] = ` | ||
"import { toDisplayString as _toDisplayString } from "vue"; | ||
export default { | ||
name: 'Baz', | ||
render (_ctx, _cache, $props, $setup, $data, $options) { | ||
return _toDisplayString(a); | ||
}, | ||
setup () { | ||
const a = 1; | ||
return { | ||
a | ||
}; | ||
} | ||
}; | ||
" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { describe, test, expect } from 'vitest' | ||
import { parse as babelParse } from '@babel/parser' | ||
import { Compiler, FervidCompileOptions } from '..' | ||
|
||
describe('SFC analyze <script> bindings', () => { | ||
// https://github.com/vuejs/core/blob/272ab9fbdcb1af0535108b9f888e80d612f9171d/packages/compiler-sfc/__tests__/compileScript.spec.ts#L1252-L1306 | ||
describe('auto name inference', () => { | ||
test('basic', () => { | ||
const { content } = compile( | ||
`<script setup>const a = 1</script> | ||
<template>{{ a }}</template>`, | ||
{ | ||
filename: 'FooBar.vue', | ||
id: '' | ||
}, | ||
) | ||
expect(content).toMatch(`export default { | ||
__name: "FooBar"`) | ||
assertCode(content) | ||
}) | ||
|
||
test('do not overwrite manual name (object)', () => { | ||
const { content } = compile( | ||
`<script> | ||
export default { | ||
name: 'Baz' | ||
} | ||
</script> | ||
<script setup>const a = 1</script> | ||
<template>{{ a }}</template>`, | ||
{ | ||
filename: 'FooBar.vue', | ||
id: '' | ||
}, | ||
) | ||
expect(content).not.toMatch(`name: 'FooBar'`) | ||
expect(content).toMatch(`name: 'Baz'`) | ||
assertCode(content) | ||
}) | ||
|
||
test('do not overwrite manual name (call)', () => { | ||
const { content } = compile( | ||
`<script> | ||
import { defineComponent } from 'vue' | ||
export default defineComponent({ | ||
name: 'Baz' | ||
}) | ||
</script> | ||
<script setup>const a = 1</script> | ||
<template>{{ a }}</template>`, | ||
{ | ||
filename: 'FooBar.vue', | ||
id: '' | ||
}, | ||
) | ||
expect(content).not.toMatch(`name: 'FooBar'`) | ||
expect(content).toMatch(`name: 'Baz'`) | ||
assertCode(content) | ||
}) | ||
}) | ||
}) | ||
|
||
// https://github.com/vuejs/core/blob/272ab9fbdcb1af0535108b9f888e80d612f9171d/packages/compiler-sfc/__tests__/utils.ts#L11-L24 | ||
function compile(src: string, options: FervidCompileOptions) { | ||
const compiler = new Compiler() | ||
const result = compiler.compileSync(src, options) | ||
|
||
if (result.errors.length) { | ||
console.warn(result.errors[0]) | ||
} | ||
|
||
return { | ||
content: result.code | ||
} | ||
} | ||
|
||
function assertCode(code: string) { | ||
// parse the generated code to make sure it is valid | ||
try { | ||
babelParse(code, { | ||
sourceType: 'module', | ||
plugins: [ | ||
'typescript', | ||
['importAttributes', { deprecatedAssertSyntax: true }], | ||
], | ||
}) | ||
} catch (e: any) { | ||
console.log(code) | ||
throw e | ||
} | ||
expect(code).toMatchSnapshot() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters