Skip to content
Merged
Prev Previous commit
Refactor with cliArguments variable
  • Loading branch information
Sosuke Suzuki committed Oct 23, 2022
commit 9406d54946d7e80c30b26b66965e7419258955ca
104 changes: 32 additions & 72 deletions tests/integration/__tests__/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,14 @@ describe("--cache option", () => {
});

it("re-formats when a file has been updated.", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
const cliArguments = [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
];
const { stdout: firstStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
Expand All @@ -144,13 +145,7 @@ describe("--cache option", () => {
// Update `a.js`
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;");

const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
const { stdout: secondStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
// the cache of `b.js` is only available.
expect.arrayContaining([
Expand All @@ -161,13 +156,14 @@ describe("--cache option", () => {
});

it("re-formats when timestamp has been updated", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
const cliArguments = [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
];
const { stdout: firstStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
Expand All @@ -179,13 +175,7 @@ describe("--cache option", () => {
const time = new Date();
await fs.utimes(path.join(dir, "a.js"), time, time);

const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-strategy",
"metadata",
".",
]);
const { stdout: secondStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
// the cache of `b.js` is only available.
expect.arrayContaining([
Expand Down Expand Up @@ -246,13 +236,14 @@ describe("--cache option", () => {
});

it("re-formats when multiple cached files are updated.", async () => {
await runPrettier(dir, [
const cliArguments = [
"--write",
"--cache",
"--cache-strategy",
"metadata",
".",
]);
];
await runPrettier(dir, cliArguments);

// Update `a.js` to unformatted
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`; ");
Expand All @@ -263,13 +254,7 @@ describe("--cache option", () => {

await runPrettier(dir, ["--cache", "--cache-strategy", "metadata", "."]);

const { stdout: thirdStdout } = await runPrettier(dir, [
"--write",
"--cache",
"--cache-strategy",
"metadata",
".",
]);
const { stdout: thirdStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(thirdStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
Expand Down Expand Up @@ -341,27 +326,22 @@ describe("--cache option", () => {
});

it("doesn't format when cache is available", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
const cliArguments = [
"--cache",
"--cache-strategy",
"content",
"--write",
".",
]);
];
const { stdout: firstStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
])
);

const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--cache-strategy",
"content",
"--write",
".",
]);
const { stdout: secondStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
Expand All @@ -371,13 +351,14 @@ describe("--cache option", () => {
});

it("re-formats when a file has been updated.", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
const cliArguments = [
"--cache",
"--cache-strategy",
"content",
"--write",
".",
]);
];
const { stdout: firstStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
Expand All @@ -388,13 +369,7 @@ describe("--cache option", () => {
// Update `a.js`
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`;");

const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--cache-strategy",
"content",
"--write",
".",
]);
const { stdout: secondStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
// the cache of `b.js` is only available.
expect.arrayContaining([
Expand All @@ -405,13 +380,14 @@ describe("--cache option", () => {
});

it("doesn't re-format when timestamp has been updated", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
const cliArguments = [
"--cache",
"--cache-strategy",
"content",
"--write",
".",
]);
];
const { stdout: firstStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
Expand All @@ -423,13 +399,7 @@ describe("--cache option", () => {
const time = new Date();
await fs.utimes(path.join(dir, "a.js"), time, time);

const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--cache-strategy",
"content",
"--write",
".",
]);
const { stdout: secondStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
Expand Down Expand Up @@ -489,13 +459,14 @@ describe("--cache option", () => {
});

it("re-formats when multiple cached files are updated.", async () => {
await runPrettier(dir, [
const cliArguments = [
"--write",
"--cache",
"--cache-strategy",
"content",
".",
]);
];
await runPrettier(dir, cliArguments);

// Update `a.js` to unformatted
await fs.writeFile(path.join(dir, "a.js"), "const a = `a`; ");
Expand All @@ -506,13 +477,7 @@ describe("--cache option", () => {

await runPrettier(dir, ["--cache", "--cache-strategy", "content", "."]);

const { stdout: thirdStdout } = await runPrettier(dir, [
"--write",
"--cache",
"--cache-strategy",
"content",
".",
]);
const { stdout: thirdStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(thirdStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
Expand Down Expand Up @@ -615,27 +580,22 @@ describe("--cache option", () => {
});

it("does'nt format when cache is available", async () => {
const { stdout: firstStdout } = await runPrettier(dir, [
const cliArguments = [
"--cache",
"--write",
"--cache-location",
nonDefaultCacheFileName,
".",
]);
];
const { stdout: firstStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms$/),
expect.stringMatching(/^b\.js .+ms$/),
])
);

const { stdout: secondStdout } = await runPrettier(dir, [
"--cache",
"--write",
"--cache-location",
nonDefaultCacheFileName,
".",
]);
const { stdout: secondStdout } = await runPrettier(dir, cliArguments);
expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual(
expect.arrayContaining([
expect.stringMatching(/^a\.js .+ms \(cached\)$/),
Expand Down