Files
test-repo/tests/integration/__tests__/bundle.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.8 KiB
JavaScript
Raw Normal View History

2022-04-06 15:41:50 +08:00
import fs from "node:fs";
import path from "node:path";
import createEsmUtils from "esm-utils";
2022-02-09 10:11:46 +08:00
import fastGlob from "fast-glob";
import { projectRoot } from "../env.js";
2022-04-06 16:05:22 +08:00
import createSandBox from "../../config/utils/create-sandbox.cjs";
import * as coreOptions from "../../../src/main/core-options.js";
import codeSamples from "../../../website/playground/codeSamples.mjs";
import jestPathSerializer from "../path-serializer.js";
2020-09-17 01:08:44 +09:00
const { require } = createEsmUtils(import.meta);
expect.addSnapshotSerializer(jestPathSerializer);
2020-09-17 01:08:44 +09:00
const parserNames = coreOptions.options.parser.choices.map(
({ value }) => value
);
const distDirectory = path.join(projectRoot, "dist");
// Files including U+FFEE can't load in Chrome Extension
// `prettier-chrome-extension` https://github.com/prettier/prettier-chrome-extension
// details https://github.com/prettier/prettier/pull/8534
test("code", async () => {
const files = await fastGlob(["**/*"], {
cwd: distDirectory,
absolute: true,
});
for (const file of files) {
const text = await fs.promises.readFile(file, "utf8");
expect(text.includes("\ufffe")).toBe(false);
}
});
2020-09-17 01:08:44 +09:00
describe("standalone", () => {
const standalone = require(path.join(distDirectory, "standalone.js"));
2022-02-05 16:24:10 +03:00
const plugins = fastGlob
2020-09-17 01:08:44 +09:00
.sync(["parser-*.js"], { cwd: distDirectory, absolute: true })
.map((file) => require(file));
2022-04-06 15:41:50 +08:00
let esmStandalone;
let esmPlugins;
beforeAll(async () => {
({ default: esmStandalone } = await import(
path.join(distDirectory, "esm/standalone.mjs")
));
esmPlugins = await Promise.all(
fastGlob
.sync(["esm/parser-*.mjs"], { cwd: distDirectory, absolute: true })
.map(async (file) => (await import(file)).default)
);
});
2020-09-17 01:08:44 +09:00
for (const parser of parserNames) {
test(parser, () => {
const input = codeSamples(parser);
const umdOutput = standalone.format(input, {
parser,
plugins,
});
expect(typeof input).toBe("string");
expect(typeof umdOutput).toBe("string");
expect(umdOutput).not.toBe(input);
const esmOutput = esmStandalone.format(input, {
parser,
plugins: esmPlugins,
});
expect(esmOutput).toBe(umdOutput);
});
}
});
test("global objects", async () => {
const files = await fastGlob(["standalone.js", "parser-*.js"], {
cwd: distDirectory,
absolute: true,
});
const allowedGlobalObjects = new Set(["prettier", "prettierPlugins"]);
const getGlobalObjects = (file) => {
const sandbox = createSandBox({ files: [file] });
return Object.fromEntries(
Object.entries(sandbox).filter(
([property]) => !allowedGlobalObjects.has(property)
)
);
};
for (const file of files) {
const globalObjects = getGlobalObjects(file);
expect(globalObjects).toStrictEqual({});
}
});