-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use bun test and add ndjson tests
- Loading branch information
Showing
9 changed files
with
117 additions
and
53 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
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,4 @@ | ||
{"name": "Gilbert", "wins": [["straight", "7♣"], ["one pair", "10♥"]]} | ||
{"name": "Alexa", "wins": [["two pair", "4♠"], ["two pair", "9♠"]]} | ||
{"name": "May", "wins": []} | ||
{"name": "Deloise", "wins": [["three of a kind", "5♣"]]} |
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,50 @@ | ||
import { describe, expect, it, mock } from 'bun:test'; | ||
import path from 'path'; | ||
|
||
import { mapJsonLines, parseJsonLine } from '../ndjson'; | ||
|
||
function fixture(...filePath: string[]) { | ||
return path.join(__dirname, 'fixtures', ...filePath); | ||
} | ||
|
||
describe('mapJsonLines', () => { | ||
it('maps each line of file', async () => { | ||
const lines: string[] = []; | ||
await mapJsonLines(fixture('ndjson.json'), (content) => { | ||
lines.push(content); | ||
}); | ||
|
||
expect(lines).toEqual([ | ||
expect.stringContaining('Gilbert'), | ||
expect.stringContaining('Alexa'), | ||
expect.stringContaining('May'), | ||
expect.stringContaining('Deloise'), | ||
]); | ||
}); | ||
|
||
it('maps each line with line numbers starting from 1', async () => { | ||
const onReadLine = mock(); | ||
await mapJsonLines(fixture('ndjson.json'), onReadLine); | ||
|
||
expect(onReadLine).not.toHaveBeenCalledWith(expect.any(String), 0); | ||
expect(onReadLine).toHaveBeenCalledWith(expect.any(String), 1); | ||
expect(onReadLine).toHaveBeenCalledWith(expect.any(String), 2); | ||
expect(onReadLine).toHaveBeenCalledWith(expect.any(String), 3); | ||
expect(onReadLine).toHaveBeenCalledWith(expect.any(String), 4); | ||
}); | ||
}); | ||
|
||
describe('parseJsonLine', () => { | ||
it('parses a single line from file', async () => { | ||
expect(await parseJsonLine(fixture('ndjson.json'), 1)).toMatchObject({ name: 'Gilbert' }); | ||
expect(await parseJsonLine(fixture('ndjson.json'), 2)).toMatchObject({ name: 'Alexa' }); | ||
expect(await parseJsonLine(fixture('ndjson.json'), 3)).toMatchObject({ name: 'May' }); | ||
expect(await parseJsonLine(fixture('ndjson.json'), 4)).toMatchObject({ name: 'Deloise' }); | ||
}); | ||
|
||
it('throws if single line is not found', async () => { | ||
await expect(parseJsonLine(fixture('ndjson.json'), 99999)).rejects.toThrow( | ||
'Line 99999 not found in file' | ||
); | ||
}); | ||
}); |
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