Skip to content

Commit

Permalink
test: add one test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Feb 27, 2023
1 parent 5fcc1f5 commit 7a303fa
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 4 deletions.
4 changes: 1 addition & 3 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ export async function createVite(
// let's set the watcher independently
if (commonConfig.server) {
if (isWatcherEnabled) {
commonConfig.server.watch = {
cwd: fileURLToPath(settings.config.root),
};
commonConfig.server.watch = {};
} else if (mode === 'build') {
commonConfig.server.watch = {
ignored: ['**'],
Expand Down
42 changes: 41 additions & 1 deletion packages/astro/test/cli.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { expect } from 'chai';
import { cli, parseCliDevStart, cliServerLogSetup } from './test-utils.js';
import { promises as fs } from 'fs';
import { promises as fs, writeFileSync, readFileSync } from 'fs';
import { fileURLToPath } from 'url';
import { isIPv4 } from 'net';
import { join } from 'path';
import stripAnsi from 'strip-ansi';

export function wait(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

describe('astro cli', () => {
const cliServerLogSetupWithFixture = (flags, cmd) => {
Expand All @@ -15,6 +21,40 @@ describe('astro cli', () => {
expect(proc.exitCode).to.equal(0);
});

it('astro check --watch', async () => {
// used to save data coming from `process.stdout`
let stdout = '';
const invalidContent = 'foobar';
const projectRootURL = new URL('./fixtures/astro-check-watch/', import.meta.url);
let process = cli('check', '--root', fileURLToPath(projectRootURL), '--experimental-watch');

process.stdout.on('data', (chunk) => {
stdout += chunk;
});

// we wait for the command to do its job and print stuff to the console...
await wait(3000);

stdout = stripAnsi(stdout);
expect(stdout).to.include('0 errors');
// we need to clear the former output
stdout = '';
// we modify the astro file
const astroFilePath = join(fileURLToPath(projectRootURL), 'src/pages/index.astro');
const originalContent = readFileSync(astroFilePath, 'utf-8');

// we save some invalid content in the file
writeFileSync(astroFilePath, invalidContent);

// we wait for the command to write something in the console
await wait(3000);
stdout = stripAnsi(stdout);
// we restore the content of the file before assertion, so we don't keep a dirty file around
writeFileSync(astroFilePath, originalContent);

expect(stdout).to.include('1 error');
}).timeout(35000);

it('astro --version', async () => {
const pkgURL = new URL('../package.json', import.meta.url);
const pkgVersion = await fs.readFile(pkgURL, 'utf8').then((data) => JSON.parse(data).version);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'astro/config';

// https://astro.build/config
export default defineConfig({
integrations: [],
});
8 changes: 8 additions & 0 deletions packages/astro/test/fixtures/astro-check-watch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-check-watch",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1></h1>
2 changes: 2 additions & 0 deletions packages/astro/test/fixtures/astro-check-watch/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Having a `tsconfig.json`, even empty speeds up the test massively since TypeScript does not have to look for one
{}
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a303fa

Please sign in to comment.