Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Nov 17, 2022
1 parent 6133dd9 commit cc7e76d
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 0 deletions.
19 changes: 19 additions & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ for (const { testName, isRoot } of unitTests) {
return null
}
}

// mock an in-memory module store (such as webpack's) where the same filename with
// two different querystrings can correspond to two different modules, one importing
// the other
if (testName === "querystring-self-import") {
if (id.endsWith("input.js") || id.endsWith("base.js") || id.endsWith("dep.js")) {
return fs.readFileSync(id).toString()
}

if (id.endsWith("base.js?__withQuery")) {
return `
import * as origBase from './base';
export const dogs = origBase.dogs.concat('Cory', 'Bodhi');
export const cats = origBase.cats.concat('Teaberry', 'Sassafras', 'Persephone');
export const rats = origBase.rats;
`;
}
}

return this.constructor.prototype.readFile.apply(this, arguments);
});

Expand Down
7 changes: 7 additions & 0 deletions test/unit/cjs-querystring/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Test that CJS files treat question marks in filenames as any other character,
// matching Node behavior

// https://www.youtube.com/watch?v=2ve20PVNZ18

const baseball = require("./who?what?idk!");
console.log(baseball.players);
5 changes: 5 additions & 0 deletions test/unit/cjs-querystring/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"package.json",
"test/unit/cjs-querystring/input.js",
"test/unit/cjs-querystring/who?what?idk!.js"
]
12 changes: 12 additions & 0 deletions test/unit/cjs-querystring/who?what?idk!.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
players: {
first: "Who",
second: "What",
third: "I Don't Know",
left: "Why",
center: "Because",
pitcher: "Tomorrow",
catcher: "Today",
shortstop: "I Don't Give a Damn!",
},
};
4 changes: 4 additions & 0 deletions test/unit/esm-querystring/animalFacts/aardvark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { numSpecies } from "./bear?beaver?bison";
console.log(`There are ${numSpecies} species of bears.`);

export const food = "termites";
4 changes: 4 additions & 0 deletions test/unit/esm-querystring/animalFacts/bear.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as cheetah from "./cheetah?cow=chipmunk";
console.log(`Cheetahs can run ${cheetah.topSpeed} mph.`);

export const numSpecies = 8;
1 change: 1 addition & 0 deletions test/unit/esm-querystring/animalFacts/cheetah.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const topSpeed = 65;
5 changes: 5 additions & 0 deletions test/unit/esm-querystring/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Test that querystrings of various forms get stripped from esm imports

import * as aardvark from "./animalFacts/aardvark?anteater";

console.log(`Aardvarks eat ${aardvark.food}.`);
7 changes: 7 additions & 0 deletions test/unit/esm-querystring/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"package.json",
"test/unit/esm-querystring/animalFacts/aardvark.js",
"test/unit/esm-querystring/animalFacts/bear.js",
"test/unit/esm-querystring/animalFacts/cheetah.js",
"test/unit/esm-querystring/input.js"
]
5 changes: 5 additions & 0 deletions test/unit/querystring-self-import/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as dep from "./dep";

export const dogs = ["Charlie", "Maisey"];
export const cats = ["Piper"];
export const rats = dep.rats;
1 change: 1 addition & 0 deletions test/unit/querystring-self-import/dep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const rats = ["Debra"];
10 changes: 10 additions & 0 deletions test/unit/querystring-self-import/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Test that if a file and the same file with a querystring correspond to different
// modules in memory, one can successfully import the other. The import chain
// goes `input` (this file) -> `base?__withQuery` -> `base` -> `dep`, which means
// that if `dep` shows up in `output`, we know that both `base?__withQuery` and
// `base` have been loaded successfully.

import * as baseWithQuery from "./base?__withQuery";
console.log("Dogs:", baseWithQuery.dogs);
console.log("Cats:", baseWithQuery.cats);
console.log("Rats:", baseWithQuery.rats);
6 changes: 6 additions & 0 deletions test/unit/querystring-self-import/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"package.json",
"test/unit/querystring-self-import/base.js",
"test/unit/querystring-self-import/dep.js",
"test/unit/querystring-self-import/input.js"
]

0 comments on commit cc7e76d

Please sign in to comment.