Skip to content

Commit

Permalink
test(vite): add plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Feb 3, 2022
1 parent 4403f12 commit 698626d
Show file tree
Hide file tree
Showing 15 changed files with 489 additions and 5 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"docs:build": "pnpm -C docs run build",
"docs:serve": "pnpm -C docs run serve",
"release": "bumpp vite-plugin-laravel/package.json --push --tag --commit \"release: v\"",
"test:php": ".\\vendor\\bin\\pest"
"test": "npm run test:php && npm run test:vite",
"test:php": ".\\vendor\\bin\\pest",
"test:vite": "vitest --run"
},
"devDependencies": {
"@innocenzi/eslint-config": "^0.7.1",
Expand Down
101 changes: 101 additions & 0 deletions pnpm-lock.yaml

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

6 changes: 4 additions & 2 deletions vite-plugin-laravel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
"bugs": "https://github.com/innocenzi/laravel-vite/issues",
"scripts": {
"prepare": "npm run build",
"build": "tsup src/index.ts"
"build": "tsup src/index.ts",
"test": "vitest"
},
"devDependencies": {
"@types/debug": "^4.1.7",
"tsup": "^5.11.13",
"typescript": "^4.5.5",
"vite": "^2.7.13"
"vite": "^2.7.13",
"vitest": "^0.2.5"
},
"dependencies": {
"debug": "^4.3.3",
Expand Down
12 changes: 10 additions & 2 deletions vite-plugin-laravel/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@ const debug = makeDebugger(PREFIX)
* Calls an artisan command.
*/
export function callArtisan(executable: string, ...params: string[]): string {
if (process.env.VITEST) {
return execaSync(process.env.TEST_ARTISAN_SCRIPT!, [executable, 'artisan', ...params], { encoding: 'utf-8' })?.stdout
}

return execaSync(executable, ['artisan', ...params])?.stdout
}

/**
* Calls a shell command.
*/
export function callShell(executable: string, ...params: string[]): string {
if (process.env.VITEST) {
return execaSync(process.env.TEST_ARTISAN_SCRIPT!, [executable, ...params])?.stdout
}

return execaSync(executable, [...params])?.stdout
}

/**
* Reads the configuration from the `php artisan vite:config` command.
*/
export function readConfig(options: Options, env: Record<string, string>, name?: string): ResolvedConfiguration {
export function readConfig(options: Options, env: NodeJS.ProcessEnv, name?: string): ResolvedConfiguration {
const executable = getPhpExecutablePath(options, env)
const configFromJson = (json: any, name?: string) => {
if (name && !(name in json.configs)) {
Expand All @@ -45,7 +53,7 @@ export function readConfig(options: Options, env: Record<string, string>, name?:

try {
// Sets path from environment variable
if (options.config !== false && env.CONFIG_PATH_VITE) {
if (!options.config && options.config !== false && env.CONFIG_PATH_VITE) {
debug('Setting configuration file path to CONFIG_PATH_VITE.')
options.config = env.CONFIG_PATH_VITE
}
Expand Down
33 changes: 33 additions & 0 deletions vite-plugin-laravel/tests/__fixtures__/artisan-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"configs": {
"artisan": {
"entrypoints": {
"ssr": "resources/scripts/ssr-artisan.ts",
"paths": [
"resources/scripts/main-artisan.ts"
],
"ignore": "/\\.(d\\.ts|json)$/"
},
"dev_server": {
"enabled": true,
"url": "http://localhost:3000",
"ping_before_using_manifest": true,
"ping_url": null,
"ping_timeout": 1,
"key": null,
"cert": null
},
"build_path": "build"
}
},
"aliases": { "@": "resources" },
"commands": { "artisan": [], "shell": [] },
"testing": { "use_manifest": false },
"env_prefixes": ["VITE_", "MIX_", "SCRIPT_"],
"interfaces": {
"heartbeat_checker": "Innocenzi\\Vite\\HeartbeatCheckers\\HttpHeartbeatChecker",
"tag_generator": "Innocenzi\\Vite\\TagGenerators\\CallbackTagGenerator",
"entrypoints_finder": "Innocenzi\\Vite\\EntrypointsFinder\\DefaultEntrypointsFinder"
},
"default": "artisan"
}
33 changes: 33 additions & 0 deletions vite-plugin-laravel/tests/__fixtures__/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"configs": {
"default": {
"entrypoints": {
"ssr": "resources/scripts/ssr.ts",
"paths": [
"resources/scripts/main.ts"
],
"ignore": "/\\.(d\\.ts|json)$/"
},
"dev_server": {
"enabled": true,
"url": "http://localhost:3000",
"ping_before_using_manifest": true,
"ping_url": null,
"ping_timeout": 1,
"key": null,
"cert": null
},
"build_path": "build"
}
},
"aliases": { "@": "resources" },
"commands": { "artisan": [], "shell": [] },
"testing": { "use_manifest": false },
"env_prefixes": ["VITE_", "MIX_", "SCRIPT_"],
"interfaces": {
"heartbeat_checker": "Innocenzi\\Vite\\HeartbeatCheckers\\HttpHeartbeatChecker",
"tag_generator": "Innocenzi\\Vite\\TagGenerators\\CallbackTagGenerator",
"entrypoints_finder": "Innocenzi\\Vite\\EntrypointsFinder\\DefaultEntrypointsFinder"
},
"default": "default"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
color: red;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import '@/css/styles.css'
console.log('app content')
Loading

0 comments on commit 698626d

Please sign in to comment.