+
Skip to content

fix(vite-plugin-angular): add detection for Vitest CLI in watch mode #1602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion packages/vite-plugin-angular/src/lib/angular-vite-plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,58 @@
import { describe, it, expect } from 'vitest';
import { angular } from './angular-vite-plugin';
import { angular, isTestWatchMode } from './angular-vite-plugin';

describe('angularVitePlugin', () => {
it('should work', () => {
expect(angular()[0].name).toEqual('@analogjs/vite-plugin-angular');
});
});

describe('isTestWatchMode', () => {
it('should return false if vitest command not included in the command', () => {
const result = isTestWatchMode('vite');

expect(result).toBeFalsy();
});

it('should return false for vitest --run', () => {
const result = isTestWatchMode('vitest', ['--run']);

expect(result).toBeFalsy();
});

it('should return true for vitest --no-run', () => {
const result = isTestWatchMode('vitest', ['--no-run']);

expect(result).toBeTruthy();
});

it('should return true for vitest --watch', () => {
const result = isTestWatchMode('vitest', ['--watch']);

expect(result).toBeTruthy();
});

it('should return true for vitest watch', () => {
const result = isTestWatchMode('vitest', ['watch']);

expect(result).toBeTruthy();
});

it('should return false for vitest --no-watch', () => {
const result = isTestWatchMode('vitest', ['--no-watch']);

expect(result).toBeFalsy();
});

it('should return false for vitest --watch=false', () => {
const result = isTestWatchMode('vitest', ['--watch=false']);

expect(result).toBeFalsy();
});

it('should return false for vitest --watch false', () => {
const result = isTestWatchMode('vitest', ['--watch', 'false']);

expect(result).toBeFalsy();
});
});
50 changes: 48 additions & 2 deletions packages/vite-plugin-angular/src/lib/angular-vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export function angular(options?: PluginOptions): Plugin[] {
let nextProgram: NgtscProgram | undefined | ts.Program;
let builderProgram: ts.EmitAndSemanticDiagnosticsBuilderProgram;
let watchMode = false;
let testWatchMode = false;
let testWatchMode = isTestWatchMode();
let inlineComponentStyles: Map<string, string> | undefined;
let externalComponentStyles: Map<string, string> | undefined;
const sourceFileCache = new SourceFileCache();
Expand Down Expand Up @@ -241,8 +241,11 @@ export function angular(options?: PluginOptions): Plugin[] {
// - vite override from vitest-angular
// - @nx/vite executor set server.watch explicitly to undefined (watch)/null (watch=false)
// - vite config for test.watch variable
// - vitest watch mode detected from the command line
testWatchMode =
!(config.server.watch === null) || config.test?.watch === true;
!(config.server.watch === null) ||
config.test?.watch === true ||
testWatchMode;
},
configureServer(server) {
viteServer = server;
Expand Down Expand Up @@ -1038,3 +1041,46 @@ function getComponentStyleSheetMeta(id: string): {
function getFilenameFromPath(id: string): string {
return new URL(id, 'http://localhost').pathname.replace(/^\//, '');
}

/**
* Checks for vitest run from the command line
* @returns boolean
*/
export function isTestWatchMode(
command = process.env['_'],
args = process.argv
) {
const isVitestCommand = command?.includes('vitest');

// vitest command was not used
if (!isVitestCommand) {
return false;
}

// vitest --run
const hasRun = args.find((arg) => arg.includes('--run'));
if (hasRun) {
return false;
}

// vitest --no-run
const hasNoRun = args.find((arg) => arg.includes('--no-run'));
if (hasNoRun) {
return true;
}

// check for --watch=false or --no-watch
const hasWatch = args.find((arg) => arg.includes('watch'));
if (hasWatch && ['false', 'no'].some((neg) => hasWatch.includes(neg))) {
return false;
}

// check for --watch false
const watchIndex = args.findIndex((arg) => arg.includes('watch'));
const watchArg = args[watchIndex + 1];
if (watchArg && watchArg === 'false') {
return false;
}

return true;
}
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载