Testing functions that work with files benefits from having testfiles at hand, as well for the file type(s) that must be handled as for those that must be excluded from handling to prevent errors.
The goal of this package is to test if a function can identify, distinguish and reject file types. Providing all possible properties of a certain type and narrowing down to specific configurations and possible errors goes far beyond its scope (this should be better done with packages of multiple testfiles for only one file type).
The package installs an extensible collection of small testfiles for different file types and a function to get a path for a testfile of a specific type to be used in a function call (see below). Note that full MIME types are used for easier distinguishing between e.g. image/jpeg and video/JPEG, moreover MIME types are clear concerning e.g. “jpeg” vs. “jpg” and wrongly set file extensions may crash your application, i.e. you should already prefer them in your code. Refer to sources like MDN’s list of common media types to clarify historical idiosyncrasies like .txt files being text/plain or .mp3 files being audio/mpeg.
The provided testfiles have real content and structure to distinguish e.g. actual plaintext files from CSS files - usually tools like mimetype and the well established Linux file command do not analyse contents and would classify an empty xy.css as text/css or a CSS file xy with missing .css extension as text/plain as a default for any file missing a “magic” file signature (not to speak of plain fakes like Bun’s “file.type” and Deno’s “contentType” that fully rely on a file’s extension). The provided testfiles have enough content to support possible file type checking by taking samples from their beginning.
Note that not all discussed types are provided yet, but you could add them yourself to the local directory (see Details).
hh lohmann <hh.lohmann@gmail.com>
import { predefinedTestfile } from 'predefined-test-files'
myFunctionConsumingFiles( predefinedTestfile( mimeType ) )
const arrayOfExistingTypessMatchingSearch = predefinedTestfile( `search:` + mimeType )
search: prefix: Exact MIME type identifier of requested testfile
image/jpeg will return the absolute path to testfile.image.jpegsearch: prefix: Part of MIME type identifier to search testfiles for
search:image/ may return [ 'image/jpeg', 'image/png' ]search: prefix = predefinedTestfile( mimeType )
testfile.[ mimeType.top ].[ mimeType.sub ][ mimeType.top ] / [ mimeType.sub ]: see Detailssearch: prefix = predefinedTestfile( search: + mimeType )
[ 'image/jpeg', 'image/png' ] myPdfReader( predefinedTestfile( 'application/pdf' ) )
predefinedTestfile( 'search:image/' )
.forEach( value => {
myImgProcessor( predefinedTestfile( value ) )
} )
// This would help if an outdated documentation relies on
// "application/javascript" which is officially replaced by
// "text/javascript"
predefinedTestfile( 'search:/javascript' )
.forEach( value => {
myJsValidator( predefinedTestfile( value ) )
} )
if( predefinedTestfile( 'search:image/bmp' ).length!==0 ){
myImgProcessor( predefinedTestfile( 'image/bmp' ) )
}
else{
console.log( 'Bitmaps currently not testable' )
}
Intentionally no options for testing by properties like file size, naming etc. since these do not depend on the type of a file.
forEach iteration to test exactly those image types that your function is expected to handle, e.g.
['gif','jpeg','png'].forEach( value => {
yourFunction( predefinedTestfile( 'image/'+value ) )
})
Pick for your preferred package manager:
npm i predefined-test-files
pnpm i predefined-test-files
bun i predefined-test-files
# For Yarn you should double check docs for your and / or
# current Yarn version, newer versions do not treat `i package_name`
# as an alias for `add ...` and exclude global installations
yarn add predefined-test-files
image/jpeg/ in an identifier, e.g. image
Note that the structure of a MIME type identifier is just implicitly defined over various RFCs, in practice it is a top level type like
imagewith a subtype likejpegconnected by a slash/
/ in an identifier, e.g. jpegPossible MIME types are those listed in the official IANA registy
testfile.[ mimeType.top ].[ mimeType.sub ]
predefinedTestfilepredefinedTestfile returns an absolute path to testfile.[ mimeType.top ].[ mimeType.sub ]
/home/joe-doe/fantastic-project/node_moules/predefined-test-files/testfiles/testfile.application.pdfSee demos
MIT (see LICENSE.txt)
Testfiles for top level types application, audio, font, image and video as well as for text/rtf are taken from Edmundas Ramanauskas’s file-type-mime package under MIT license (c) 2023 Edmundas Ramanauskas