1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import fs from 'node:fs';
- import { setStdout } from '../dist/index.js';
- import stripAnsi from 'strip-ansi';
- import { before, beforeEach } from 'node:test';
- export function setup() {
- const ctx = { messages: [] };
- before(() => {
- setStdout(
- Object.assign({}, process.stdout, {
- write(buf) {
- ctx.messages.push(stripAnsi(String(buf)).trim());
- return true;
- },
- })
- );
- });
- beforeEach(() => {
- ctx.messages = [];
- });
- return {
- messages() {
- return ctx.messages;
- },
- length() {
- return ctx.messages.length;
- },
- hasMessage(content) {
- return !!ctx.messages.find((msg) => msg.includes(content));
- },
- };
- }
- const resetEmptyFixture = () =>
- fs.promises.rm(new URL('./fixtures/empty/tsconfig.json', import.meta.url));
- const resetNotEmptyFixture = async () => {
- const packagePath = new URL('./fixtures/not-empty/package.json', import.meta.url);
- const tsconfigPath = new URL('./fixtures/not-empty/tsconfig.json', import.meta.url);
- const packageJsonData = JSON.parse(
- await fs.promises.readFile(packagePath, { encoding: 'utf-8' })
- );
- const overriddenPackageJson = Object.assign(packageJsonData, {
- scripts: {
- dev: 'astro dev',
- build: 'astro build',
- preview: 'astro preview',
- },
- });
- return Promise.all([
- fs.promises.writeFile(packagePath, JSON.stringify(overriddenPackageJson, null, 2), {
- encoding: 'utf-8',
- }),
- fs.promises.writeFile(tsconfigPath, '{}', { encoding: 'utf-8' }),
- ]);
- };
- export const resetFixtures = () =>
- Promise.allSettled([resetEmptyFixture(), resetNotEmptyFixture()]);
|