From 430d4e10bb096c68fcfea889a0f4d847531ffb9a Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Mon, 10 Feb 2020 17:34:13 +0300 Subject: [PATCH] test(compiler-sfc): add real tests for source maps (#704) --- packages/compiler-sfc/__tests__/parse.spec.ts | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/compiler-sfc/__tests__/parse.spec.ts b/packages/compiler-sfc/__tests__/parse.spec.ts index fcb59061a..ddd6570de 100644 --- a/packages/compiler-sfc/__tests__/parse.spec.ts +++ b/packages/compiler-sfc/__tests__/parse.spec.ts @@ -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(`\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)}\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(`\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)}\n` + ).descriptor.script + expect(script!.map).not.toBeUndefined() + + const consumer = new SourceMapConsumer(script!.map!) + consumer.eachMapping(mapping => { + expect(mapping.originalLine - mapping.generatedLine).toBe(padding) + }) }) })