/
index.js
57 lines (49 loc) · 1.74 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const { pathToFileURL } = require("url");
const url = pathToFileURL(
require("path").resolve("./test/cases/esm/import-meta/index.js")
).toString();
const webpackVersion = parseInt(
require("../../../../package.json").version,
10
);
it('typeof import.meta === "object"', () => {
expect(typeof import.meta).toBe("object");
if (typeof import.meta !== "object") require("fail");
});
it('typeof import.meta.url === "string"', () => {
expect(typeof import.meta.url).toBe("string");
if (typeof import.meta.url !== "string") require("fail");
});
it('typeof import.meta.webpack === "number"', () => {
expect(typeof import.meta.webpack).toBe("number");
if (typeof import.meta.webpack !== "number") require("fail");
});
it("should return correct import.meta.url", () => {
expect(import.meta.url).toBe(url);
expect(import.meta["url"]).toBe(url);
expect("my" + import.meta.url).toBe("my" + url);
if (import.meta.url.indexOf("index.js") === -1) require("fail");
});
it("should return correct import.meta.webpack", () => {
expect(import.meta.webpack).toBe(webpackVersion);
if (import.meta.webpack < 5) require("fail");
if (import.meta.webpack >= 5) {
} else {
require("fail");
}
});
it("should return undefined for unknown property", () => {
expect(import.meta.other).toBe(undefined);
if (typeof import.meta.other !== "undefined") require("fail");
expect(() => import.meta.other.other.other).toThrowError();
});
it("should add warning on direct import.meta usage", () => {
expect(Object.keys(import.meta)).toHaveLength(0);
});
it("should support destructuring assignment", () => {
let version, url2, c;
({ webpack: version } = { url: url2 } = { c } = import.meta);
expect(version).toBeTypeOf("number");
expect(url2).toBe(url);
expect(c).toBe(undefined);
});