utils.ts 742 B

123456789101112131415161718192021222324252627282930
  1. import { parse, SFCScriptCompileOptions, compileScript } from '../src'
  2. import { parse as babelParse } from '@babel/parser'
  3. import { babelParserDefaultPlugins } from '@vue/shared'
  4. export const mockId = 'xxxxxxxx'
  5. export function compileSFCScript(
  6. src: string,
  7. options?: Partial<SFCScriptCompileOptions>
  8. ) {
  9. const { descriptor } = parse(src)
  10. return compileScript(descriptor, {
  11. ...options,
  12. id: mockId
  13. })
  14. }
  15. export function assertCode(code: string) {
  16. // parse the generated code to make sure it is valid
  17. try {
  18. babelParse(code, {
  19. sourceType: 'module',
  20. plugins: [...babelParserDefaultPlugins, 'typescript']
  21. })
  22. } catch (e: any) {
  23. console.log(code)
  24. throw e
  25. }
  26. expect(code).toMatchSnapshot()
  27. }