|
| 1 | +import { Plugin } from 'vite-5' |
| 2 | +import { ViteDevServerConfig } from '../../src/devServer' |
| 3 | +import { Vite } from '../../src/getVite' |
| 4 | +import { CypressSourcemap } from '../../src/plugins' |
| 5 | +import Chai, { expect } from 'chai' |
| 6 | +import SinonChai from 'sinon-chai' |
| 7 | +import sinon from 'sinon' |
| 8 | + |
| 9 | +Chai.use(SinonChai) |
| 10 | + |
| 11 | +describe('sourcemap plugin', () => { |
| 12 | + ['js', 'jsx', 'ts', 'tsx', 'vue', 'svelte', 'mjs', 'cjs'].forEach((ext) => { |
| 13 | + it(`should append sourcemap to the code if sourceMappingURL is not present for files with extension ${ext}`, () => { |
| 14 | + const code = 'console.log("hello world")' |
| 15 | + const id = `test.js` |
| 16 | + const options = {} as ViteDevServerConfig |
| 17 | + const vite = {} as Vite |
| 18 | + const plugin = CypressSourcemap(options, vite) as Plugin & { getCombinedSourcemap: () => { toUrl: () => string } } |
| 19 | + |
| 20 | + plugin.getCombinedSourcemap = () => { |
| 21 | + return { |
| 22 | + toUrl: () => 'data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==', |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + expect(plugin.name).to.equal('cypress:sourcemap') |
| 27 | + expect(plugin.enforce).to.equal('post') |
| 28 | + |
| 29 | + if (plugin.transform instanceof Function) { |
| 30 | + const result = plugin.transform.call(plugin, code, id) |
| 31 | + |
| 32 | + expect(result.code).to.eq('console.log("hello world")\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==') |
| 33 | + } else { |
| 34 | + throw new Error('transform is not a function') |
| 35 | + } |
| 36 | + }) |
| 37 | + |
| 38 | + it(`should replace sourceMappingURL with sourcemap and handle query parameters for files with extension ${ext}`, () => { |
| 39 | + const code = 'console.log("hello world")\n//# sourceMappingURL=old-url' |
| 40 | + const id = `test.js?v=12345` |
| 41 | + const options = {} as ViteDevServerConfig |
| 42 | + const vite = {} as Vite |
| 43 | + const plugin = CypressSourcemap(options, vite) as Plugin & { getCombinedSourcemap: () => { toUrl: () => string } } |
| 44 | + |
| 45 | + plugin.getCombinedSourcemap = () => { |
| 46 | + return { |
| 47 | + toUrl: () => 'data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==', |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + expect(plugin.name).to.equal('cypress:sourcemap') |
| 52 | + expect(plugin.enforce).to.equal('post') |
| 53 | + |
| 54 | + if (plugin.transform instanceof Function) { |
| 55 | + const result = plugin.transform.call(plugin, code, id) |
| 56 | + |
| 57 | + expect(result.code).to.eq('console.log("hello world")\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==') |
| 58 | + } else { |
| 59 | + throw new Error('transform is not a function') |
| 60 | + } |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should not append sourcemap to the code if sourceMappingURL is already present', () => { |
| 65 | + const code = 'console.log("hello world")\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozfQ==' |
| 66 | + const id = `test.js` |
| 67 | + const options = {} as ViteDevServerConfig |
| 68 | + const vite = {} as Vite |
| 69 | + const plugin = CypressSourcemap(options, vite) as Plugin & { getCombinedSourcemap: () => { toUrl: () => string } } |
| 70 | + |
| 71 | + plugin.getCombinedSourcemap = sinon.stub() |
| 72 | + |
| 73 | + expect(plugin.name).to.equal('cypress:sourcemap') |
| 74 | + expect(plugin.enforce).to.equal('post') |
| 75 | + |
| 76 | + if (plugin.transform instanceof Function) { |
| 77 | + const result = plugin.transform.call(plugin, code, id) |
| 78 | + |
| 79 | + expect(result).to.be.undefined |
| 80 | + expect(plugin.getCombinedSourcemap).not.to.have.been.called |
| 81 | + } else { |
| 82 | + throw new Error('transform is not a function') |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + it('should not append sourcemap to the code if the file is not a js, jsx, ts, tsx, vue, mjs, or cjs file', () => { |
| 87 | + const code = 'console.log("hello world")' |
| 88 | + const id = `test.css` |
| 89 | + const options = {} as ViteDevServerConfig |
| 90 | + const vite = {} as Vite |
| 91 | + const plugin = CypressSourcemap(options, vite) as Plugin & { getCombinedSourcemap: () => { toUrl: () => string } } |
| 92 | + |
| 93 | + plugin.getCombinedSourcemap = sinon.stub() |
| 94 | + |
| 95 | + expect(plugin.name).to.equal('cypress:sourcemap') |
| 96 | + expect(plugin.enforce).to.equal('post') |
| 97 | + |
| 98 | + if (plugin.transform instanceof Function) { |
| 99 | + const result = plugin.transform.call(plugin, code, id) |
| 100 | + |
| 101 | + expect(result).to.be.undefined |
| 102 | + expect(plugin.getCombinedSourcemap).not.to.have.been.called |
| 103 | + } else { |
| 104 | + throw new Error('transform is not a function') |
| 105 | + } |
| 106 | + }) |
| 107 | +}) |
0 commit comments