npm package

Providing predefined files of certain types to test functions working with them

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>

Synopsis

  import { predefinedTestfile } from 'predefined-test-files'

  myFunctionConsumingFiles( predefinedTestfile( mimeType ) )

  const arrayOfExistingTypessMatchingSearch = predefinedTestfile( `search:` + mimeType )

Parameters

mimeType

Returns

Examples

Caveats

Installation

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

Details

Demo

See demos on GitHub Pages for this repo

Tests

Source Code

License

References

Internet Assigned Numbers Authority (IANA): Media Types

Internet Assigned Numbers Authority (IANA): Top-Level Media Types

mimetype / File-MimeInfo

MDN: Common media types (Web)

Wikipedia: List of file signatures

Wikipedia: Request for Comments (RFC)

Linux “file” command

Endspacer: './markdown-assets/endspacer.png' missing - see https://hh-lohmann.github.io/html-endspacer
[top]