Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
fix: add more test case for import_map. ref #132
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Apr 9, 2020
1 parent 8f5ddf1 commit e4b1d6a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
3 changes: 3 additions & 0 deletions __test__/import_maps/empty_import_map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"imports": {}
}
3 changes: 3 additions & 0 deletions __test__/import_maps/import_map_not_valid_field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"imports": []
}
39 changes: 35 additions & 4 deletions core/import_map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const TEST_DIR = path.join(__dirname, "..", "__test__");
test("core / import_map", async () => {
const mockWorkspaceDir = path.join(TEST_DIR, "import_maps");

const invalidImportMapFilepath = path.join(mockWorkspaceDir, "invalid.json");
const validImportMapFilepath = path.join(mockWorkspaceDir, "import_map.json");

expect(ImportMap.create(validImportMapFilepath).toJSON()).toEqual({
Expand All @@ -29,18 +28,50 @@ test("core / import_map", async () => {
path.join(mockWorkspaceDir, "src", "relative", "path", "to", "mod.ts")
);

expect(ImportMap.create(invalidImportMapFilepath).toJSON()).toEqual({});

const importMap = await ImportMap.create(validImportMapFilepath);

for (const [prefix, moduleName] of importMap) {
expect(typeof prefix).toBe("string");
expect(typeof moduleName).toBe("string");
expect(prefix).not.toEqual(moduleName);
}
});

// if import_map not exist
test("core / import_map if import_map file not exist", async () => {
expect(
ImportMap.create(path.join(__dirname, "path", "not", "exist")).toJSON()
).toEqual({});
});

test("core / import_map if invalid import_map", async () => {
const mockWorkspaceDir = path.join(TEST_DIR, "import_maps");

const filepath = path.join(mockWorkspaceDir, "invalid.json");

const importMap = ImportMap.create(filepath);

expect(importMap.toJSON()).toEqual({});
});

test("core / import_map if it is empty", async () => {
const mockWorkspaceDir = path.join(TEST_DIR, "import_maps");

const filepath = path.join(mockWorkspaceDir, "empty_import_map.json");

const importMap = ImportMap.create(filepath);

expect(importMap.toJSON()).toEqual({});
});

test("core / import_map if imports field not an Object", async () => {
const mockWorkspaceDir = path.join(TEST_DIR, "import_maps");

const filepath = path.join(
mockWorkspaceDir,
"import_map_not_valid_field.json"
);

const importMap = ImportMap.create(filepath);

expect(importMap.toJSON()).toEqual({});
});
9 changes: 9 additions & 0 deletions core/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ export class ImportMap implements ImportMapInterface {

try {
importMap = JSON.parse(importMapContent || "");

// Make sure `importMap.imports` is a key-value object
// Otherwise, an exception may be thrown
if (
Object.prototype.toString.call(importMap.imports) !==
"[object Object]"
) {
importMap.imports = {};
}
} catch {
importMap.imports = {};
}
Expand Down

0 comments on commit e4b1d6a

Please sign in to comment.