diff --git a/tests/integration/__tests__/cache.js b/tests/integration/__tests__/cache.js index 8b89888e64bf..799c0489e049 100644 --- a/tests/integration/__tests__/cache.js +++ b/tests/integration/__tests__/cache.js @@ -176,4 +176,152 @@ describe("--cache option", () => { await expect(fs.stat(defaultCacheFile)).rejects.toThrowError(); }); }); + + describe("--cache-strategy content", () => { + it("creates default cache file named `node_modules/.cache/prettier/.prettier-cache`", async () => { + await expect(fs.stat(defaultCacheFile)).rejects.toHaveProperty( + "code", + "ENOENT" + ); + await runPrettier(dir, ["--cache", "--cache-strategy", "content", "."]); + await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); + }); + + it("does'nt format when cache is available", async () => { + const { stdout: firstStdout } = await runPrettier(dir, [ + "--cache", + "--cache-strategy", + "content", + "--write", + ".", + ]); + 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", + ".", + ]); + expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual( + expect.arrayContaining([ + expect.stringMatching(/^a\.js .+ms \(cached\)$/), + expect.stringMatching(/^b\.js .+ms \(cached\)$/), + ]) + ); + }); + + it("re-formats when a file has been updated.", async () => { + const { stdout: firstStdout } = await runPrettier(dir, [ + "--cache", + "--cache-strategy", + "content", + "--write", + ".", + ]); + expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual( + expect.arrayContaining([ + expect.stringMatching(/^a\.js .+ms$/), + expect.stringMatching(/^b\.js .+ms$/), + ]) + ); + + // 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", + ".", + ]); + expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual( + // the cache of `b.js` is only available. + expect.arrayContaining([ + expect.stringMatching(/^a\.js .+ms$/), + expect.stringMatching(/^b\.js .+ms \(cached\)$/), + ]) + ); + }); + + it("does'nt re-format when timestamp has been updated", async () => { + const { stdout: firstStdout } = await runPrettier(dir, [ + "--cache", + "--cache-strategy", + "content", + "--write", + ".", + ]); + expect(stripAnsi(firstStdout).split("\n").filter(Boolean)).toEqual( + expect.arrayContaining([ + expect.stringMatching(/^a\.js .+ms$/), + expect.stringMatching(/^b\.js .+ms$/), + ]) + ); + + // update timestamp + 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", + ".", + ]); + expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual( + expect.arrayContaining([ + expect.stringMatching(/^a\.js .+ms \(cached\)$/), + expect.stringMatching(/^b\.js .+ms \(cached\)$/), + ]) + ); + }); + + it("re-formats when options has been updated.", async () => { + const { stdout: firstStdout } = await runPrettier(dir, [ + "--cache", + "--cache-strategy", + "content", + "--write", + ".", + ]); + 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-strategy", + "content", + "--trailing-comma", + "all", + ".", + ]); + expect(stripAnsi(secondStdout).split("\n").filter(Boolean)).toEqual( + expect.arrayContaining([ + expect.stringMatching(/^a\.js .+ms$/), + expect.stringMatching(/^b\.js .+ms$/), + ]) + ); + }); + + it("removes cache file when run Prettier without `--cache` option", async () => { + await runPrettier(dir, ["--cache", "--write", "."]); + await expect(fs.stat(defaultCacheFile)).resolves.not.toThrowError(); + await runPrettier(dir, ["--write", "."]); + await expect(fs.stat(defaultCacheFile)).rejects.toThrowError(); + }); + }); });