0.7.13 ā€¢ Published 4 years ago

test-file-magic v0.7.13

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

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.
0.7.13

4 years ago

0.7.10

5 years ago

0.7.9

5 years ago

0.7.8

5 years ago

0.7.7

5 years ago

0.7.5

5 years ago

0.7.4

5 years ago

0.7.3

5 years ago

0.7.2

5 years ago

0.7.1

5 years ago

0.7.0

5 years ago