Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lobsterkatie committed Dec 3, 2022
1 parent 707d0b0 commit b8239c7
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ out
coverage
test/**/dist
test/**/actual.js
test/unit/cjs-querystring/who?what?idk!.js
55 changes: 45 additions & 10 deletions test/unit.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
const fs = require('fs');
const { join, relative } = require('path');
const { join, relative, sep } = require('path');
const { nodeFileTrace } = require('../out/node-file-trace');

global._unit = true;

const skipOnWindows = ['yarn-workspaces', 'yarn-workspaces-base-root', 'yarn-workspace-esm', 'asset-symlink', 'require-symlink'];
const skipOnWindows = ['yarn-workspaces', 'yarn-workspaces-base-root', 'yarn-workspace-esm', 'asset-symlink', 'require-symlink', 'cjs-querystring'];
const unitTestDirs = fs.readdirSync(join(__dirname, 'unit'));
const unitTests = [
...unitTestDirs.map(testName => ({testName, isRoot: false})),
...unitTestDirs.map(testName => ({testName, isRoot: true})),
...unitTestDirs.map(testName => ({testName, isRoot: false})),
];

for (const { testName, isRoot } of unitTests) {
const testSuffix = `${testName} from ${isRoot ? 'root' : 'cwd'}`;
if (process.platform === 'win32' && (isRoot || skipOnWindows.includes(testName))) {
console.log(`Skipping unit test on Windows: ${testSuffix}`);
continue;
};
const unitPath = join(__dirname, 'unit', testName);

if (process.platform === 'win32') {
if (isRoot || skipOnWindows.includes(testName)) {
console.log(`Skipping unit test on Windows: ${testSuffix}`);
continue;
}
} else {
if (testName === 'cjs-querystring') {
// Create (a git-ignored copy of) the file we need, since committing it
// breaks CI on Windows. See https://github.com/vercel/nft/pull/322.
const currentFilepath = join(unitPath, 'noPunctuation', 'whowhatidk.js');
const newFilepath = currentFilepath.replace(
'noPunctuation' + sep + 'whowhatidk.js',
'who?what?idk!.js'
);
if (!fs.existsSync(newFilepath)) {
fs.copyFileSync(currentFilepath, newFilepath);
}
}
}

it(`should correctly trace ${testSuffix}`, async () => {

Expand All @@ -41,6 +57,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 All @@ -67,8 +102,8 @@ for (const { testName, isRoot } of unitTests) {
if (testName === 'multi-input') {
inputFileNames.push('input-2.js', 'input-3.js', 'input-4.js');
}
const { fileList, reasons } = await nodeFileTrace(

const { fileList, reasons, warnings } = await nodeFileTrace(
inputFileNames.map(file => join(unitPath, file)),
{
base: isRoot ? '/' : `${__dirname}/../`,
Expand Down Expand Up @@ -193,7 +228,7 @@ for (const { testName, isRoot } of unitTests) {
expect(sortedFileList).toEqual(expected);
}
catch (e) {
console.warn(reasons);
console.warn({reasons, warnings});
fs.writeFileSync(join(unitPath, 'actual.js'), JSON.stringify(sortedFileList, null, 2));
throw e;
}
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);
12 changes: 12 additions & 0 deletions test/unit/cjs-querystring/noPunctuation/whowhatidk.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!",
},
};
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"
]
4 changes: 4 additions & 0 deletions test/unit/esm-querystring-mjs/animalFacts/aardvark.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { numSpecies } from "./bear.mjs?beaver?bison";
console.log(`There are ${numSpecies} species of bears.`);

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

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

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

console.log(`Aardvarks eat ${aardvark.food}.`);
7 changes: 7 additions & 0 deletions test/unit/esm-querystring-mjs/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
"test/unit/esm-querystring-mjs/animalFacts/aardvark.mjs",
"test/unit/esm-querystring-mjs/animalFacts/bear.mjs",
"test/unit/esm-querystring-mjs/animalFacts/cheetah.mjs",
"test/unit/esm-querystring-mjs/input.js",
"test/unit/esm-querystring-mjs/package.json"
]
4 changes: 4 additions & 0 deletions test/unit/esm-querystring-mjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"type": "module"
}
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 @@
[
"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",
"test/unit/esm-querystring/package.json"
]
4 changes: 4 additions & 0 deletions test/unit/esm-querystring/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"private": true,
"type": "module"
}
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 b8239c7

Please sign in to comment.