JestGen
JestGen is a powerful and versatile extension for developers that automates the process of creating unit test files for Jest testing framework. It dynamically generates test templates based on your JavaScript or TypeScript source files, enabling you to focus more on writing test cases, rather than setting up the test files.
Start using JestGen today and experience a more efficient, enjoyable testing workflow!
- Features
- Usages
- Requirements
- Extension Settings
- Release Notes
- Issues
- Contribution
Features
- Automated Test File Generation: JestGen automatically generates a Jest test file template based on the source file you select. It takes care of all the setup, allowing you to get straight into writing test cases.
- Dynamic Template Generation: Test templates are dynamically generated based on the source file properties, such as the file name and function names. This allows your test files to reflect the structure and content of your source files, making your tests more intuitive and easier to manage.
- Custom Comment Parsing: JestGen uses AST parsing to extract user-defined descriptions and expectations from comments in the source file. You can use special tags to indicate the description of what a function does and what it should do, and JestGen will incorporate this information into the test file.
- Configurable Test File Naming and Structure: The extension is highly configurable, allowing you to specify the naming conventions for test files and the directory structure. You can set up JestGen to fit seamlessly into your existing workflow and project structure.
- Test Case Update: JestGen identifies and updates existing test cases when changes are made in source file comments. It eliminates the need for manual updates and ensures your test files are always up-to-date.
More features are coming!
Usages
Using JestGen is intuitive and straightforward. There are several ways to generate Jest test templates:
- Keyboard Shortcut: Simply select (a single click will suffice) the name of the method you want to generate a test case for and press
cmd+alt+t
. JestGen will automatically generate a corresponding test file.
- Context Menu: You can also generate a Jest test file by double-clicking the method name and then selecting Generate Jest File from the right-click context menu.
- Dynamic Generation with Comments: JestGen can enhance your tests by dynamically generating descriptive content from comments placed above your methods. By default, the generated test case doesn't come with descriptions. If you want to provide more context to your test cases, consider adding special comments.
It is recommended that whichever generation method is used, it be used in conjunction with the annotation method. Not only will this give you richer test templates, it also improves the readability of your code.
Here's an example of how to format your comments for dynamic generation:
/**
* loginUser
*
* @JestGen.describe: When user logs in to the platform
* @JestGen.it: Should successfully authenticate user credentials
*/
export const loginUser = async () => {
// method body...
}
The function name is required in comments!
Requirements
In order to use JestGen effectively, you will need to have the Jest testing framework installed in your workspace. If you haven't installed Jest yet, you can do so by using the following npm command:
npm install --save-dev jest ts-jest @types/jest
Extension Settings
JestGen is designed to be versatile and adaptable to your specific needs. You can use it out of the box with its default settings to generate Jest test templates, or customize its behavior through a .jestgen.json
configuration file in the root directory of your workspace.
Here's a part of the currently supported configuration options:
useCustomTemplate
: A boolean value that determines whether to use a custom template for generating the test files. Defaults to false.
customTemplatePath
: A string value representing the path to the custom template file. This option is used only if useCustomTemplate is set to true.
useSupertest
: A boolean value indicating whether to use Supertest in the test files. Supertest is a high-level abstraction for testing HTTP, useful for testing your Express.js server.
appPath
: A string value representing the relative path to the app file. This is required if useSupertest is set to true.
More configuration options can be found in Configuration
Below is an example configuration:
{
"useCustomTemplate": true,
"customTemplatePath": "/path/to/your/template.txt",
"customPlaceholders": [
["${customStr}", "This is for test"],
["${importRelativePath}", "${relativePathLevel}"]
],
"relativePathLevel": "../../.."
}
Due to the different project structures, it is recommended to use absolute paths for all configuration paths.
Release Notes
More change details can be found in CHANGELOG
1.0.4
Add custom placeholders in custom templates
1.0.3
Fix some issues
1.0.2
Change the test file path
1.0.1
Upload extension icon
1.0.0
Initial release of JestGen
Issues
Submit the issues if you find any bug or have any suggestion.
Contribution
Fork the repo and submit pull requests.