0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-23 21:53:55 -05:00
astro/packages/create-astro/test/utils.js
Shoaib Khan 3007d24c98
chore: Migrate all packages/create-astro/test to node:test (#10084)
* chore: Migrate All packages/create-astro/test to node:test

* Some minor fix

* Requested Changes done

* Reopen

* Apply suggestions from code review

* let's test with concurrency

* chore: fix possible false positive tests

* todo test

* skip tests

* Apply suggestions from code review

---------

Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
2024-02-13 14:41:59 +00:00

62 lines
1.5 KiB
JavaScript

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()]);