Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESM/CJS import tests #84

Merged
merged 16 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.1",
"flatted": "^3.3.1",
"get-port": "^7.0.0",
"http-mitm-proxy": "^1.1.0",
"make-synchronous": "^1.0.0",
Expand Down
91 changes: 91 additions & 0 deletions test/import.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Type checking is disabled in this file because we want type checking to work without the `/dist/`
// directory being populated.
import { spawn } from "child_process";
import path from "path";
import { fileURLToPath } from "url";

import test from "ava";
import flatted from "flatted";

// Any `require()` statements actually get treated as ESM imports under the hood in the tests, so
// this is a kind of hacky way to perform the module import in a subprocess. Because we're only
// really interested in whether the different builds and import types work in these tests, this
// should be good enough.
const requireCjs = async (
modulePath: string,
): Promise<{ [key: string]: unknown }> => {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const moduleFullPath = path.resolve(__dirname, modulePath);
const subprocessCode = `
// Necessary for the browser tests.
global.window = {};

// Use flatted for better handling of non-JSON content, circular referecnes, etc.
const { stringify } = require("flatted");

const module = require("${moduleFullPath}");
process.stdout.write(stringify(module));
`;

return new Promise((resolve, reject) => {
const subprocess = spawn("node", ["-e", subprocessCode], {
stdio: ["inherit", "pipe", "pipe"],
});

let stdout = "";
subprocess.stdout.on("data", (chunk) => {
stdout += chunk;
});

let stderr = "";
subprocess.stderr.on("data", (chunk) => {
stderr += chunk;
});

subprocess.on("close", (code) => {
if (code === 0) {
try {
const result = flatted.parse(stdout);
resolve(result);
} catch (error) {
reject(new Error(`Failed to parse subprocess output:\n\n${stdout}`));
}
} else {
reject(new Error(`Subprocess exited with code ${code}:\n\n${stderr}`));
}
});
});
};

test.before(() => {
global.window = {} as Window & typeof globalThis;
});

test("cli", async (t) => {
const cli = await requireCjs("../dist/cli/index.js");
t.truthy(cli?.program);
});

test("lib browser cjs", async (t) => {
const lib = await requireCjs("../dist/lib/browser/index.js");
t.truthy(lib);
t.false("default" in lib, "library should not be nested under `default`");
});

test("lib browser mjs", async (t) => {
const { default: lib } = await import("../dist/lib/browser/index.mjs");
t.truthy(lib);
t.false("default" in lib, "library should not be nested under `default`");
});

test("lib node cjs", async (t) => {
const lib = await requireCjs("../dist/lib/index.js");
t.truthy(lib);
t.false("default" in lib, "library should not be nested under `default`");
});

test("lib node mjs", async (t) => {
const { default: lib } = await import("../dist/lib/index.mjs");
t.truthy(lib);
t.false("default" in lib, "library should not be nested under `default`");
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"preserveConstEnums": true
},
"include": ["src/**/*", "test/**/*"],
"exclude": ["dist/**/*", "node_modules/**/*"]
"exclude": ["dist/**/*", "node_modules/**/*", "test/import.test.ts"]
}
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2351,6 +2351,11 @@ flatted@^3.2.9:
resolved "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz"
integrity sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==

flatted@^3.3.1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.3.1.tgz#21db470729a6734d4997002f439cb308987f567a"
integrity sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==

follow-redirects@^1.15.0:
version "1.15.3"
resolved "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz"
Expand Down Expand Up @@ -2966,7 +2971,7 @@ json-stable-stringify@^1.0.2:

json-stringify-safe@^5.0.1:
version "5.0.1"
resolved "https://registry.npmjs.org/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz"
resolved "https://registry.yarnpkg.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb"
integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA==

jsonfile@^6.0.1:
Expand Down