Skip to content

Commit dcc759e

Browse files
committed
refactor: split test files
1 parent b97d8f2 commit dcc759e

File tree

4 files changed

+179
-167
lines changed

4 files changed

+179
-167
lines changed

test/_utils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { format } from "prettier";
2+
import { generateCode } from "../src";
3+
4+
export function generate(mod: any) {
5+
return format(generateCode(mod).code, { parser: "babel-ts" }).trim();
6+
}

test/exports.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { expect, it, describe } from "vitest";
2+
import { parseCode } from "../src";
3+
import { generate } from "./_utils";
4+
5+
describe("exports", () => {
6+
it("manipulate exports", () => {
7+
const mod = parseCode("");
8+
9+
expect(mod.exports).toMatchInlineSnapshot(`{}`);
10+
expect(generate(mod)).toMatchInlineSnapshot('""');
11+
12+
mod.exports.default = { foo: "1" };
13+
14+
expect(generate(mod)).toMatchInlineSnapshot(`
15+
"export default {
16+
foo: \\"1\\",
17+
};"
18+
`);
19+
20+
mod.exports.default.foo = 2;
21+
22+
expect(generate(mod)).toMatchInlineSnapshot(`
23+
"export default {
24+
foo: 2,
25+
};"
26+
`);
27+
28+
mod.exports.named ||= [];
29+
mod.exports.named.push("a");
30+
31+
expect(generate(mod)).toMatchInlineSnapshot(`
32+
"export default {
33+
foo: 2,
34+
};
35+
36+
export const named = [\\"a\\"];"
37+
`);
38+
39+
// delete
40+
delete mod.exports.default;
41+
42+
expect(generate(mod)).toMatchInlineSnapshot(
43+
'"export const named = [\\"a\\"];"'
44+
);
45+
46+
delete mod.exports.named;
47+
48+
expect(generate(mod)).toMatchInlineSnapshot('""');
49+
});
50+
});

test/imports.test.ts

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { expect, it, describe } from "vitest";
2+
import { parseCode } from "../src";
3+
import { generate } from "./_utils";
4+
5+
describe("imports", () => {
6+
it("manipulate imports", () => {
7+
const mod = parseCode(`
8+
import { defineConfig, Plugin } from 'vite'
9+
import Vue from '@vitejs/plugin-vue'
10+
import * as path from 'path'
11+
12+
export default defineConfig({
13+
foo: []
14+
})`);
15+
16+
expect(mod.exports.default.$args[0]).toMatchInlineSnapshot(`
17+
{
18+
"foo": [],
19+
}
20+
`);
21+
expect(mod.imports).toMatchInlineSnapshot(`
22+
{
23+
"Plugin": {
24+
"from": "vite",
25+
"imported": "Plugin",
26+
"local": "Plugin",
27+
},
28+
"Vue": {
29+
"from": "@vitejs/plugin-vue",
30+
"imported": "default",
31+
"local": "Vue",
32+
},
33+
"defineConfig": {
34+
"from": "vite",
35+
"imported": "defineConfig",
36+
"local": "defineConfig",
37+
},
38+
"path": {
39+
"from": "path",
40+
"imported": "*",
41+
"local": "path",
42+
},
43+
}
44+
`);
45+
46+
expect(mod.imports.path).toMatchInlineSnapshot(`
47+
{
48+
"from": "path",
49+
"imported": "*",
50+
"local": "path",
51+
}
52+
`);
53+
54+
mod.imports.path.local = "path2";
55+
mod.imports.Vue.local = "VuePlugin";
56+
57+
delete mod.imports.Plugin;
58+
59+
expect(generate(mod)).toMatchInlineSnapshot(`
60+
"import { defineConfig } from \\"vite\\";
61+
import VuePlugin from \\"@vitejs/plugin-vue\\";
62+
import * as path2 from \\"path\\";
63+
64+
export default defineConfig({
65+
foo: [],
66+
});"
67+
`);
68+
69+
expect(generate(mod)).toMatchInlineSnapshot(`
70+
"import { defineConfig } from \\"vite\\";
71+
import VuePlugin from \\"@vitejs/plugin-vue\\";
72+
import * as path2 from \\"path\\";
73+
74+
export default defineConfig({
75+
foo: [],
76+
});"
77+
`);
78+
79+
mod.imports.$add({
80+
from: "foo",
81+
imported: "default",
82+
local: "Foo",
83+
});
84+
mod.imports.$add({
85+
from: "star",
86+
imported: "*",
87+
local: "Star",
88+
});
89+
mod.imports.$add({
90+
from: "vite",
91+
imported: "Good",
92+
});
93+
94+
expect(generate(mod)).toMatchInlineSnapshot(`
95+
"import * as Star from \\"star\\";
96+
import Foo from \\"foo\\";
97+
import { defineConfig, Good } from \\"vite\\";
98+
import VuePlugin from \\"@vitejs/plugin-vue\\";
99+
import * as path2 from \\"path\\";
100+
101+
export default defineConfig({
102+
foo: [],
103+
});"
104+
`);
105+
106+
mod.imports.defineConfig.from = "vitest/config";
107+
108+
expect(generate(mod)).toMatchInlineSnapshot(`
109+
"import { defineConfig } from \\"vitest/config\\";
110+
import * as Star from \\"star\\";
111+
import Foo from \\"foo\\";
112+
import { Good } from \\"vite\\";
113+
import VuePlugin from \\"@vitejs/plugin-vue\\";
114+
import * as path2 from \\"path\\";
115+
116+
export default defineConfig({
117+
foo: [],
118+
});"
119+
`);
120+
});
121+
});

