1
1
import { assert } from "chai" ;
2
2
import { createMatchPathAsync } from "../src/match-path-async" ;
3
- import { join } from "path" ;
3
+ import { join , dirname } from "path" ;
4
4
5
5
describe ( "match-path-async" , ( ) => {
6
6
it ( "should locate path that matches with star and exists" , done => {
@@ -14,106 +14,103 @@ describe("match-path-async", () => {
14
14
( path , callback ) => callback ( undefined , path === existingPath ) ,
15
15
undefined ,
16
16
( _err , result ) => {
17
- assert . equal ( result , existingPath ) ;
17
+ assert . equal ( result , dirname ( existingPath ) ) ;
18
18
done ( ) ;
19
19
}
20
20
) ;
21
21
} ) ;
22
-
23
22
/*
24
23
it("should resolve to correct path when many are specified", () => {
25
24
const matchPath = createMatchPath("/root/", {
26
25
"lib/*": ["foo1/*", "foo2/*", "location/*", "foo3/*"]
27
26
});
27
+ const existingPath = join("/root", "location", "mylib", "index.ts");
28
28
const result = matchPath(
29
- "/root/test.ts",
30
29
"lib/mylib",
31
30
(_: string) => undefined,
32
- (name: string) => name === join("/root", "location", "mylib", "index.ts") ,
31
+ (name: string) => name === existingPath ,
33
32
[".ts"]
34
33
);
35
- assert.equal(result, join("/root", "location", "mylib" ));
34
+ assert.equal(result, dirname(existingPath ));
36
35
});
37
36
38
37
it("should locate path that matches with star and prioritize pattern with longest prefix", () => {
39
38
const matchPath = createMatchPath("/root/", {
40
39
"*": ["location/*"],
41
40
"lib/*": ["location/*"]
42
41
});
42
+ const existingPath1 = join("/root", "location", "lib", "mylib", "index.ts");
43
+ const existingPath2 = join("/root", "location", "mylib", "index.ts");
43
44
const result = matchPath(
44
- "/root/test.ts",
45
45
"lib/mylib",
46
46
undefined,
47
- (name: string) =>
48
- name === join("/root", "location", "lib", "mylib", "index.ts") ||
49
- name === join("/root", "location", "mylib", "index.ts")
47
+ (name: string) => name === existingPath1 || name === existingPath2
50
48
);
51
- assert.equal(result, join("/root", "location", "mylib" ));
49
+ assert.equal(result, dirname(existingPath2 ));
52
50
});
53
51
54
52
it("should locate path that matches with star and exists with extension", () => {
55
53
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
54
+ const existingPath = join("/root", "location", "mylib.myext");
56
55
const result = matchPath(
57
- "/root/test.ts",
58
56
"lib/mylib",
59
57
(_: string) => undefined,
60
- (name: string) => name === join("/root", "location", "mylib.myext") ,
58
+ (name: string) => name === existingPath ,
61
59
[".js", ".myext"]
62
60
);
63
- assert.equal(result, join("/root", "location", "mylib" ));
61
+ assert.equal(result, removeExtension(existingPath ));
64
62
});
65
63
66
64
it("should resolve request with extension specified", () => {
67
65
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
66
+ const existingPath = join("/root", "location", "test.jpg");
68
67
const result = matchPath(
69
- "/root/test.ts",
70
68
"lib/test.jpg",
71
69
(_: string) => undefined,
72
- (name: string) => name === join("/root", "location", "test.jpg") ,
70
+ (name: string) => name === existingPath ,
73
71
[".js", ".myext"]
74
72
);
75
- assert.equal(result, join("/root", "location", "test.jpg") );
73
+ assert.equal(result, existingPath );
76
74
});
77
75
78
76
it("should locate path that matches without star and exists", () => {
79
77
const matchPath = createMatchPath("/root/", {
80
78
"lib/foo": ["location/foo"]
81
79
});
80
+ const existingPath = join("/root", "location", "foo.ts");
82
81
const result = matchPath(
83
- "/root/test.ts",
84
82
"lib/foo",
85
83
(_: string) => undefined,
86
- (name: string) => name === join("/root", "location", "foo.ts")
84
+ (name: string) => name === existingPath
87
85
);
88
- assert.equal(result, join("/root", "location", "foo" ));
86
+ assert.equal(result, removeExtension(existingPath ));
89
87
});
90
88
91
89
it("should resolve to parent folder when filename is in subfolder", () => {
92
90
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
91
+ const existingPath = join("/root", "location", "mylib", "index.ts");
93
92
const result = matchPath(
94
- "/root/subfolder/file.ts",
95
93
"lib/mylib",
96
94
(_: string) => undefined,
97
- (name: string) => name === join("/root", "location", "mylib", "index.ts")
95
+ (name: string) => name === existingPath
98
96
);
99
- assert.equal(result, join("/root", "location", "mylib" ));
97
+ assert.equal(result, dirname(existingPath ));
100
98
});
101
99
102
100
it("should resolve from main field in package.json", () => {
103
101
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
102
+ const existingPath = join("/root", "location", "mylib", "kalle.ts");
104
103
const result = matchPath(
105
- "/root/subfolder/file.ts",
106
104
"lib/mylib",
107
105
(_: string) => ({ main: "./kalle.ts" }),
108
- (name: string) => name === join("/root", "location", "mylib", "kalle.ts")
106
+ (name: string) => name === existingPath
109
107
);
110
- assert.equal(result, join("/root", "location", "mylib", "kalle" ));
108
+ assert.equal(result, removeExtension(existingPath ));
111
109
});
112
110
113
111
it("should resolve from main field in package.json and correctly remove file extension", () => {
114
112
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
115
113
const result = matchPath(
116
- "/root/subfolder/file.js",
117
114
"lib/mylib.js",
118
115
(_: string) => ({ main: "./kalle.js" }),
119
116
(name: string) =>
@@ -123,7 +120,6 @@ describe("match-path-async", () => {
123
120
124
121
// Make sure we escape the "."
125
122
const result2 = matchPath(
126
- "/root/subfolder/file.js",
127
123
"lib/mylibjs",
128
124
(_: string) => ({ main: "./kallejs" }),
129
125
(name: string) =>
@@ -137,25 +133,24 @@ describe("match-path-async", () => {
137
133
138
134
it("should resolve to with the help of baseUrl when not explicitly set", () => {
139
135
const matchPath = createMatchPath("/root/", {});
136
+ const existingPath = join("/root", "mylib", "index.ts");
140
137
const result = matchPath(
141
- "/root/test.ts",
142
138
"mylib",
143
139
(_: string) => undefined,
144
- (name: string) => name === join("/root", "mylib", "index.ts")
140
+ (name: string) => name === existingPath
145
141
);
146
- assert.equal(result, "/root/mylib" );
142
+ assert.equal(result, dirname(existingPath) );
147
143
});
148
144
149
145
it("should not locate path that does not match", () => {
150
146
const matchPath = createMatchPath("/root/", { "lib/*": ["location/*"] });
147
+ const existingPath = join("root", "location", "mylib");
151
148
const result = matchPath(
152
- "/root/asd.ts",
153
149
"mylib",
154
150
(_: string) => undefined,
155
- (name: string) => name === join("root", "location", "mylib")
151
+ (name: string) => name === existingPath
156
152
);
157
153
assert.equal(result, undefined);
158
154
});
159
-
160
155
*/
161
156
} ) ;
0 commit comments