Skip to content

Commit

Permalink
refactor: break down test files
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 25, 2023
1 parent 4a286e2 commit 5af3f8c
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 140 deletions.
4 changes: 2 additions & 2 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export class MagicastError extends Error {
this.rawMessage = message;
this.options = options;

if (options?.ast && options?.code) {
if (options?.ast && options?.code && options.ast.loc) {
// construct a code frame point to the start of the ast node
const { line, column } = options.ast.loc!.start;
const { line, column } = options.ast.loc.start;
const lines = options.code.split("\n");
const start = Math.max(0, line - 3);
const end = Math.min(lines.length, line + 3);
Expand Down
2 changes: 2 additions & 0 deletions src/proxy/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function creatImportProxy(
root: Program
): ProxifiedImportItem {
if (_importProxyCache.has(specifier)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return _importProxyCache.get(specifier)!;
}
const proxy = createProxy(
Expand Down Expand Up @@ -114,6 +115,7 @@ export function creatImportProxy(

export function createImportsProxy(
root: Program,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
mod: ProxifiedModule
): ProxifiedImportsMap {
// TODO: cache
Expand Down
56 changes: 56 additions & 0 deletions test/array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, expect, it } from "vitest";
import { parseCode } from "../src";
import { generate } from "./_utils";

describe("array", () => {
it("array operations", () => {
const mod = parseCode(`export default [1, 2, 3, 4, 5]`);

expect(mod.exports.default.length).toBe(5);
expect(mod.exports.default.includes(5)).toBe(true);
expect(mod.exports.default.includes(6)).toBe(false);

const deleted = mod.exports.default.splice(1, 3, { foo: "bar" }, "bar");

expect(deleted).toEqual([2, 3, 4]);

expect(generate(mod)).toMatchInlineSnapshot(
`
"export default [
1,
{
foo: \\"bar\\",
},
\\"bar\\",
5,
];"
`
);

const foundIndex = mod.exports.default.findIndex(
(item) => item.foo === "bar"
);
const found = mod.exports.default.find((item) => item.foo === "bar");

expect(foundIndex).toBe(1);
expect(found).toMatchInlineSnapshot(`
{
"foo": "bar",
}
`);
});

it("array should be iterable", () => {
const mod = parseCode(`
export const config = {
array: ['a']
}
`);
const arr = [...mod.exports.config.array];
expect(arr).toMatchInlineSnapshot(`
[
"a",
]
`);
});
});
2 changes: 1 addition & 1 deletion test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, describe, it } from "vitest";
import { parseCode } from "../src";
import { generate } from "./_utils";

describe("magicast", () => {
describe("errors", () => {
it("ternary", () => {
const mod = parseCode(
`
Expand Down
2 changes: 1 addition & 1 deletion test/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, it, describe } from "vitest";
import { CodeFormatOptions, detectCodeFormat } from "../src";

describe("magicast:style", () => {
describe("format", () => {
const cases: Array<{
name: string;
code: string;
Expand Down
41 changes: 41 additions & 0 deletions test/function-call.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";
import { parseCode } from "../src";
import { generate } from "./_utils";

describe("function-calls", () => {
it("function wrapper", () => {
const mod = parseCode(`
export const a: any = { foo: 1}
export default defineConfig({
// Modules
modules: ["a"]
})
`);

expect(mod.exports.a.foo).toBe(1);
expect(mod.exports.default.$type).toBe("function-call");
expect(mod.exports.default.$callee).toBe("defineConfig");
expect(mod.exports.default.$args).toMatchInlineSnapshot(`
[
{
"modules": [
"a",
],
},
]
`);

const options = mod.exports.default.$args[0];

options.modules ||= [];
options.modules.push("b");

expect(generate(mod)).toMatchInlineSnapshot(`
"export const a: any = { foo: 1 };
export default defineConfig({
// Modules
modules: [\\"a\\", \\"b\\"],
});"
`);
});
});
137 changes: 1 addition & 136 deletions test/basic.test.ts → test/general.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, it, describe } from "vitest";
import { generateCode, parseCode } from "../src";
import { generate } from "./_utils";

describe("magicast", () => {
describe("general", () => {
it("basic object and array", () => {
const mod = parseCode(`export default { a: 1, b: { c: {} } }`);

Expand Down Expand Up @@ -107,42 +107,6 @@ describe("magicast", () => {
);
});

it("function wrapper", () => {
const mod = parseCode(`
export const a: any = { foo: 1}
export default defineConfig({
// Modules
modules: ["a"]
})
`);

expect(mod.exports.a.foo).toBe(1);
expect(mod.exports.default.$type).toBe("function-call");
expect(mod.exports.default.$callee).toBe("defineConfig");
expect(mod.exports.default.$args).toMatchInlineSnapshot(`
[
{
"modules": [
"a",
],
},
]
`);

const options = mod.exports.default.$args[0];

options.modules ||= [];
options.modules.push("b");

expect(generate(mod)).toMatchInlineSnapshot(`
"export const a: any = { foo: 1 };
export default defineConfig({
// Modules
modules: [\\"a\\", \\"b\\"],
});"
`);
});

it("delete property", () => {
const mod = parseCode(`export default { a: 1, b: [1, { foo: 'bar' }] }`);

Expand All @@ -165,43 +129,6 @@ describe("magicast", () => {
`);
});

it("array operations", () => {
const mod = parseCode(`export default [1, 2, 3, 4, 5]`);

expect(mod.exports.default.length).toBe(5);
expect(mod.exports.default.includes(5)).toBe(true);
expect(mod.exports.default.includes(6)).toBe(false);

const deleted = mod.exports.default.splice(1, 3, { foo: "bar" }, "bar");

expect(deleted).toEqual([2, 3, 4]);

expect(generate(mod)).toMatchInlineSnapshot(
`
"export default [
1,
{
foo: \\"bar\\",
},
\\"bar\\",
5,
];"
`
);

const foundIndex = mod.exports.default.findIndex(
(item) => item.foo === "bar"
);
const found = mod.exports.default.find((item) => item.foo === "bar");

expect(foundIndex).toBe(1);
expect(found).toMatchInlineSnapshot(`
{
"foo": "bar",
}
`);
});

it("should preserve code styles", () => {
const mod = parseCode(`
export const config = {
Expand All @@ -215,66 +142,4 @@ describe("magicast", () => {
}"
`);
});

it("array should be iterable", () => {
const mod = parseCode(`
export const config = {
array: ['a']
}
`);
const arr = [...mod.exports.config.array];
expect(arr).toMatchInlineSnapshot(`
[
"a",
]
`);
});

it("object property", () => {
const mod = parseCode(
`
export default {
foo: {
['a']: 1,
['a-b']: 2,
foo() {}
}
}
`.trim()
);

expect(mod.exports.default.foo.a).toBe(1);
expect(mod.exports.default.foo["a-b"]).toBe(2);
expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(`
[
"a",
"a-b",
"foo",
]
`);

mod.exports.default.foo["a-b-c"] = 3;

expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(`
[
"a",
"a-b",
"foo",
"a-b-c",
]
`);

mod.exports.default.foo["a-b"] = "updated";

expect(generate(mod)).toMatchInlineSnapshot(`
"export default {
foo: {
[\\"a\\"]: 1,
[\\"a-b\\"]: \\"updated\\",
foo() {},
\\"a-b-c\\": 3,
},
};"
`);
});
});
53 changes: 53 additions & 0 deletions test/object.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, expect, it } from "vitest";
import { parseCode } from "../src";
import { generate } from "./_utils";

describe("object", () => {
it("object property", () => {
const mod = parseCode(
`
export default {
foo: {
['a']: 1,
['a-b']: 2,
foo() {}
}
}
`.trim()
);

expect(mod.exports.default.foo.a).toBe(1);
expect(mod.exports.default.foo["a-b"]).toBe(2);
expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(`
[
"a",
"a-b",
"foo",
]
`);

mod.exports.default.foo["a-b-c"] = 3;

expect(Object.keys(mod.exports.default.foo)).toMatchInlineSnapshot(`
[
"a",
"a-b",
"foo",
"a-b-c",
]
`);

mod.exports.default.foo["a-b"] = "updated";

expect(generate(mod)).toMatchInlineSnapshot(`
"export default {
foo: {
[\\"a\\"]: 1,
[\\"a-b\\"]: \\"updated\\",
foo() {},
\\"a-b-c\\": 3,
},
};"
`);
});
});

0 comments on commit 5af3f8c

Please sign in to comment.