0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00
astro/packages/create-astro/test/utils.js
2024-03-07 18:16:14 +00:00

63 lines
1.5 KiB
JavaScript

import fs from 'node:fs';
import { before, beforeEach } from 'node:test';
import stripAnsi from 'strip-ansi';
import { setStdout } from '../dist/index.js';
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',
},
dependencies: undefined,
});
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()]);