11import { exec } from 'node:child_process'
22import { promises as fs } from 'node:fs'
33import path from 'node:path'
4- import test , { suite } from 'node:test'
4+ import test , { snapshot , suite } from 'node:test'
5+
6+ // All examples will be tested with no additional command-line arguments. To
7+ // also test with specific sets of arguments, add them here:
8+ const additionalCommandLineArguments : Readonly <
9+ Record < string , readonly string [ ] >
10+ > = {
11+ 'fibonacci.plz' : [
12+ '--input=0' ,
13+ '--input=1' ,
14+ '--input=2' ,
15+ '--input=10' ,
16+ '--input="not a number"' ,
17+ '--input=-1' ,
18+ ] ,
19+ }
20+
21+ snapshot . setResolveSnapshotPath ( _ =>
22+ path . join ( import . meta. dirname , '..' , 'examples' , '.snapshot' ) ,
23+ )
524
625const exampleDirectoryPath = path . join ( import . meta. dirname , '..' , 'examples' )
726const pleasePath = path . join (
@@ -16,24 +35,54 @@ suite('examples', async () => {
1635 fileName => fileName . endsWith ( '.plz' ) ,
1736 )
1837 for ( const exampleFileName of exampleFileNames ) {
19- const exampleFilePath = path . join ( exampleDirectoryPath , exampleFileName )
20- // TODO: Use snapshot testing instead of merely checking for errors.
21- test (
22- exampleFileName ,
23- ( ) =>
24- new Promise ( ( resolve , reject ) => {
25- const _childProcess = exec (
26- `cat "${ exampleFilePath } " | node "${ pleasePath } " --output-format=plz` ,
27- ( error , _stdout , _stderr ) => {
28- // `error` is an `ExecError` when exit status is nonzero.
29- if ( error !== null ) {
30- reject ( error )
31- } else {
32- resolve ( undefined )
33- }
34- } ,
35- )
36- } ) ,
37- )
38+ const setsOfCommandLineArguments = [
39+ '' , // Always test with no arguments.
40+ ...( additionalCommandLineArguments [ exampleFileName ] ?? [ ] ) ,
41+ ] as const
42+
43+ for ( const commandLineArguments of setsOfCommandLineArguments ) {
44+ const exampleFilePath = path . join ( exampleDirectoryPath , exampleFileName )
45+ test (
46+ exampleFileName . concat (
47+ commandLineArguments === '' ? '' : ` ${ commandLineArguments } ` ,
48+ ) ,
49+ _ =>
50+ new Promise ( ( resolve , reject ) => {
51+ const _childProcess = exec (
52+ `cat "${ exampleFilePath } " | node "${ pleasePath } " --no-color --output-format=plz ${ commandLineArguments } ` ,
53+ ( error , stdout , stderr ) => {
54+ // `error` is an `ExecException` when exit status is nonzero.
55+ if ( error !== null ) {
56+ reject ( error )
57+ } else {
58+ Promise . all ( [
59+ test ( 'stdout' , t =>
60+ t . assert . snapshot ( stdout , snapshotOptions ) ) ,
61+ test ( 'stderr' , t =>
62+ t . assert . snapshot ( stderr , snapshotOptions ) ) ,
63+ ] )
64+ . then ( _ => resolve ( undefined ) )
65+ . catch ( reject )
66+ }
67+ } ,
68+ )
69+ } ) ,
70+ )
71+ }
3872 }
3973} )
74+
75+ // Expect snapshots to already be strings.
76+ const snapshotOptions = {
77+ serializers : [
78+ ( value : unknown ) => {
79+ if ( typeof value !== 'string' ) {
80+ throw new Error (
81+ `snapshot was not a string (was \`${ JSON . stringify ( value ) } \`)` ,
82+ )
83+ } else {
84+ return value
85+ }
86+ } ,
87+ ] ,
88+ }
0 commit comments