Certainly! Below is a Vitest test file for a self-contained ASCII art generation function.
I'll provide:
- A simple
generateAsciiArt function (for demonstration).
- A Vitest test file that checks if the function outputs ASCII art matching the described object.
1. asciiArtGenerator.ts (the function)
// asciiArtGenerator.ts
export function generateAsciiArt(description: string): string {
// For demonstration, only supports "cat" and "tree"
switch (description.toLowerCase()) {
case "cat":
return `
/\\_/\\
( o.o )
> ^ <
`.trim();
case "tree":
return `
/\\
/ \\
/____\\
||
||
`.trim();
default:
return `No ASCII art available for "${description}"`;
}
}
2. asciiArtGenerator.test.ts (the Vitest test file)
// asciiArtGenerator.test.ts
import { describe, it, expect } from 'vitest';
import { generateAsciiArt } from './asciiArtGenerator';
describe('generateAsciiArt', () => {
it('should generate ASCII art for "cat"', () => {
const result = generateAsciiArt('cat');
expect(result).toContain('/\\_/\\');
expect(result).toContain('( o.o )');
expect(result).toContain('> ^ <');
// Optionally, check the whole output
expect(result).toBe(
[
' /\\_/\\',
'( o.o )',
' > ^ <'
].join('\n')
);
});
it('should generate ASCII art for "tree"', () => {
const result = generateAsciiArt('tree');
expect(result).toContain('/\\');
expect(result).toContain('||');
// Optionally, check the whole output
expect(result).toBe(
[
' /\\',
' / \\',
'/____\\',
' ||',
' ||'
].join('\n')
);
});
it('should return a message for unknown objects', () => {
const result = generateAsciiArt('dog');
expect(result).toBe('No ASCII art available for "dog"');
});
});
How this works:
- The function is self-contained and only supports "cat" and "tree" for demonstration.
- The test file checks that the output contains expected ASCII art features and matches the full output.
- It also checks the fallback for unknown descriptions.
You can expand the function and tests for more objects as needed!