|
1 | 1 | const swc = require("../pkg");
|
2 | 2 |
|
3 |
| -it("should be loadable", function () { |
4 |
| - const output = swc.transformSync("class Foo {}", {}); |
| 3 | +describe("transform", () => { |
| 4 | + it("should work", function () { |
| 5 | + const output = swc.transformSync("class Foo {}", {}); |
| 6 | + |
| 7 | + expect(output).toMatchInlineSnapshot(` |
| 8 | + Object { |
| 9 | + "code": "function _classCallCheck(instance, Constructor) { |
| 10 | + if (!(instance instanceof Constructor)) { |
| 11 | + throw new TypeError(\\"Cannot call a class as a function\\"); |
| 12 | + } |
| 13 | + } |
| 14 | + var Foo = function Foo() { |
| 15 | + \\"use strict\\"; |
| 16 | + _classCallCheck(this, Foo); |
| 17 | + }; |
| 18 | + ", |
| 19 | + } |
| 20 | + `); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should work with async facade", async () => { |
| 24 | + const output = await swc.transform("class Foo {}", {}); |
| 25 | + |
| 26 | + expect(output).toMatchInlineSnapshot(` |
| 27 | + Object { |
| 28 | + "code": "function _classCallCheck(instance, Constructor) { |
| 29 | + if (!(instance instanceof Constructor)) { |
| 30 | + throw new TypeError(\\"Cannot call a class as a function\\"); |
| 31 | + } |
| 32 | + } |
| 33 | + var Foo = function Foo() { |
| 34 | + \\"use strict\\"; |
| 35 | + _classCallCheck(this, Foo); |
| 36 | + }; |
| 37 | + ", |
| 38 | + } |
| 39 | + `); |
| 40 | + }); |
| 41 | + |
| 42 | + it("should work with program object", async () => { |
| 43 | + const input = swc.parseSync("class Foo {}", { |
| 44 | + syntax: "typescript", |
| 45 | + target: "es2021", |
| 46 | + }); |
| 47 | + |
| 48 | + const output = await swc.transform(input, {}); |
| 49 | + expect(output).toMatchInlineSnapshot(` |
| 50 | + Object { |
| 51 | + "code": "function _classCallCheck(instance, Constructor) { |
| 52 | + if (!(instance instanceof Constructor)) { |
| 53 | + throw new TypeError(\\"Cannot call a class as a function\\"); |
| 54 | + } |
| 55 | + } |
| 56 | + var Foo = function Foo() { |
| 57 | + \\"use strict\\"; |
| 58 | + _classCallCheck(this, Foo); |
| 59 | + }; |
| 60 | + ", |
| 61 | + } |
| 62 | + `); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should support 'paths' and 'baseUrl'", () => { |
| 66 | + const { code } = swc.transformSync( |
| 67 | + ` |
| 68 | + import foo from '@src/app'; |
| 69 | + console.log(foo) |
| 70 | + `, |
| 71 | + { |
| 72 | + filename: "main.js", |
| 73 | + jsc: { |
| 74 | + parser: { |
| 75 | + syntax: "typescript", |
| 76 | + }, |
| 77 | + target: "es2021", |
| 78 | + transform: {}, |
| 79 | + baseUrl: __dirname, |
| 80 | + paths: { |
| 81 | + "@src/*": ["bar/*"], |
| 82 | + }, |
| 83 | + }, |
| 84 | + module: { |
| 85 | + type: "commonjs", |
| 86 | + }, |
| 87 | + } |
| 88 | + ); |
| 89 | + |
| 90 | + expect(code).toContain(`bar/app`); |
| 91 | + }); |
5 | 92 | });
|
6 | 93 |
|
7 |
| -it("should support 'paths' and 'baseUrl'", async () => { |
8 |
| - const { code } = await swc.transformSync( |
9 |
| - ` |
10 |
| - import foo from '@src/app'; |
11 |
| - console.log(foo) |
12 |
| - `, |
13 |
| - { |
14 |
| - filename: "main.js", |
15 |
| - jsc: { |
16 |
| - parser: { |
17 |
| - syntax: "typescript", |
| 94 | +describe("parse", () => { |
| 95 | + it("should work", () => { |
| 96 | + const output = swc.parseSync("class Foo {}", { |
| 97 | + syntax: "typescript", |
| 98 | + target: "es2021", |
| 99 | + }); |
| 100 | + |
| 101 | + expect(output).toMatchInlineSnapshot(` |
| 102 | + Object { |
| 103 | + "body": Array [ |
| 104 | + Object { |
| 105 | + "body": Array [], |
| 106 | + "declare": false, |
| 107 | + "decorators": Array [], |
| 108 | + "identifier": Object { |
| 109 | + "optional": false, |
| 110 | + "span": Object { |
| 111 | + "ctxt": 0, |
| 112 | + "end": 394, |
| 113 | + "start": 391, |
| 114 | + }, |
| 115 | + "type": "Identifier", |
| 116 | + "value": "Foo", |
| 117 | + }, |
| 118 | + "implements": Array [], |
| 119 | + "isAbstract": false, |
| 120 | + "span": Object { |
| 121 | + "ctxt": 0, |
| 122 | + "end": 397, |
| 123 | + "start": 385, |
| 124 | + }, |
| 125 | + "superClass": null, |
| 126 | + "superTypeParams": null, |
| 127 | + "type": "ClassDeclaration", |
| 128 | + "typeParams": null, |
18 | 129 | },
|
19 |
| - target: "es2021", |
20 |
| - transform: {}, |
21 |
| - baseUrl: __dirname, |
22 |
| - paths: { |
23 |
| - "@src/*": ["bar/*"], |
| 130 | + ], |
| 131 | + "interpreter": null, |
| 132 | + "span": Object { |
| 133 | + "ctxt": 0, |
| 134 | + "end": 397, |
| 135 | + "start": 385, |
| 136 | + }, |
| 137 | + "type": "Module", |
| 138 | + } |
| 139 | + `); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should work with async facade", async () => { |
| 143 | + const output = await swc.parse("class Foo {}", { |
| 144 | + syntax: "typescript", |
| 145 | + target: "es2021", |
| 146 | + }); |
| 147 | + |
| 148 | + expect(output).toMatchInlineSnapshot(` |
| 149 | + Object { |
| 150 | + "body": Array [ |
| 151 | + Object { |
| 152 | + "body": Array [], |
| 153 | + "declare": false, |
| 154 | + "decorators": Array [], |
| 155 | + "identifier": Object { |
| 156 | + "optional": false, |
| 157 | + "span": Object { |
| 158 | + "ctxt": 0, |
| 159 | + "end": 407, |
| 160 | + "start": 404, |
| 161 | + }, |
| 162 | + "type": "Identifier", |
| 163 | + "value": "Foo", |
| 164 | + }, |
| 165 | + "implements": Array [], |
| 166 | + "isAbstract": false, |
| 167 | + "span": Object { |
| 168 | + "ctxt": 0, |
| 169 | + "end": 410, |
| 170 | + "start": 398, |
| 171 | + }, |
| 172 | + "superClass": null, |
| 173 | + "superTypeParams": null, |
| 174 | + "type": "ClassDeclaration", |
| 175 | + "typeParams": null, |
24 | 176 | },
|
25 |
| - }, |
26 |
| - module: { |
27 |
| - type: "commonjs", |
28 |
| - }, |
29 |
| - } |
30 |
| - ); |
31 |
| - |
32 |
| - expect(code).toContain(`bar/app`); |
| 177 | + ], |
| 178 | + "interpreter": null, |
| 179 | + "span": Object { |
| 180 | + "ctxt": 0, |
| 181 | + "end": 410, |
| 182 | + "start": 398, |
| 183 | + }, |
| 184 | + "type": "Module", |
| 185 | + } |
| 186 | + `); |
| 187 | + }); |
| 188 | +}); |
| 189 | + |
| 190 | +describe("minify", () => { |
| 191 | + it("should work", () => { |
| 192 | + const output = swc.minifySync( |
| 193 | + "const somename = 1; console.log(somename);" |
| 194 | + ); |
| 195 | + |
| 196 | + expect(output).toMatchInlineSnapshot(` |
| 197 | + Object { |
| 198 | + "code": "const a=1;console.log(1)", |
| 199 | + } |
| 200 | + `); |
| 201 | + }); |
| 202 | + |
| 203 | + it("should work with async facade", async () => { |
| 204 | + const output = await swc.minify( |
| 205 | + "const somename = 1; console.log(somename);" |
| 206 | + ); |
| 207 | + |
| 208 | + expect(output).toMatchInlineSnapshot(` |
| 209 | + Object { |
| 210 | + "code": "const a=1;console.log(1)", |
| 211 | + } |
| 212 | + `); |
| 213 | + }); |
| 214 | +}); |
| 215 | + |
| 216 | +describe("print", () => { |
| 217 | + it("should work", () => { |
| 218 | + const input = swc.parseSync("class Foo {}", { |
| 219 | + syntax: "typescript", |
| 220 | + target: "es2021", |
| 221 | + }); |
| 222 | + |
| 223 | + const output = swc.printSync(input); |
| 224 | + expect(output).toMatchInlineSnapshot(` |
| 225 | + Object { |
| 226 | + "code": "class Foo { |
| 227 | + } |
| 228 | + ", |
| 229 | + } |
| 230 | + `); |
| 231 | + }); |
| 232 | + |
| 233 | + it("should work with async facade", async () => { |
| 234 | + const input = swc.parseSync("class Foo {}", { |
| 235 | + syntax: "typescript", |
| 236 | + target: "es2021", |
| 237 | + }); |
| 238 | + |
| 239 | + const output = await swc.print(input); |
| 240 | + expect(output).toMatchInlineSnapshot(` |
| 241 | + Object { |
| 242 | + "code": "class Foo { |
| 243 | + } |
| 244 | + ", |
| 245 | + } |
| 246 | + `); |
| 247 | + }); |
33 | 248 | });
|
0 commit comments