Skip to content

Commit cc7e76d

Browse files
committed
add tests
1 parent 6133dd9 commit cc7e76d

File tree

13 files changed

+86
-0
lines changed

13 files changed

+86
-0
lines changed

test/unit.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ for (const { testName, isRoot } of unitTests) {
4141
return null
4242
}
4343
}
44+
45+
// mock an in-memory module store (such as webpack's) where the same filename with
46+
// two different querystrings can correspond to two different modules, one importing
47+
// the other
48+
if (testName === "querystring-self-import") {
49+
if (id.endsWith("input.js") || id.endsWith("base.js") || id.endsWith("dep.js")) {
50+
return fs.readFileSync(id).toString()
51+
}
52+
53+
if (id.endsWith("base.js?__withQuery")) {
54+
return `
55+
import * as origBase from './base';
56+
export const dogs = origBase.dogs.concat('Cory', 'Bodhi');
57+
export const cats = origBase.cats.concat('Teaberry', 'Sassafras', 'Persephone');
58+
export const rats = origBase.rats;
59+
`;
60+
}
61+
}
62+
4463
return this.constructor.prototype.readFile.apply(this, arguments);
4564
});
4665

test/unit/cjs-querystring/input.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test that CJS files treat question marks in filenames as any other character,
2+
// matching Node behavior
3+
4+
// https://www.youtube.com/watch?v=2ve20PVNZ18
5+
6+
const baseball = require("./who?what?idk!");
7+
console.log(baseball.players);

test/unit/cjs-querystring/output.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
"package.json",
3+
"test/unit/cjs-querystring/input.js",
4+
"test/unit/cjs-querystring/who?what?idk!.js"
5+
]
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
players: {
3+
first: "Who",
4+
second: "What",
5+
third: "I Don't Know",
6+
left: "Why",
7+
center: "Because",
8+
pitcher: "Tomorrow",
9+
catcher: "Today",
10+
shortstop: "I Don't Give a Damn!",
11+
},
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { numSpecies } from "./bear?beaver?bison";
2+
console.log(`There are ${numSpecies} species of bears.`);
3+
4+
export const food = "termites";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import * as cheetah from "./cheetah?cow=chipmunk";
2+
console.log(`Cheetahs can run ${cheetah.topSpeed} mph.`);
3+
4+
export const numSpecies = 8;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const topSpeed = 65;

test/unit/esm-querystring/input.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Test that querystrings of various forms get stripped from esm imports
2+
3+
import * as aardvark from "./animalFacts/aardvark?anteater";
4+
5+
console.log(`Aardvarks eat ${aardvark.food}.`);

test/unit/esm-querystring/output.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
"package.json",
3+
"test/unit/esm-querystring/animalFacts/aardvark.js",
4+
"test/unit/esm-querystring/animalFacts/bear.js",
5+
"test/unit/esm-querystring/animalFacts/cheetah.js",
6+
"test/unit/esm-querystring/input.js"
7+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as dep from "./dep";
2+
3+
export const dogs = ["Charlie", "Maisey"];
4+
export const cats = ["Piper"];
5+
export const rats = dep.rats;
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const rats = ["Debra"];
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Test that if a file and the same file with a querystring correspond to different
2+
// modules in memory, one can successfully import the other. The import chain
3+
// goes `input` (this file) -> `base?__withQuery` -> `base` -> `dep`, which means
4+
// that if `dep` shows up in `output`, we know that both `base?__withQuery` and
5+
// `base` have been loaded successfully.
6+
7+
import * as baseWithQuery from "./base?__withQuery";
8+
console.log("Dogs:", baseWithQuery.dogs);
9+
console.log("Cats:", baseWithQuery.cats);
10+
console.log("Rats:", baseWithQuery.rats);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
"package.json",
3+
"test/unit/querystring-self-import/base.js",
4+
"test/unit/querystring-self-import/dep.js",
5+
"test/unit/querystring-self-import/input.js"
6+
]

0 commit comments

Comments
 (0)