test/index.test.ts

Lines changed: 2 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { expect, it, describe } from "vitest";
2-
import { format } from "prettier";
3-
import { parseCode, generateCode } from "../src";
4-
5-
function generate(mod: any) {
6-
return format(generateCode(mod).code, { parser: "babel-ts" }).trim();
7-
}
2+
import { parseCode } from "../src";
3+
import { generate } from "./_utils";
84

95
describe("magicast", () => {
106
it("basic object and array", () => {
@@ -205,165 +201,4 @@ describe("magicast", () => {
205201
}
206202
`);
207203
});
208-
209-
it("manipulate exports", () => {
210-
const mod = parseCode("");
211-
212-
expect(mod.exports).toMatchInlineSnapshot(`{}`);
213-
expect(generate(mod)).toMatchInlineSnapshot('""');
214-
215-
mod.exports.default = { foo: "1" };
216-
217-
expect(generate(mod)).toMatchInlineSnapshot(`
218-
"export default {
219-
foo: \\"1\\",
220-
};"
221-
`);
222-
223-
mod.exports.default.foo = 2;
224-
225-
expect(generate(mod)).toMatchInlineSnapshot(`
226-
"export default {
227-
foo: 2,
228-
};"
229-
`);
230-
231-
mod.exports.named ||= [];
232-
mod.exports.named.push("a");
233-
234-
expect(generate(mod)).toMatchInlineSnapshot(`
235-
"export default {
236-
foo: 2,
237-
};
238-
239-
export const named = [\\"a\\"];"
240-
`);
241-
242-
// delete
243-
delete mod.exports.default;
244-
245-
expect(generate(mod)).toMatchInlineSnapshot(
246-
'"export const named = [\\"a\\"];"'
247-
);
248-
249-
delete mod.exports.named;
250-
251-
expect(generate(mod)).toMatchInlineSnapshot('""');
252-
});
253-
254-
it("manipulate imports", () => {
255-
const mod = parseCode(`
256-
import { defineConfig, Plugin } from 'vite'
257-
import Vue from '@vitejs/plugin-vue'
258-
import * as path from 'path'
259-
260-
export default defineConfig({
261-
foo: []
262-
})`);
263-
264-
expect(mod.exports.default.$args[0]).toMatchInlineSnapshot(`
265-
{
266-
"foo": [],
267-
}
268-
`);
269-
expect(mod.imports).toMatchInlineSnapshot(`
270-
{
271-
"Plugin": {
272-
"from": "vite",
273-
"imported": "Plugin",
274-
"local": "Plugin",
275-
},
276-
"Vue": {
277-
"from": "@vitejs/plugin-vue",
278-
"imported": "default",
279-
"local": "Vue",
280-
},
281-
"defineConfig": {
282-
"from": "vite",
283-
"imported": "defineConfig",
284-
"local": "defineConfig",
285-
},
286-
"path": {
287-
"from": "path",
288-
"imported": "*",
289-
"local": "path",
290-
},
291-
}
292-
`);
293-
294-
expect(mod.imports.path).toMatchInlineSnapshot(`
295-
{
296-
"from": "path",
297-
"imported": "*",
298-
"local": "path",
299-
}
300-
`);
301-
302-
mod.imports.path.local = "path2";
303-
mod.imports.Vue.local = "VuePlugin";
304-
305-
delete mod.imports.Plugin;
306-
307-
expect(generate(mod)).toMatchInlineSnapshot(`
308-
"import { defineConfig } from \\"vite\\";
309-
import VuePlugin from \\"@vitejs/plugin-vue\\";
310-
import * as path2 from \\"path\\";
311-
312-
export default defineConfig({
313-
foo: [],
314-
});"
315-
`);
316-
317-
expect(generate(mod)).toMatchInlineSnapshot(`
318-
"import { defineConfig } from \\"vite\\";
319-
import VuePlugin from \\"@vitejs/plugin-vue\\";
320-
import * as path2 from \\"path\\";
321-
322-
export default defineConfig({
323-
foo: [],
324-
});"
325-
`);
326-
327-
mod.imports.$add({
328-
from: "foo",
329-
imported: "default",
330-
local: "Foo",
331-
});
332-
mod.imports.$add({
333-
from: "star",
334-
imported: "*",
335-
local: "Star",
336-
});
337-
mod.imports.$add({
338-
from: "vite",
339-
imported: "Good",
340-
});
341-
342-
expect(generate(mod)).toMatchInlineSnapshot(`
343-
"import * as Star from \\"star\\";
344-
import Foo from \\"foo\\";
345-
import { defineConfig, Good } from \\"vite\\";
346-
import VuePlugin from \\"@vitejs/plugin-vue\\";
347-
import * as path2 from \\"path\\";
348-
349-
export default defineConfig({
350-
foo: [],
351-
});"
352-
`);
353-
354-
mod.imports.defineConfig.from = "vitest/config";
355-
356-
expect(generate(mod)).toMatchInlineSnapshot(`
357-
"import { defineConfig } from \\"vitest/config\\";
358-
import * as Star from \\"star\\";
359-
import Foo from \\"foo\\";
360-
import { Good } from \\"vite\\";
361-
import VuePlugin from \\"@vitejs/plugin-vue\\";
362-
import * as path2 from \\"path\\";
363-
364-
export default defineConfig({
365-
foo: [],
366-
});"
367-
`);
368-
});
369204
});

0 commit comments

Comments
 (0)