Skip to content

Commit

Permalink
Add more tests to boost coverage and cleanup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dhkatz committed Aug 23, 2019
1 parent 64b2c8c commit ed52e82
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 27 deletions.
42 changes: 17 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,15 @@ function resolveImports(file: ReadonlyArray<string>, imports: FileData[], option

const aliases: { [key: string]: string[] | undefined } = {};
for (const alias in paths) {
if (!paths.hasOwnProperty(alias)) {
continue;
}
/* istanbul ignore else */
if (paths.hasOwnProperty(alias)) {
let resolved = alias;
if (alias.endsWith('/*')) {
resolved = alias.replace('/*', '/');
}

let resolved = alias;
if (alias.endsWith('/*')) {
resolved = alias.replace('/*', '/');
aliases[resolved] = paths[alias];
}

aliases[resolved] = paths[alias];
}

const lines: string[] = [...file];
Expand All @@ -87,28 +86,21 @@ function resolveImports(file: ReadonlyArray<string>, imports: FileData[], option

let resolved: string = '';
for (const alias in aliases) {
if (!aliases.hasOwnProperty(alias)) {
continue;
}
/* istanbul ignore else */
if (aliases.hasOwnProperty(alias) && imported.import.startsWith(alias)) {
const choices: string[] | undefined = aliases[alias];

if (!imported.import.startsWith(alias)) {
continue;
}
if (choices != undefined) {
resolved = choices[0];
if (resolved.endsWith('/*')) {
resolved = resolved.replace('/*', '/');
}

const choices: string[] | undefined = aliases[alias];
resolved = imported.import.replace(alias, resolved);

if (choices !== undefined && choices !== null) {
resolved = choices[0];
if (resolved.endsWith('/*')) {
resolved = resolved.replace('/*', '/');
break;
}

resolved = imported.import.replace(alias, resolved);
} else {
continue;
}

break;
}

if (resolved.length < 1) {
Expand Down
65 changes: 63 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const compilerOptions: CompilerOptions = { paths: { 'MyAlias': ['MyAliasFolder/M

const run = async (test: TestCase): Promise<void> => {
return new Promise<void>((resolve, reject) => {
const input = new File({ contents: Buffer.from(test.input), path: test.path });
const input = new File({ contents: test.input == null ? null : Buffer.from(test.input), path: test.path });
const stream = ObjectStream.fromArray([input]);

stream
.pipe(plugin(test.pluginOptions))
.pipe(ObjectStream.transform({
onEntered: (args: EnteredArgs<File, void>) => {
try {
expect(args.object.contents.toString()).toEqual(test.expected);
expect(args.object.contents ? args.object.contents.toString() : args.object.contents).toEqual(test.expected);
} catch (error) {
reject(error);
}
Expand Down Expand Up @@ -69,6 +69,23 @@ import C from "../../MyAliasFolder/MyAliasClass";
});
});

it('should work with no baseUrl', async () => {
return run({
pluginOptions: { configuration: { paths: { 'MyAlias': ['MyAliasFolder/MyAliasClass'] } } },
path: './src/FileFolder/InnerFileFolder/File.ts',
input: `
import A from "./asdf";
import B from "./MyAlias";
import C from "MyAlias";
`,
expected: `
import A from "./asdf";
import B from "./MyAlias";
import C from "../../../MyAliasFolder/MyAliasClass";
`
});
});

it('should support dynamic imports', async () => {
return run({
pluginOptions: { configuration: compilerOptions },
Expand Down Expand Up @@ -99,6 +116,41 @@ const C = require("../../MyAliasFolder/MyAliasClass");
});
});

it('should pass files with no import aliases', async () => {
return run({
pluginOptions: { configuration: compilerOptions },
path: './src/FileFolder/InnerFileFolder/File.ts',
input: `
const A = require("./asdf");
const B = require("./MyAlias");
const C = require("test");
`,
expected: `
const A = require("./asdf");
const B = require("./MyAlias");
const C = require("test");
`
});
});

it('should pass empty files', async () => {
return run({
pluginOptions: { configuration: compilerOptions },
path: './src/FileFolder/InnerFileFolder/File.ts',
input: '',
expected: ''
});
});

it('should pass null files', async () => {
return run({
pluginOptions: { configuration: compilerOptions },
path: './src/FileFolder/InnerFileFolder/File.ts',
input: null,
expected: null
});
});

it('should throw with multiple imports on one line', async () => {
return expect(run({
pluginOptions: { configuration: { compilerOptions } },
Expand Down Expand Up @@ -128,3 +180,12 @@ it('should throw with no path', async () => {
expected: '',
})).rejects.toThrow()
});

it('should throw with no paths in compilerOptions', async () => {
await expect(run({
pluginOptions: { configuration: { paths: undefined! } },
path: './src/FileFolder/InnerFileFolder/File.ts',
input: '',
expected: '',
})).rejects.toThrow()
});

0 comments on commit ed52e82

Please sign in to comment.