πŸš€ Big News:Socket Has Acquired Secure Annex.Learn More β†’
Socket
Book a DemoSign in
Socket

test-file-magic

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

test-file-magic

Switch between source and test files, automatically scaffolding your tests if they don't exist.

latest
Source
npmnpm
Version
0.7.13
Version published
Maintainers
1
Created
Source

This is an open-source VS Code extension created by Herb Caudill.

Why?

Going back and forth between a test file and its source is something that I do many, many times a day; and every time I do it, I have to think about where test files are stored relative to source files. It's a tiny bit of cognitive friction that I'd like to get rid of.

Creating a test file is also a chore. It's a tiny chore, to be sure, but it's also a distraction and a source of friction: I have to think about what naming convention we use, where it should be saved, and I have to put some boilerplate in place before I can start actually writing a test.

Features

Test File Magic gets rid of that friction with a single keyboard shortcut, alt+T alt+T, to do all of those things:

  • From a test file, press alt+T alt+T to jump to the corresponding source file.
  • From a source file, press alt+T alt+T to jump to the test file.
  • If a test file doesn't exist, one is created and scaffolded with the necessary boilerplate (importing the source module, creating a describe block, etc.)

screenshot

You can also invoke this command from the context menu: Right-click on a file and choose Toggle Test ↔ Source to jump from a source file to a test file. Do the same to jump back.

This command is also available from the command palette.

Configuration

Different developers, teams, and projects organize their tests differently:

  • Some put them side-by-side with source files; others gather them into a tests folder in each directory; still others put them in a root-level tests folder with a parallel file structure.
  • Some insert a keyword like test or spec between the filename and extension (e.g. foo.test.js).

The Test File Magic extension can be customized to work with many common patterns for organizing test files.

By default, it assumes that your tests are organized like this:

πŸ“ [workspace root]
    πŸ“ src
        πŸ“„ index.js
        πŸ“„ index.test.js
        πŸ“„ foo.js
        πŸ“„ foo.test.js
        πŸ“ lib
            πŸ“„ bar.js
            πŸ“„ bar.test.js

Test File Magic offers these settings:

NameDescriptionDefault
testFileMagic.fileExtensionsFile extensions for source and test files (comma-separated).ts, js, tsx, jsx
testFileMagic.testKeywordKeyword for test filenames, inserted before the file extension. For example, if set to spec, the test file for foo.ts is foo.spec.ts.test
testFileMagic.sourceDirectoryName of the directory containing source files.src
testFileMagic.testDirectoryName of the directory (or directories) containing test files. If this is not set, each test file lives alongside its corresponding source file.(notΒ set)
testFileMagic.testDirectoryLocationIf testFileMagic.testDirectory is set, this indicates whether there is just one test directory or many:
  • root Tests are stored in a single root-level test directory, with an internal directory structure mirroring the source directory's structure.
  • alongside Tests are stored in multiple test directories, each one alongside its corresponding source files.
root
testFileMagic.testFileTemplateTemplate to use when creating a new test file for a module. (See below)

Note that either testFileMagic.testDirectory or testFileMagic.testKeyword (or both) need to be set.

As with all settings, you can change these at the user level, or at the workspace level so that you can adapt the extension's behavior to the file naming scheme used on each project you work on.

Configuration examples

Tests identified with a different keyword

πŸ“ [workspace root]
  πŸ“ src
    πŸ“„ index.js
    πŸ“„ index.spec.js    👸 custom keyword for tests (`spec` instead of `test`)
    πŸ“„ foo.js
    πŸ“„ foo.spec.js      👸
    πŸ“ lib
      πŸ“„ bar.js
      πŸ“„ bar.spec.js    👸
{
  "testFileMagic.testKeyword": "spec"
}

Tests in subdirectories

πŸ“ [workspace root]
    πŸ“ src
        πŸ“„ index.js
        πŸ“„ foo.js
        πŸ“ tests                👸 each folder has a `tests` subdirectory
            πŸ“„ index.test.js
            πŸ“„ foo.test.js
        πŸ“ lib
            πŸ“„ bar.js
            πŸ“ tests            👸
                πŸ“„ bar.test.js
{
  "testFileMagic.testDirectory": "tests",
  "testFileMagic.testDirectoryLocation": "alongside"
}

Tests in root-level directory

πŸ“ [workspace root]
    πŸ“ src
        πŸ“„ index.js
        πŸ“ lib
            πŸ“„ bar.js
    πŸ“ tests                    👸 root-level `tests` directory with parallel file structure
        πŸ“„ index.test.js
        πŸ“ lib
            πŸ“„ bar.test.js
{
  "testFileMagic.testDirectory": "tests",
  "testFileMagic.testDirectoryLocation": "root"
}

Tests in root-level directory with no keyword

πŸ“ [workspace root]
    πŸ“ src
        πŸ“„ index.js
        πŸ“ lib
            πŸ“„ bar.js
    πŸ“ tests
        πŸ“„ index.js             👸 test files don't include a `test` or `spec` keyword
        πŸ“ lib
            πŸ“„ bar.js           👸
{
  "testFileMagic.testKeyword": "",
  "testFileMagic.testDirectory": "tests",
  "testFileMagic.testDirectoryLocation": "root"
}

Cutomizing the template for new test files

If you try to go to a test file that doesn't exist, the file will be created. For example, if you are in an untested file called foo.ts and you invoke this extension, you'll get a new file like this:

import { foo } from './foo'

describe('foo', () => {
  it('should work', () => {
    
  })
})

This template is defined in settings as an array of strings.

[
  "import { ${moduleName} } from '${modulePath}'",
  "",
  "describe('${moduleName}', () => {",
  "  it('should work', () => {",
  "",
  "  })",
  "})"
]
  • ${moduleName} will be replaced the current filename, without the extension.
  • ${modulePath} will be replaced with the relative path from the new test file to the source file.

FAQs

Package last updated on 25 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts