A set of custom Vitest matchers for interactions with the file system.
Install vitest-fs
with your favorite package manager:
$ pnpm add -D vitest-fs
Add a setup file to your Vitest configuration:
import { defineConfig } from 'vitest/config'
export default defineConfig({
test: {
setupFiles: ['tests-setup.ts'],
},
})
Extends the built-in Vitest matchers with some or all matchers of vitest-fs
in your setup file:
import { expect } from 'vitest'
// Use all matchers of `vitest-fs`.
import * as matchers from 'vitest-fs'
expect.extend(matchers)
// Use some matchers of `vitest-fs`.
import { toBeFile, toEqualFile } from 'vitest-fs'
expect.extend({ toBeFile, toEqualFile })
Asserts that the specified path is a directory.
import { expect, test } from 'vitest'
test('should test if a path is a directory', () => {
expect('path/to/directory').toBeDirectory()
})
Asserts that the specified path is an empty directory.
import { expect, test } from 'vitest'
test('should test if a path is an empty directory', () => {
expect('path/to/empty/directory').toBeEmptyDirectory()
})
Asserts that the specified path is an empty file.
import { expect, test } from 'vitest'
test('should test if a path is an empty file', () => {
expect('path/to/empty/file').toBeEmptyFile()
})
Asserts that the specified path is an executable.
import { expect, test } from 'vitest'
test('should test if a path is an executable', () => {
expect('path/to/executable').toBeExecutable()
})
Asserts that the specified path is a file.
import { expect, test } from 'vitest'
test('should test if a path is a file', () => {
expect('path/to/file').toBeFile()
})
Asserts that the specified path is a valid JSON file.
import { expect, test } from 'vitest'
test('should test if a path is a valid JSON file', () => {
expect('path/to/json/file').toBeJsonFile()
})
Asserts that the specified path is a symbolic link.
import { expect, test } from 'vitest'
test('should test if a path is a symbolic link', () => {
expect('path/to/symbolic/link').toBeSymbolicLink()
})
Asserts that the directory structure of the specified path is equal to another one.
Note Only the directory structures should be equal for this matcher to pass, not the content of the files in the directories.
import { expect, test } from 'vitest'
test('should test if directory structures are the same', () => {
expect('path/to/directory').toEqualDirectory('path/to/other/directory')
})
Asserts that a file content is equal to the content of another file.
import { expect, test } from 'vitest'
test('should test if two files are the same', () => {
expect('path/to/file').toEqualFile('path/to/other/file')
})
You can also remove whitespaces for the assertion.
import { expect, test } from 'vitest'
test('should test if two files are the same with whitespaces removed', () => {
expect('path/to/file').toEqualFile('path/to/other/file', { removeWhitespaces: true })
})
Asserts that a file content is equal to a specific content.
import { expect, test } from 'vitest'
test('should test if a file content is equal to a specific content', () => {
expect('path/to/file').toEqualFileContent('file content')
})
You can also remove whitespaces for the assertion.
import { expect, test } from 'vitest'
test('should test if a file content is equal to a specific content with whitespaces removed', () => {
expect('path/to/file').toEqualFileContent('filecontent', { removeWhitespaces: true })
})
Asserts that a JSON file is equal to another JSON file.
import { expect, test } from 'vitest'
test('should test if two JSON files are the same', () => {
expect('path/to/json/file').toEqualJsonFile('path/to/other/json/file')
})
Asserts that a JSON file is equal to a specific JSON content.
import { expect, test } from 'vitest'
test('should test if two files are the same', () => {
expect('path/to/file').toEqualJsonFileContent({ key: 'value' })
})
Licensed under the MIT License, Copyright © HiDeoo.
See LICENSE for more information.