Import Javascript files
This feature is available in the paid version.
- You can import functions from other js files. Useful for code re-use and check-in to your git repo.
Create JS File
- Create a new JS file in the workspace.
- Add the function you want to import in the file.
- Export the function using
module.exports
test-import.js
// test-import.js file
function helloWorld(){
return "hello world";
}
module.exports = {
helloWorld
}
Import JS File
- Import js file using
require
- Make sure to include the
.js
extension in the file path. - The path should be relative (right-click on the file and select
Copy Relative Path
) to the workspace if you are using Git-Sync. Otherwise, use the full path.
var { helloWorld } = require("thunder-tests/test-import.js");
var result = helloWorld();
console.log(result);
Save Path in Env Variable
- You can also save the path in the
Environment variable
, so you don't have to change the path in every request if you move the file to different folder.
var scriptPath = tc.getVar("scriptPath");
var {helloWorld} = require(scriptPath);
var result = helloWorld();
console.log(result);
Autocomplete
- To enable autocompletion, you can refer to the
tc-types.d.ts
file. - Copy the tc-types.d.ts file to your project and reference it at the top for VS Code autocomplete suggestions.
// @ts-check
/// copy tc-types.d.ts file for vscode autocompletion on tc object
/// <reference path="./tc-types.d.ts" />
TS Validation
- Use
// @ts-check
at the top of the script to enable TypeScript validation.