Skip to content

Commit

Permalink
test: add error test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
WanQuanXie committed Aug 15, 2024
1 parent f9258b2 commit 39e083a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
Empty file.
1 change: 1 addition & 0 deletions src/__tests__/fixtures/error/default-import/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import Lucide from "lucide-react-native";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import Lucide, { Star, BookHeart, Trash2 } from "lucide-react-native";
1 change: 1 addition & 0 deletions src/__tests__/fixtures/error/unknown-icon/actual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { Star12345 } from "lucide-react-native";
73 changes: 38 additions & 35 deletions src/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,59 @@
import path from "node:path";
import fs from "node:fs";
import assert from "node:assert";
import { transformFileSync } from "@babel/core";
import plugin from "../index";

describe("lucide-react-native modularized builds", () => {
const fixturesDir = path.join(__dirname, "fixtures");
const errorFixturesDir = path.join(__dirname, "error-fixtures");
const errorFixturesDir = path.join(__dirname, "fixtures", "error");

fs.readdirSync(fixturesDir).map((caseName) => {
const fixtureDir = path.join(fixturesDir, caseName);
const actualFile = path.join(fixtureDir, "actual.js");
const expectedFile = path.join(fixtureDir, "expected.js");
fs.readdirSync(fixturesDir)
.filter((caseName) => caseName !== "error")
.map((caseName) => {
const fixtureDir = path.join(fixturesDir, caseName);
const actualFile = path.join(fixtureDir, "actual.js");
const expectedFile = path.join(fixtureDir, "expected.js");

describe(`should work with ${caseName.split("-").join(" ")}`, () => {
// Programmatically test with the useES option both on and off
it("cjs", () => {
const actual = transformFileSync(actualFile, {
compact: true,
plugins: [plugin],
})?.code?.trim();
const expected = transformFileSync(expectedFile, {
compact: true,
})?.code?.trim();
expect(actual).toBe(expected);
});
describe(`should work with ${caseName.split("-").join(" ")}`, () => {
// Programmatically test with the useES option both on and off
it("cjs", () => {
const actual = transformFileSync(actualFile, {
compact: true,
plugins: [plugin],
})?.code?.trim();
const expected = transformFileSync(expectedFile, {
compact: true,
})?.code?.trim();
expect(actual).toBe(expected);
});

it("esm", () => {
const actual = transformFileSync(actualFile, {
compact: true,
plugins: [[plugin, { useES: true }]],
})?.code;
it("esm", () => {
const actual = transformFileSync(actualFile, {
compact: true,
plugins: [[plugin, { useES: true }]],
})?.code;

// The only difference is that cjs should be replaced with esm. This way, no changes to
// the tests are needed to cover testing of useES.
const expected = transformFileSync(expectedFile, {
compact: true,
})?.code?.replace(/cjs/g, "esm");
expect(actual).toBe(expected);
// The only difference is that cjs should be replaced with esm. This way, no changes to
// the tests are needed to cover testing of useES.
const expected = transformFileSync(expectedFile, {
compact: true,
})?.code?.replace(/cjs/g, "esm");
expect(actual).toBe(expected);
});
});
});
});

fs.readdirSync(errorFixturesDir).map((caseName) => {
const fixtureDir = path.join(fixturesDir, caseName);
const actualFile = path.join(fixtureDir, "actual.js");

it(`should throw an error with ${caseName.split("-").join(" ")}`, () => {
assert.throws(function () {
const actual = transformFileSync(actualFile, {
plugins: [plugin],
})?.code;
describe(`should throw an error with ${caseName.split("-").join(" ")}`, () => {
it(`${caseName.split("-").join(" ")}`, () => {
expect(function () {
transformFileSync(actualFile, {
plugins: [plugin],
})?.code;
}).toThrow();
});
});
});
Expand Down

0 comments on commit 39e083a

Please sign in to comment.