compileSass.test.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import assert from "node:assert/strict";
  2. import fs from "node:fs/promises";
  3. import os from "node:os";
  4. import path from "node:path";
  5. import test from "node:test";
  6. import { compileSass } from "../src/actions/compileSass/index.js";
  7. async function setupStylesheetFixture(t) {
  8. const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "rhedyn-sass-"));
  9. const stylesDir = path.join(tempDir, "styles");
  10. const outDir = path.join(tempDir, "dist");
  11. const filePath = path.join(stylesDir, "main.scss");
  12. const fileOutputPath = path.join(outDir, "static/styles/main.css");
  13. t.after(async () => {
  14. await fs.rm(tempDir, { force: true, recursive: true });
  15. });
  16. await fs.mkdir(stylesDir, { recursive: true });
  17. await fs.mkdir(path.dirname(fileOutputPath), { recursive: true });
  18. await fs.writeFile(filePath, "body { color: red; }", "utf8");
  19. return { tempDir, outDir, filePath, fileOutputPath };
  20. }
  21. test("compileSass does not include css in detail by default", async t => {
  22. const { tempDir, outDir, filePath, fileOutputPath } = await setupStylesheetFixture(t);
  23. const result = await compileSass({
  24. config: {
  25. filePath,
  26. fileOutputPath,
  27. },
  28. meta: {
  29. opts: {
  30. outDir,
  31. runDir: tempDir,
  32. },
  33. },
  34. });
  35. assert.equal(result.detail.href, "/static/styles/main.css");
  36. assert.equal("css" in result.detail, false);
  37. });
  38. test("compileSass includes css in detail when configured", async t => {
  39. const { tempDir, outDir, filePath, fileOutputPath } = await setupStylesheetFixture(t);
  40. const result = await compileSass({
  41. config: {
  42. filePath,
  43. fileOutputPath,
  44. includeCssInDetail: true,
  45. },
  46. meta: {
  47. opts: {
  48. outDir,
  49. runDir: tempDir,
  50. },
  51. },
  52. });
  53. assert.equal(result.detail.href, "/static/styles/main.css");
  54. assert.equal(typeof result.detail.css, "string");
  55. assert.equal(result.detail.css.includes("color:red"), true);
  56. });