From 10490b42698ebdfa6caf83c20eb13e9aed075075 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Wed, 16 Nov 2022 06:56:55 -0800 Subject: [PATCH] add tests --- test/unit.test.js | 19 +++++++++++++++++++ test/unit/cjs-querystring/input.js | 7 +++++++ test/unit/cjs-querystring/output.js | 5 +++++ test/unit/cjs-querystring/who?what?idk!.js | 12 ++++++++++++ .../esm-querystring/animalFacts/aardvark.js | 4 ++++ test/unit/esm-querystring/animalFacts/bear.js | 4 ++++ .../esm-querystring/animalFacts/cheetah.js | 1 + test/unit/esm-querystring/input.js | 5 +++++ test/unit/esm-querystring/output.js | 7 +++++++ test/unit/querystring-self-import/base.js | 5 +++++ test/unit/querystring-self-import/dep.js | 1 + test/unit/querystring-self-import/input.js | 10 ++++++++++ test/unit/querystring-self-import/output.js | 6 ++++++ 13 files changed, 86 insertions(+) create mode 100644 test/unit/cjs-querystring/input.js create mode 100644 test/unit/cjs-querystring/output.js create mode 100644 test/unit/cjs-querystring/who?what?idk!.js create mode 100644 test/unit/esm-querystring/animalFacts/aardvark.js create mode 100644 test/unit/esm-querystring/animalFacts/bear.js create mode 100644 test/unit/esm-querystring/animalFacts/cheetah.js create mode 100644 test/unit/esm-querystring/input.js create mode 100644 test/unit/esm-querystring/output.js create mode 100644 test/unit/querystring-self-import/base.js create mode 100644 test/unit/querystring-self-import/dep.js create mode 100644 test/unit/querystring-self-import/input.js create mode 100644 test/unit/querystring-self-import/output.js diff --git a/test/unit.test.js b/test/unit.test.js index ce269b38..e5e5acd4 100644 --- a/test/unit.test.js +++ b/test/unit.test.js @@ -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); }); diff --git a/test/unit/cjs-querystring/input.js b/test/unit/cjs-querystring/input.js new file mode 100644 index 00000000..381fd64a --- /dev/null +++ b/test/unit/cjs-querystring/input.js @@ -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); diff --git a/test/unit/cjs-querystring/output.js b/test/unit/cjs-querystring/output.js new file mode 100644 index 00000000..296a204b --- /dev/null +++ b/test/unit/cjs-querystring/output.js @@ -0,0 +1,5 @@ +[ + "package.json", + "test/unit/cjs-querystring/input.js", + "test/unit/cjs-querystring/who?what?idk!.js" +] diff --git a/test/unit/cjs-querystring/who?what?idk!.js b/test/unit/cjs-querystring/who?what?idk!.js new file mode 100644 index 00000000..b85e362c --- /dev/null +++ b/test/unit/cjs-querystring/who?what?idk!.js @@ -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!", + }, +}; diff --git a/test/unit/esm-querystring/animalFacts/aardvark.js b/test/unit/esm-querystring/animalFacts/aardvark.js new file mode 100644 index 00000000..3e92e04f --- /dev/null +++ b/test/unit/esm-querystring/animalFacts/aardvark.js @@ -0,0 +1,4 @@ +import { numSpecies } from "./bear?beaver?bison"; +console.log(`There are ${numSpecies} species of bears.`); + +export const food = "termites"; diff --git a/test/unit/esm-querystring/animalFacts/bear.js b/test/unit/esm-querystring/animalFacts/bear.js new file mode 100644 index 00000000..211edff1 --- /dev/null +++ b/test/unit/esm-querystring/animalFacts/bear.js @@ -0,0 +1,4 @@ +import * as cheetah from "./cheetah?cow=chipmunk"; +console.log(`Cheetahs can run ${cheetah.topSpeed} mph.`); + +export const numSpecies = 8; diff --git a/test/unit/esm-querystring/animalFacts/cheetah.js b/test/unit/esm-querystring/animalFacts/cheetah.js new file mode 100644 index 00000000..836b4ddf --- /dev/null +++ b/test/unit/esm-querystring/animalFacts/cheetah.js @@ -0,0 +1 @@ +export const topSpeed = 65; diff --git a/test/unit/esm-querystring/input.js b/test/unit/esm-querystring/input.js new file mode 100644 index 00000000..869821d8 --- /dev/null +++ b/test/unit/esm-querystring/input.js @@ -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}.`); diff --git a/test/unit/esm-querystring/output.js b/test/unit/esm-querystring/output.js new file mode 100644 index 00000000..e3f714f6 --- /dev/null +++ b/test/unit/esm-querystring/output.js @@ -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" +] diff --git a/test/unit/querystring-self-import/base.js b/test/unit/querystring-self-import/base.js new file mode 100644 index 00000000..5805573a --- /dev/null +++ b/test/unit/querystring-self-import/base.js @@ -0,0 +1,5 @@ +import * as dep from "./dep"; + +export const dogs = ["Charlie", "Maisey"]; +export const cats = ["Piper"]; +export const rats = dep.rats; diff --git a/test/unit/querystring-self-import/dep.js b/test/unit/querystring-self-import/dep.js new file mode 100644 index 00000000..2be58feb --- /dev/null +++ b/test/unit/querystring-self-import/dep.js @@ -0,0 +1 @@ +export const rats = ["Debra"]; diff --git a/test/unit/querystring-self-import/input.js b/test/unit/querystring-self-import/input.js new file mode 100644 index 00000000..6a4b2675 --- /dev/null +++ b/test/unit/querystring-self-import/input.js @@ -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); diff --git a/test/unit/querystring-self-import/output.js b/test/unit/querystring-self-import/output.js new file mode 100644 index 00000000..d6dc6b2c --- /dev/null +++ b/test/unit/querystring-self-import/output.js @@ -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" +]