utils.ts 729 B

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