util.ts 734 B

1234567891011121314151617181920212223242526272829303132333435
  1. import {
  2. parse,
  3. compileScript,
  4. type SFCParseOptions,
  5. type SFCScriptCompileOptions
  6. } from '../src'
  7. import { parse as babelParse } from '@babel/parser'
  8. export const mockId = 'xxxxxxxx'
  9. export function compile(
  10. source: string,
  11. options?: Partial<SFCScriptCompileOptions>,
  12. parseOptions?: Partial<SFCParseOptions>
  13. ) {
  14. const sfc = parse({
  15. ...parseOptions,
  16. source
  17. })
  18. return compileScript(sfc, { id: mockId, ...options })
  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. }