I have an Angular application that can parse files from user input. I need to run Jasmine/Karma unit tests in my browser to verify the parsing is working correctly.
Obviously I cannot just read the file with fs.readFileSync
since this is not running in Node. Is there any way to load files similar to this, perhaps to load them into Jasmine/Karma memory before the tests are run?
Currently I have to copy the contents of my files into strings in mock typescript objects. This is a lot of silly duplication since the actually file is right here beside it. Plus, this does not work for binary files since copying the file contents as a string doesn't play well.
Here's what I'm doing currently:
mock-files.ts
const encoder = new TextEncoder();
function createBuffer(str: string) {
return encoder.encode(str).buffer;
}
export const mockSongShowPlusFile1: IRawDataFile = {
name: 'Be Near',
ext: 'sbsong',
type: '',
dataAsBuffer: createBuffer(`&\u0000\u0000...`), //long binary string here, but it doesn't work
dataAsString: ``, //For other file types I'd paste the raw content as a string here
};
test.spec.ts
import { mockSongShowPlusFile1 } from 'mock-files';
it('should return a song for a SongShow Plus 7 file1', () => {
const testFile: IRawDataFile = structuredClone(mockSongShowPlusFile1);
expect(inputConverter.extractSongData(testFile)).toEqual({
fileName: testFile.name,
title: 'Be Near',
info: [
{ name: 'Author', value: 'Barnard, Shane' },
{ name: 'Copyright', value: '2003 Waiting Room Music' },
{ name: 'CCLI', value: '4090362' },
{ name: 'Key', value: 'B' },
],
slides: [ ... ], //actual array data would go here
});
});
How can I get this binary file data into my unit tests?