]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
test(compiler-sfc): add real tests for source maps (#704)
authorDmitry Sharshakov <d3dx12.xx@gmail.com>
Mon, 10 Feb 2020 14:34:13 +0000 (17:34 +0300)
committerGitHub <noreply@github.com>
Mon, 10 Feb 2020 14:34:13 +0000 (09:34 -0500)
packages/compiler-sfc/__tests__/parse.spec.ts

index fcb59061aebcd4705a44b33a9820cc0e7fa4bb9e..ddd6570de9a5ca30e8f518e6bee9593c741b3738 100644 (file)
@@ -1,23 +1,40 @@
 import { parse } from '../src'
 import { mockWarn } from '@vue/shared'
 import { baseParse, baseCompile } from '@vue/compiler-core'
+import { SourceMapConsumer } from 'source-map'
 
 describe('compiler:sfc', () => {
   mockWarn()
 
   describe('source map', () => {
     test('style block', () => {
-      const style = parse(`<style>\n.color {\n color: red;\n }\n</style>\n`)
-        .descriptor.styles[0]
-      // TODO need to actually test this with SourceMapConsumer
+      // Padding determines how many blank lines will there be before the style block
+      const padding = Math.round(Math.random() * 10)
+      const style = parse(
+        `${'\n'.repeat(padding)}<style>\n.color {\n color: red;\n }\n</style>\n`
+      ).descriptor.styles[0]
+
       expect(style.map).not.toBeUndefined()
+
+      const consumer = new SourceMapConsumer(style.map!)
+      consumer.eachMapping(mapping => {
+        expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
+      })
     })
 
     test('script block', () => {
-      const script = parse(`<script>\nconsole.log(1)\n }\n</script>\n`)
-        .descriptor.script
-      // TODO need to actually test this with SourceMapConsumer
+      // Padding determines how many blank lines will there be before the style block
+      const padding = Math.round(Math.random() * 10)
+      const script = parse(
+        `${'\n'.repeat(padding)}<script>\nconsole.log(1)\n }\n</script>\n`
+      ).descriptor.script
+
       expect(script!.map).not.toBeUndefined()
+
+      const consumer = new SourceMapConsumer(script!.map!)
+      consumer.eachMapping(mapping => {
+        expect(mapping.originalLine - mapping.generatedLine).toBe(padding)
+      })
     })
   })