Files
cross-env/src/__tests__/command.test.ts
T
0112288e01 feat: modernize cross-env with TypeScript, Vitest, and ESM-only build (#261)
NOTE: For the vast majority of users running supported versions of Node.js, this should not require any changes and you can update without issue.

BREAKING CHANGE: This is a major rewrite that changes the module format from CommonJS to ESM-only. The package now requires Node.js >=20 and only exports ESM modules (not relevant in most cases).

BREAKING CHANGE: The package.json exports have been updated to remove CommonJS support. Only ESM imports are now supported.

BREAKING CHANGE: All source files have been converted from JavaScript to TypeScript, requiring TypeScript-aware tooling for development.

- Replace Jest with Vitest for testing
- Convert all source files from .js to .ts with proper TypeScript types
- Use zshy for ESM-only builds (removes CJS support)
- Adopt @epic-web/config for TypeScript, ESLint, and Prettier
- Update to Node.js >=20 requirement
- Remove kcd-scripts dependency
- Add comprehensive e2e tests with GitHub Actions matrix testing
- Update GitHub workflow with caching and cross-platform testing
- Modernize documentation and remove outdated sections
- Update all dependencies to latest versions
- Add proper TypeScript declarations and exports

The tool maintains its original functionality while being completely modernized with the latest tooling and best practices

Co-authored-by: GitHub Action <[email protected]>
2025-07-25 15:19:14 -06:00

82 lines
2.6 KiB
TypeScript

import { describe, test, expect, vi, afterEach } from 'vitest'
import { commandConvert } from '../command.js'
import * as isWindowsModule from '../is-windows.js'
vi.mock('../is-windows.js')
const env = {
test: 'a',
test1: 'b',
test2: 'c',
test3: 'd',
empty_var: '',
}
afterEach(() => {
vi.clearAllMocks()
})
describe('commandConvert', () => {
test('converts unix-style env variable usage for windows', () => {
vi.mocked(isWindowsModule.isWindows).mockReturnValue(true)
expect(commandConvert('$test', env)).toBe('%test%')
})
test('leaves command unchanged when not a variable', () => {
expect(commandConvert('test', env)).toBe('test')
})
test("doesn't convert windows-style env variable", () => {
vi.mocked(isWindowsModule.isWindows).mockReturnValue(false)
expect(commandConvert('%test%', env)).toBe('%test%')
})
test('leaves variable unchanged when using correct operating system', () => {
vi.mocked(isWindowsModule.isWindows).mockReturnValue(false)
expect(commandConvert('$test', env)).toBe('$test')
})
test('is stateless', () => {
// this test prevents falling into regexp traps like this:
// http://stackoverflow.com/a/1520853/971592
vi.mocked(isWindowsModule.isWindows).mockReturnValue(true)
expect(commandConvert('$test', env)).toBe(commandConvert('$test', env))
})
test('converts embedded unix-style env variables usage for windows', () => {
vi.mocked(isWindowsModule.isWindows).mockReturnValue(true)
expect(commandConvert('$test1/$test2/$test3', env)).toBe(
'%test1%/%test2%/%test3%',
)
})
test('leaves embedded variables unchanged when using correct operating system', () => {
vi.mocked(isWindowsModule.isWindows).mockReturnValue(false)
expect(commandConvert('$test1/$test2/$test3', env)).toBe(
'$test1/$test2/$test3',
)
})
test('converts braced unix-style env variable usage for windows', () => {
vi.mocked(isWindowsModule.isWindows).mockReturnValue(true)
expect(commandConvert('${test}', env)).toBe('%test%')
})
test('removes non-existent variables from the converted command', () => {
vi.mocked(isWindowsModule.isWindows).mockReturnValue(true)
expect(commandConvert('$test1/$foo/$test2', env)).toBe('%test1%//%test2%')
})
test('removes empty variables from the converted command', () => {
vi.mocked(isWindowsModule.isWindows).mockReturnValue(true)
expect(commandConvert('$foo/$test/$empty_var', env)).toBe('/%test%/')
})
test('normalizes command on windows', () => {
vi.mocked(isWindowsModule.isWindows).mockReturnValue(true)
// index.js calls `commandConvert` with `normalize` param
// as `true` for command only
expect(commandConvert('./cmd.bat', env, true)).toBe('cmd.bat')
})
})