utils.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import fs from 'node:fs';
  2. import { setStdout } from '../dist/index.js';
  3. import stripAnsi from 'strip-ansi';
  4. import { before, beforeEach } from 'node:test';
  5. export function setup() {
  6. const ctx = { messages: [] };
  7. before(() => {
  8. setStdout(
  9. Object.assign({}, process.stdout, {
  10. write(buf) {
  11. ctx.messages.push(stripAnsi(String(buf)).trim());
  12. return true;
  13. },
  14. })
  15. );
  16. });
  17. beforeEach(() => {
  18. ctx.messages = [];
  19. });
  20. return {
  21. messages() {
  22. return ctx.messages;
  23. },
  24. length() {
  25. return ctx.messages.length;
  26. },
  27. hasMessage(content) {
  28. return !!ctx.messages.find((msg) => msg.includes(content));
  29. },
  30. };
  31. }
  32. const resetEmptyFixture = () =>
  33. fs.promises.rm(new URL('./fixtures/empty/tsconfig.json', import.meta.url));
  34. const resetNotEmptyFixture = async () => {
  35. const packagePath = new URL('./fixtures/not-empty/package.json', import.meta.url);
  36. const tsconfigPath = new URL('./fixtures/not-empty/tsconfig.json', import.meta.url);
  37. const packageJsonData = JSON.parse(
  38. await fs.promises.readFile(packagePath, { encoding: 'utf-8' })
  39. );
  40. const overriddenPackageJson = Object.assign(packageJsonData, {
  41. scripts: {
  42. dev: 'astro dev',
  43. build: 'astro build',
  44. preview: 'astro preview',
  45. },
  46. });
  47. return Promise.all([
  48. fs.promises.writeFile(packagePath, JSON.stringify(overriddenPackageJson, null, 2), {
  49. encoding: 'utf-8',
  50. }),
  51. fs.promises.writeFile(tsconfigPath, '{}', { encoding: 'utf-8' }),
  52. ]);
  53. };
  54. export const resetFixtures = () =>
  55. Promise.allSettled([resetEmptyFixture(), resetNotEmptyFixture()]);