From 0e5895043ffdc76857f798e0daabc05797ff22ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Mon, 20 Dec 2021 18:26:30 +0900 Subject: [PATCH] test(es/codegen): Add some tests for sourcemap (#3078) --- .../tests/fixture/sourcemap/006/input/.swcrc | 11 +++ .../fixture/sourcemap/006/input/index.js | 42 ++++++++ .../fixture/sourcemap/006/output/index.js | 97 +++++++++++++++++++ .../fixture/sourcemap/006/output/index.map | 37 +++++++ .../tests/fixture/sourcemap/007/input/.swcrc | 11 +++ .../fixture/sourcemap/007/input/index.js | 9 ++ .../fixture/sourcemap/007/output/index.js | 8 ++ .../fixture/sourcemap/007/output/index.map | 16 +++ .../tests/fixture/sourcemap/008/input/.swcrc | 13 +++ .../fixture/sourcemap/008/input/index.js | 21 ++++ .../fixture/sourcemap/008/output/index.js | 17 ++++ .../fixture/sourcemap/008/output/index.map | 16 +++ .../tests/fixture/sourcemap/009/input/.swcrc | 13 +++ .../fixture/sourcemap/009/input/index.js | 7 ++ .../fixture/sourcemap/009/output/index.js | 7 ++ .../fixture/sourcemap/009/output/index.map | 16 +++ 16 files changed, 341 insertions(+) create mode 100644 crates/swc/tests/fixture/sourcemap/006/input/.swcrc create mode 100644 crates/swc/tests/fixture/sourcemap/006/input/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/006/output/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/006/output/index.map create mode 100644 crates/swc/tests/fixture/sourcemap/007/input/.swcrc create mode 100644 crates/swc/tests/fixture/sourcemap/007/input/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/007/output/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/007/output/index.map create mode 100644 crates/swc/tests/fixture/sourcemap/008/input/.swcrc create mode 100644 crates/swc/tests/fixture/sourcemap/008/input/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/008/output/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/008/output/index.map create mode 100644 crates/swc/tests/fixture/sourcemap/009/input/.swcrc create mode 100644 crates/swc/tests/fixture/sourcemap/009/input/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/009/output/index.js create mode 100644 crates/swc/tests/fixture/sourcemap/009/output/index.map diff --git a/crates/swc/tests/fixture/sourcemap/006/input/.swcrc b/crates/swc/tests/fixture/sourcemap/006/input/.swcrc new file mode 100644 index 00000000000..d9f64e4559a --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/006/input/.swcrc @@ -0,0 +1,11 @@ +{ + "sourceMaps": true, + "inlineSourcesContent": true, + "jsc": { + "transform": { + "hidden": { + "jest": true + } + } + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/006/input/index.js b/crates/swc/tests/fixture/sourcemap/006/input/index.js new file mode 100644 index 00000000000..513aad087ff --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/006/input/index.js @@ -0,0 +1,42 @@ +it('should compress avif smaller than webp and smaller than jpg', async () => { + + /** + * + * 'Foo bar baz' + * + * Return @ + */ + const query = { url: '/test.jpg', w, q: 75 } + const res1 = await fetchViaHTTP(appPort, '/_next/image', query, { + headers: { + accept: 'image/avif', + }, + }) + expect(res1.status).toBe(200) + expect(res1.headers.get('Content-Type')).toBe('image/avif') + + const res2 = await fetchViaHTTP(appPort, '/_next/image', query, { + headers: { + accept: 'image/webp', + }, + }) + expect(res2.status).toBe(200) + expect(res2.headers.get('Content-Type')).toBe('image/webp') + + const res3 = await fetchViaHTTP(appPort, '/_next/image', query, { + headers: { + accept: 'image/jpeg', + }, + }) + expect(res3.status).toBe(200) + expect(res3.headers.get('Content-Type')).toBe('image/jpeg') + + const avif = (await res1.buffer()).byteLength + const webp = (await res2.buffer()).byteLength + const jpeg = (await res3.buffer()).byteLength + + console.log({ isSharp, w, avif, webp, jpeg }) + + expect(webp).toBeLessThan(jpeg) + expect(avif).toBeLessThan(webp) +}) \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/006/output/index.js b/crates/swc/tests/fixture/sourcemap/006/output/index.js new file mode 100644 index 00000000000..428256aa64e --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/006/output/index.js @@ -0,0 +1,97 @@ +import regeneratorRuntime from "regenerator-runtime"; +function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { + try { + var info = gen[key](arg); + var value = info.value; + } catch (error) { + reject(error); + return; + } + if (info.done) { + resolve(value); + } else { + Promise.resolve(value).then(_next, _throw); + } +} +function _asyncToGenerator(fn) { + return function() { + var self = this, args = arguments; + return new Promise(function(resolve, reject) { + var gen = fn.apply(self, args); + function _next(value) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); + } + function _throw(err) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); + } + _next(undefined); + }); + }; +} +it('should compress avif smaller than webp and smaller than jpg', _asyncToGenerator(regeneratorRuntime.mark(function _callee() { + var query, res1, res2, res3, avif, webp, jpeg; + return regeneratorRuntime.wrap(function _callee$(_ctx) { + while(1)switch(_ctx.prev = _ctx.next){ + case 0: + query = { + url: '/test.jpg', + w: w, + q: 75 + }; + _ctx.next = 3; + return fetchViaHTTP(appPort, '/_next/image', query, { + headers: { + accept: 'image/avif' + } + }); + case 3: + res1 = _ctx.sent; + expect(res1.status).toBe(200); + expect(res1.headers.get('Content-Type')).toBe('image/avif'); + _ctx.next = 8; + return fetchViaHTTP(appPort, '/_next/image', query, { + headers: { + accept: 'image/webp' + } + }); + case 8: + res2 = _ctx.sent; + expect(res2.status).toBe(200); + expect(res2.headers.get('Content-Type')).toBe('image/webp'); + _ctx.next = 13; + return fetchViaHTTP(appPort, '/_next/image', query, { + headers: { + accept: 'image/jpeg' + } + }); + case 13: + res3 = _ctx.sent; + expect(res3.status).toBe(200); + expect(res3.headers.get('Content-Type')).toBe('image/jpeg'); + _ctx.next = 18; + return res1.buffer(); + case 18: + avif = _ctx.sent.byteLength; + _ctx.next = 21; + return res2.buffer(); + case 21: + webp = _ctx.sent.byteLength; + _ctx.next = 24; + return res3.buffer(); + case 24: + jpeg = _ctx.sent.byteLength; + console.log({ + isSharp: isSharp, + w: w, + avif: avif, + webp: webp, + jpeg: jpeg + }); + expect(webp).toBeLessThan(jpeg); + expect(avif).toBeLessThan(webp); + case 28: + case "end": + return _ctx.stop(); + } + }, _callee); +}))); diff --git a/crates/swc/tests/fixture/sourcemap/006/output/index.map b/crates/swc/tests/fixture/sourcemap/006/output/index.map new file mode 100644 index 00000000000..b047684c614 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/006/output/index.map @@ -0,0 +1,37 @@ +{ + "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAAA,EAAE,CAAC,CAA6D,wGAAE,QAAQ,WAAI,CAAC;QAQrEC,KAAK,EACLC,IAAI,EAQJC,IAAI,EAQJC,IAAI,EAQJC,IAAI,EACJC,IAAI,EACJC,IAAI;;;;gBA3BJN,KAAK,GAAG,CAAC;oBAACO,GAAG,EAAE,CAAW;oBAAEC,CAAC,EAADA,CAAC;oBAAEC,CAAC,EAAE,EAAE;gBAAC,CAAC;;uBACzBC,YAAY,CAACC,OAAO,EAAE,CAAc,eAAEX,KAAK,EAAE,CAAC;oBAC7DY,OAAO,EAAE,CAAC;wBACNC,MAAM,EAAE,CAAY;oBACxB,CAAC;gBACL,CAAC;;gBAJKZ,IAAI;gBAKVa,MAAM,CAACb,IAAI,CAACc,MAAM,EAAEC,IAAI,CAAC,GAAG;gBAC5BF,MAAM,CAACb,IAAI,CAACW,OAAO,CAACK,GAAG,CAAC,CAAc,gBAAGD,IAAI,CAAC,CAAY;;uBAEvCN,YAAY,CAACC,OAAO,EAAE,CAAc,eAAEX,KAAK,EAAE,CAAC;oBAC7DY,OAAO,EAAE,CAAC;wBACNC,MAAM,EAAE,CAAY;oBACxB,CAAC;gBACL,CAAC;;gBAJKX,IAAI;gBAKVY,MAAM,CAACZ,IAAI,CAACa,MAAM,EAAEC,IAAI,CAAC,GAAG;gBAC5BF,MAAM,CAACZ,IAAI,CAACU,OAAO,CAACK,GAAG,CAAC,CAAc,gBAAGD,IAAI,CAAC,CAAY;;uBAEvCN,YAAY,CAACC,OAAO,EAAE,CAAc,eAAEX,KAAK,EAAE,CAAC;oBAC7DY,OAAO,EAAE,CAAC;wBACNC,MAAM,EAAE,CAAY;oBACxB,CAAC;gBACL,CAAC;;gBAJKV,IAAI;gBAKVW,MAAM,CAACX,IAAI,CAACY,MAAM,EAAEC,IAAI,CAAC,GAAG;gBAC5BF,MAAM,CAACX,IAAI,CAACS,OAAO,CAACK,GAAG,CAAC,CAAc,gBAAGD,IAAI,CAAC,CAAY;;uBAEtCf,IAAI,CAACiB,MAAM;;gBAAzBd,IAAI,aAAyBe,UAAU;;uBACzBjB,IAAI,CAACgB,MAAM;;gBAAzBb,IAAI,aAAyBc,UAAU;;uBACzBhB,IAAI,CAACe,MAAM;;gBAAzBZ,IAAI,aAAyBa,UAAU;gBAE7CC,OAAO,CAACC,GAAG,CAAC,CAAC;oBAACC,OAAO,EAAPA,OAAO;oBAAEd,CAAC,EAADA,CAAC;oBAAEJ,IAAI,EAAJA,IAAI;oBAAEC,IAAI,EAAJA,IAAI;oBAAEC,IAAI,EAAJA,IAAI;gBAAC,CAAC;gBAE5CQ,MAAM,CAACT,IAAI,EAAEkB,YAAY,CAACjB,IAAI;gBAC9BQ,MAAM,CAACV,IAAI,EAAEmB,YAAY,CAAClB,IAAI;;;;;;AAClC,CAAC", + "names": [ + "it", + "query", + "res1", + "res2", + "res3", + "avif", + "webp", + "jpeg", + "url", + "w", + "q", + "fetchViaHTTP", + "appPort", + "headers", + "accept", + "expect", + "status", + "toBe", + "get", + "buffer", + "byteLength", + "console", + "log", + "isSharp", + "toBeLessThan" + ], + "sources": [ + "../../input/index.js" + ], + "sourcesContent": [ + "it('should compress avif smaller than webp and smaller than jpg', async () => {\n\n /**\n * \n * 'Foo bar baz'\n * \n * Return @\n */\n const query = { url: '/test.jpg', w, q: 75 }\n const res1 = await fetchViaHTTP(appPort, '/_next/image', query, {\n headers: {\n accept: 'image/avif',\n },\n })\n expect(res1.status).toBe(200)\n expect(res1.headers.get('Content-Type')).toBe('image/avif')\n\n const res2 = await fetchViaHTTP(appPort, '/_next/image', query, {\n headers: {\n accept: 'image/webp',\n },\n })\n expect(res2.status).toBe(200)\n expect(res2.headers.get('Content-Type')).toBe('image/webp')\n\n const res3 = await fetchViaHTTP(appPort, '/_next/image', query, {\n headers: {\n accept: 'image/jpeg',\n },\n })\n expect(res3.status).toBe(200)\n expect(res3.headers.get('Content-Type')).toBe('image/jpeg')\n\n const avif = (await res1.buffer()).byteLength\n const webp = (await res2.buffer()).byteLength\n const jpeg = (await res3.buffer()).byteLength\n\n console.log({ isSharp, w, avif, webp, jpeg })\n\n expect(webp).toBeLessThan(jpeg)\n expect(avif).toBeLessThan(webp)\n})" + ], + "version": 3 +} diff --git a/crates/swc/tests/fixture/sourcemap/007/input/.swcrc b/crates/swc/tests/fixture/sourcemap/007/input/.swcrc new file mode 100644 index 00000000000..d9f64e4559a --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/007/input/.swcrc @@ -0,0 +1,11 @@ +{ + "sourceMaps": true, + "inlineSourcesContent": true, + "jsc": { + "transform": { + "hidden": { + "jest": true + } + } + } +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/007/input/index.js b/crates/swc/tests/fixture/sourcemap/007/input/index.js new file mode 100644 index 00000000000..ef647a1da3c --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/007/input/index.js @@ -0,0 +1,9 @@ +/** + * Logs some text + */ +export const LogSomeText = (text) => { + console.log(text); + console.log(text); + console.log(text); + console.log(text); +}; \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/007/output/index.js b/crates/swc/tests/fixture/sourcemap/007/output/index.js new file mode 100644 index 00000000000..4237a24e47b --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/007/output/index.js @@ -0,0 +1,8 @@ +/** + * Logs some text + */ export var LogSomeText = function(text) { + console.log(text); + console.log(text); + console.log(text); + console.log(text); +}; diff --git a/crates/swc/tests/fixture/sourcemap/007/output/index.map b/crates/swc/tests/fixture/sourcemap/007/output/index.map new file mode 100644 index 00000000000..897aac223f8 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/007/output/index.map @@ -0,0 +1,16 @@ +{ + "mappings": "AAAA,EAEG,AAFH;;CAEG,AAFH,EAEG,CACH,MAAM,CAAC,GAAK,CAACA,WAAW,GAAG,QAAQ,CAAPC,IAAI,EAAK,CAAC;IAClCC,OAAO,CAACC,GAAG,CAACF,IAAI;IAChBC,OAAO,CAACC,GAAG,CAACF,IAAI;IAChBC,OAAO,CAACC,GAAG,CAACF,IAAI;IAChBC,OAAO,CAACC,GAAG,CAACF,IAAI;AACpB,CAAC", + "names": [ + "LogSomeText", + "text", + "console", + "log" + ], + "sources": [ + "../../input/index.js" + ], + "sourcesContent": [ + "/**\n * Logs some text\n */\nexport const LogSomeText = (text) => {\n console.log(text);\n console.log(text);\n console.log(text);\n console.log(text);\n};" + ], + "version": 3 +} diff --git a/crates/swc/tests/fixture/sourcemap/008/input/.swcrc b/crates/swc/tests/fixture/sourcemap/008/input/.swcrc new file mode 100644 index 00000000000..5b4bdf07506 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/008/input/.swcrc @@ -0,0 +1,13 @@ +{ + "module": { + "type": "commonjs" + }, + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": false + }, + "target": "es2016" + }, + "sourceMaps": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/008/input/index.js b/crates/swc/tests/fixture/sourcemap/008/input/index.js new file mode 100644 index 00000000000..cfbab132523 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/008/input/index.js @@ -0,0 +1,21 @@ +describe("multiline comments", () => { + it("test1", () => { + expect(false).toBe(true); + }); + + it("test2", () => { + /**/ + expect(false).toBe(true); + }); + + it("test3", () => { + /* + * + */ + expect(false).toBe(true); + }); + + it("test4", () => { + expect(false).toBe(true); + }); +}); \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/008/output/index.js b/crates/swc/tests/fixture/sourcemap/008/output/index.js new file mode 100644 index 00000000000..a7e215e4ebb --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/008/output/index.js @@ -0,0 +1,17 @@ +"use strict"; +describe("multiline comments", ()=>{ + it("test1", ()=>{ + expect(false).toBe(true); + }); + it("test2", ()=>{ + /**/ expect(false).toBe(true); + }); + it("test3", ()=>{ + /* + * + */ expect(false).toBe(true); + }); + it("test4", ()=>{ + expect(false).toBe(true); + }); +}); diff --git a/crates/swc/tests/fixture/sourcemap/008/output/index.map b/crates/swc/tests/fixture/sourcemap/008/output/index.map new file mode 100644 index 00000000000..14b107e74fd --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/008/output/index.map @@ -0,0 +1,16 @@ +{ + "mappings": ";AAAAA,QAAQ,CAAC,CAAoB,yBAAQ,CAAC;IAClCC,EAAE,CAAC,CAAO,YAAQ,CAAC;QACfC,MAAM,CAAC,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B,CAAC;IAEDF,EAAE,CAAC,CAAO,YAAQ,CAAC;QACf,EAAI,AAAJ,EAAI,CACJC,MAAM,CAAC,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B,CAAC;IAEDF,EAAE,CAAC,CAAO,YAAQ,CAAC;QACf,EAEG,AAFH;;SAEG,AAFH,EAEG,CACHC,MAAM,CAAC,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B,CAAC;IAEDF,EAAE,CAAC,CAAO,YAAQ,CAAC;QACfC,MAAM,CAAC,KAAK,EAAEC,IAAI,CAAC,IAAI;IAC3B,CAAC;AACL,CAAC", + "names": [ + "describe", + "it", + "expect", + "toBe" + ], + "sources": [ + "../../input/index.js" + ], + "sourcesContent": [ + "describe(\"multiline comments\", () => {\n it(\"test1\", () => {\n expect(false).toBe(true);\n });\n\n it(\"test2\", () => {\n /**/\n expect(false).toBe(true);\n });\n\n it(\"test3\", () => {\n /*\n *\n */\n expect(false).toBe(true);\n });\n\n it(\"test4\", () => {\n expect(false).toBe(true);\n });\n});" + ], + "version": 3 +} diff --git a/crates/swc/tests/fixture/sourcemap/009/input/.swcrc b/crates/swc/tests/fixture/sourcemap/009/input/.swcrc new file mode 100644 index 00000000000..5b4bdf07506 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/009/input/.swcrc @@ -0,0 +1,13 @@ +{ + "module": { + "type": "commonjs" + }, + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": false + }, + "target": "es2016" + }, + "sourceMaps": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/009/input/index.js b/crates/swc/tests/fixture/sourcemap/009/input/index.js new file mode 100644 index 00000000000..69ea187cd68 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/009/input/index.js @@ -0,0 +1,7 @@ +/** + * This is a + * long + * license + * header + */ +console.log(new Error().stack) \ No newline at end of file diff --git a/crates/swc/tests/fixture/sourcemap/009/output/index.js b/crates/swc/tests/fixture/sourcemap/009/output/index.js new file mode 100644 index 00000000000..30dab98b770 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/009/output/index.js @@ -0,0 +1,7 @@ +"use strict"; +/** + * This is a + * long + * license + * header + */ console.log(new Error().stack); diff --git a/crates/swc/tests/fixture/sourcemap/009/output/index.map b/crates/swc/tests/fixture/sourcemap/009/output/index.map new file mode 100644 index 00000000000..364685e4561 --- /dev/null +++ b/crates/swc/tests/fixture/sourcemap/009/output/index.map @@ -0,0 +1,16 @@ +{ + "mappings": ";AAAA,EAKG,AALH;;;;;CAKG,AALH,EAKG,CACHA,OAAO,CAACC,GAAG,CAAC,GAAG,CAACC,KAAK,GAAGC,KAAK", + "names": [ + "console", + "log", + "Error", + "stack" + ], + "sources": [ + "../../input/index.js" + ], + "sourcesContent": [ + "/**\n * This is a\n * long\n * license\n * header\n */\nconsole.log(new Error().stack)" + ], + "version": 3 +}