Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: improve helpers to test vite-node (#3842)
- Loading branch information
1 parent
103ad7c
commit 54409a2
Showing
9 changed files
with
92 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// eslint-disable-next-line no-console | ||
console.log('test 1') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,15 @@ | ||
import { expect, test } from 'vitest' | ||
import { execa } from 'execa' | ||
import { resolve } from 'pathe' | ||
|
||
const cliPath = resolve(__dirname, '../../../packages/vite-node/src/cli.ts') | ||
import { runViteNodeCli } from '../../test-utils' | ||
|
||
test('circular 1', async () => { | ||
const entryPath = resolve(__dirname, '../src/circular1/index.ts') | ||
const result = await execa('npx', ['esno', cliPath, entryPath], { reject: true }) | ||
expect(result.stdout).toMatchInlineSnapshot('"A Bindex index"') | ||
const cli = await runViteNodeCli(entryPath) | ||
expect(cli.stdout).toContain('A Bindex index') | ||
}, 60_000) | ||
|
||
test('circular 2', async () => { | ||
const entryPath = resolve(__dirname, '../src/circular2/index.ts') | ||
const result = await execa('npx', ['esno', cliPath, entryPath], { reject: true }) | ||
expect(result.stdout).toMatchInlineSnapshot('"ac b"') | ||
const cli = await runViteNodeCli(entryPath) | ||
expect(cli.stdout).toContain('ac b') | ||
}, 60_000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,48 @@ | ||
import { execa } from 'execa' | ||
import { resolve } from 'pathe' | ||
import pkg from 'vite-node/package.json' | ||
import { expect, it } from 'vitest' | ||
import { editFile, runViteNodeCli } from '../../test-utils' | ||
|
||
const cliPath = resolve(__dirname, '../../../packages/vite-node/src/cli.ts') | ||
const entryPath = resolve(__dirname, '../src/cli-parse-args.js') | ||
|
||
const version = (pkg as any).version | ||
|
||
const parseResult = (s: string) => JSON.parse(s.replaceAll('\'', '"')) | ||
|
||
it('basic', async () => { | ||
const result = await execa('npx', ['esno', cliPath, entryPath], { reject: true }) | ||
expect(result.stdout).include('node') | ||
expect(parseResult(result.stdout)).length(2) | ||
}, 60_000) | ||
const cli = await runViteNodeCli(entryPath) | ||
expect(cli.stdout).toContain('node') | ||
expect(parseResult(cli.stdout)).toHaveLength(2) | ||
}) | ||
|
||
it('--help', async () => { | ||
const r1 = await execa('npx', ['esno', cliPath, '--help', entryPath], { reject: true }) | ||
expect(r1.stdout).include('help') | ||
const r2 = await execa('npx', ['esno', cliPath, '-h', entryPath], { reject: true }) | ||
expect(r2.stdout).include('help') | ||
}, 60_000) | ||
const cli1 = await runViteNodeCli('--help', entryPath) | ||
expect(cli1.stdout).toContain('Usage:') | ||
const cli2 = await runViteNodeCli('-h', entryPath) | ||
expect(cli2.stdout).toContain('Usage:') | ||
}) | ||
|
||
it('--version', async () => { | ||
const r1 = await execa('npx', ['esno', cliPath, '--version', entryPath], { reject: true }) | ||
expect(r1.stdout).include('vite-node/') | ||
const r2 = await execa('npx', ['esno', cliPath, '-v', entryPath], { reject: true }) | ||
expect(r2.stdout).include('vite-node/') | ||
}, 60_000) | ||
const cli1 = await runViteNodeCli('--version', entryPath) | ||
expect(cli1.stdout).toContain(`vite-node/${version}`) | ||
const cli2 = await runViteNodeCli('-v', entryPath) | ||
expect(cli2.stdout).toContain(`vite-node/${version}`) | ||
}) | ||
|
||
it('script args', async () => { | ||
const r1 = await execa('npx', ['esno', cliPath, entryPath, '--version', '--help'], { reject: true }) | ||
expect(parseResult(r1.stdout)).include('--version').include('--help') | ||
}, 60_000) | ||
const cli1 = await runViteNodeCli(entryPath, '--version', '--help') | ||
expect(parseResult(cli1.stdout)).include('--version').include('--help') | ||
}) | ||
|
||
it('script args in -- after', async () => { | ||
const r1 = await execa('npx', ['esno', cliPath, entryPath, '--', '--version', '--help'], { reject: true }) | ||
expect(parseResult(r1.stdout)).include('--version').include('--help') | ||
}, 60_000) | ||
const cli1 = await runViteNodeCli(entryPath, '--', '--version', '--help') | ||
expect(parseResult(cli1.stdout)).include('--version').include('--help') | ||
}) | ||
|
||
it('correctly runs --watch', async () => { | ||
const entryPath = resolve(__dirname, '../src/watched.js') | ||
const cli = await runViteNodeCli('--watch', entryPath) | ||
await cli.waitForStdout('test 1') | ||
editFile(entryPath, c => c.replace('test 1', 'test 2')) | ||
await cli.waitForStdout('test 2') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
import { expect, it } from 'vitest' | ||
import { execa } from 'execa' | ||
import { resolve } from 'pathe' | ||
import ansiEscapes, { HelloWorld } from '../src/self-export' | ||
import { runViteNodeCli } from '../../test-utils' | ||
|
||
it('should export self', () => { | ||
expect(ansiEscapes.HelloWorld).eq(HelloWorld) | ||
expect(Reflect.get(ansiEscapes, 'default').HelloWorld).eq(HelloWorld) | ||
expect(HelloWorld).eq(1) | ||
}) | ||
|
||
const cliPath = resolve(__dirname, '../../../packages/vite-node/src/cli.ts') | ||
|
||
it('example 1', async () => { | ||
const entryPath = resolve(__dirname, '../src/self-export-example1.ts') | ||
const result = await execa('npx', ['esno', cliPath, entryPath], { reject: true }) | ||
expect(result.stdout).includes('Function') | ||
const cli = await runViteNodeCli(entryPath) | ||
await cli.waitForStdout('Function') | ||
}, 60_000) | ||
|
||
it('example 2', async () => { | ||
const entryPath = resolve(__dirname, '../src/self-export-example2.ts') | ||
const result = await execa('npx', ['esno', cliPath, entryPath], { reject: true }) | ||
expect(result.stdout).includes('HelloWorld: 1').includes('default') | ||
const cli = await runViteNodeCli(entryPath) | ||
await cli.waitForStdout('HelloWorld: 1') | ||
}, 60_000) |