|
| 1 | +import { resolve } from 'path' |
| 2 | +import { readFileSync } from 'fs' |
| 3 | +import clearModule from 'clear-module' |
| 4 | +import cache from '../lib/cache' |
| 5 | +import jestVue from '../vue-jest' |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + cache.flushAll() |
| 9 | + clearModule.all() |
| 10 | +}) |
| 11 | + |
| 12 | +const getSourceMaps = code => { |
| 13 | + const sourceMapBase64 = /\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(.*)/gim |
| 14 | + |
| 15 | + let matches |
| 16 | + let values = [] |
| 17 | + |
| 18 | + while ((matches = sourceMapBase64.exec(code)) !== null) { |
| 19 | + values.push(JSON.parse(Buffer.from(matches[1], 'base64').toString('ascii'))) |
| 20 | + } |
| 21 | + |
| 22 | + return values |
| 23 | +} |
| 24 | + |
| 25 | +test('generates source maps for .vue files', () => { |
| 26 | + const filePath = resolve(__dirname, './resources/Basic.vue') |
| 27 | + const fileString = readFileSync(filePath, { encoding: 'utf8' }) |
| 28 | + |
| 29 | + const { code } = jestVue.process(fileString, filePath, { |
| 30 | + moduleFileExtensions: ['js', 'vue'] |
| 31 | + }) |
| 32 | + |
| 33 | + const [template, js] = getSourceMaps(code) |
| 34 | + |
| 35 | + expect(js.sources[0]).toBe('Basic.vue') |
| 36 | + expect(template.sources[0]).toBe('Basic.vue') |
| 37 | +}) |
| 38 | + |
| 39 | +test('generates source maps using src attributes', () => { |
| 40 | + const filePath = resolve(__dirname, './resources/SourceMapsSrc.vue') |
| 41 | + const fileString = readFileSync(filePath, { encoding: 'utf8' }) |
| 42 | + |
| 43 | + const { code } = jestVue.process(fileString, filePath, { |
| 44 | + moduleFileExtensions: ['js', 'vue'] |
| 45 | + }) |
| 46 | + |
| 47 | + const [template, js] = getSourceMaps(code) |
| 48 | + |
| 49 | + expect(js.sources[0]).toBe('BasicSrc.js') |
| 50 | + expect(template.sources[0]).toBe('SourceMapsSrc.vue') |
| 51 | +}) |
0 commit comments