Test generator is a VSCode Extension which quickly generate test files from Typescript source files.
You can browse and install extensions from within VS Code. Press Ctrl+P
and narrow down the list commands by typing ext install dotup-vscode-test-generator
.
Or download from Visual Studio Marketplace
Open a typescript file with one or more class definitions and press Ctrl+Shift+P
to see all commands and start typing Generate test file
and hit Enter
.
Select a typescript file in the explorer, open context menu
and select Generate test file
.
This extension contributes the following settings:
dotupGenerateTestFile.testFileLocation
: Where should test files be created? Either insource file path
or inworkspace root path
.dotupGenerateTestFile.sourceDirectoryName
: Name of directory with source files. E.g.src
dotupGenerateTestFile.testDirectoryName
: Name of the test directory. Only required when usingworkspace root path
as test file location.dotupGenerateTestFile.testFileSuffix
: Suffix for test files. E.g.Suffix
NiceService.ts -> NiceService.Suffix.ts
-
dotupGenerateTestFile.template.file
Template for the whole test file.
Default value:
"default": [
"${Imports}",
"",
"${FileTests}"
]
Placeholders:
- ${Imports} - Place to put import statements
- ${FileTests} - Place to put test statements ( Test statements for one or more classes.)
-
dotupGenerateTestFile.template.class
Template for one class.
Placeholder:
- ${ClassName} - Replaced with the current class name
- ${ClassTests} - Test statements for current class
Sample configuration:
"default": [
"describe('Test class ${ClassName}', () => {",
"${ClassTests}",
"});",
""
],
-
dotupGenerateTestFile.template.method
Template for one method call.
ClassName.MethodName();
-
dotupGenerateTestFile.template.asyncmethod
Template for one async method call. The generated code contains the
async
keyword, something like this:
await ClassName.MethodName();
Placeholder:
- ${ClassName} - Replaced with the current class name
- ${MethodName} - Current method name to test
- ${Declarations} - Variable declaration for current test
- ${MemberTests} - Method calls
Sample configuration:
"default": [
"it('${MethodName}', () => {",
"// Arguments",
"${Declarations}",
"",
"// Method call",
"${MemberTests}",
"});"
]
-
dotupGenerateTestFile.template.function
:Template for one method call with return value.
const MethodCallResult = ClassName.MethodName();
-
dotupGenerateTestFile.template.asyncfunction
:Template for one async method call with return value. The generated code contains the
async
keyword, something like this:
const MethodCallResult = async ClassName.MethodName();
Placeholders:
- ${ClassName} - Replaced with the current class name
- ${MethodName} - Current method name to test
- ${Declarations} - Variable declaration for current test
- ${MemberTests} - Method calls
- ${MethodCallResult} - Replaced with method call result variable
Sample configuration:
"default": [
"it('${ClassName}-${MethodName}', () => {",
"// Arguments",
"${Declarations}",
"",
"// Method call",
"${MemberTests}",
"",
"// Expect result",
"expect(${MethodCallResult}).to.be.not.undefined;",
"});"
]
-
dotupGenerateTestFile.template.property
Template for one property call.
Placeholders:
- ${ClassName} - Replaced with the current class name
- ${PropertyName} - Current method name to test
- ${Declarations} - Variable declaration for current test
- ${MemberTests} - Method calls
- ${PropertyGetResult} - Replaced with property get result
- ${PropertySetVariable} - Replaced with the variable name, assigned to the property
Sample configuration:
"default": [
"it('${ClassName}-${MethodName}', () => {",
"// Arguments",
"${Declarations}",
"",
"// Method call",
"${MemberTests}",
"",
"// Expect result",
"expect(${PropertyGetResult}).equals(${PropertySetVariable});",
"});"
]
{
"dotupGenerateTestFile.testFileLocation": "test directory",
"dotupGenerateTestFile.sourceDirectoryName": "src",
"dotupGenerateTestFile.testDirectoryName": "test",
"dotupGenerateTestFile.testFileSuffix": "g.test",
"dotupGenerateTestFile.template.file": [
"import { expect } from 'chai';",
"${Imports}",
"",
"${FileTests}"
],
"dotupGenerateTestFile.template.class": [
"describe('Test class ${ClassName}', () => {",
"",
"${ClassTests}",
"});",
""
],
"dotupGenerateTestFile.template.method": [
"it('${ClassName}.${MethodName}()', () => {",
"// Arguments",
"${Declarations}",
"",
"// Method call",
"${MemberTests}",
"});",
""
],
"dotupGenerateTestFile.template.asyncMethod": [
"it('${ClassName}.${MethodName}()', async () => {",
"// Arguments",
"${Declarations}",
"",
"// Method call",
"${MemberTests}",
"});",
""
],
"dotupGenerateTestFile.template.function": [
"it('${ClassName}.${MethodName}()', () => {",
"// Arguments",
"${Declarations}",
"",
"// Method call",
"${MemberTests}",
"",
"// Expect result",
"expect(${MethodCallResult}).to.be.not.undefined;",
"});",
""
],
"dotupGenerateTestFile.template.asyncFunction": [
"it('${ClassName}.${MethodName}()', async() => {",
"// Arguments",
"${Declarations}",
"",
"// Method call",
"${MemberTests}",
"",
"// Expect result",
"expect(${MethodCallResult}).to.be.not.undefined;",
"});",
""
],
"dotupGenerateTestFile.template.property": [
"it('${ClassName}.${PropertyName}', () => {",
"// Arguments",
"${Declarations}",
"",
"// Property call",
"${MemberTests}",
"",
"// Expect result",
"expect(${PropertyGetResult}).equals(${PropertySetVariable});",
"});",
""
]
}
This is just a prototype to see if there is interest in such a tool.
Please give me feedback!