{"id":11078,"date":"2024-03-07T00:38:47","date_gmt":"2024-03-07T05:38:47","guid":{"rendered":"https:\/\/www.redline13.com\/blog\/?p=11078"},"modified":"2024-03-07T00:39:39","modified_gmt":"2024-03-07T05:39:39","slug":"importing-user-modules-in-k6","status":"publish","type":"post","link":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/","title":{"rendered":"Importing User Modules in k6"},"content":{"rendered":"\n<p><\/p>\n\n\n<p><a id=\"post-11078-_qeymqyf37w2t\"><\/a> <img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-11079\" src=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11078-1.png\" alt=\"Importing User Modules in k6\" width=\"400\" height=\"300\" srcset=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11078-1.png 400w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11078-1-300x225.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>In previous posts we have commented on <a href=\"https:\/\/www.redline13.com\/blog\/2024\/01\/top-5-most-useful-k6-extensions\/\" target=\"_blank\" rel=\"noopener\">extensibility<\/a> as a hallmark of the <a href=\"https:\/\/k6.io\/\" target=\"_blank\" rel=\"noopener\">k6<\/a> architecture. While there are a wide variety of official <a href=\"https:\/\/k6.io\/docs\/using-k6\/modules\/\" target=\"_blank\" rel=\"noopener\">modules<\/a> you can easily import into your k6 tests, it is also possible to import custom <a href=\"https:\/\/en.wikipedia.org\/wiki\/JavaScript\" target=\"_blank\" rel=\"noopener\">JavaScript<\/a> code. In this brief post we will show you how to easily import code from your own modules. This can potentially accelerate your development efforts by reusing these modules across multiple tests, while at the same time decluttering your scripts away from repetitive lines of code.<\/p>\n<h3><a id=\"post-11078-_v70izg70ewth\"><\/a>Creating Your Own Modules<\/h3>\n<p>Since k6 tests are built in JavaScript, you can create extension methods to perform virtually any task and have them available to your test plan. For the sake of an example, let\u2019s say we have a function that creates a special randomly generated identifier that we want to pass as parameter in our k6 requests:<\/p>\n<pre><strong>export function createRandomID(length) {<br \/><\/strong><strong>   const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';<br \/><\/strong><strong>   var id = '';<br \/><\/strong><strong>   var counter = 0;<br \/><\/strong><strong>   while (counter &lt; length) {<br \/><\/strong><strong>      id += characters.charAt(Math.floor(Math.random() * characters.length));<br \/><\/strong><strong>      counter++;<br \/><\/strong><strong>   }<br \/><\/strong><strong>   return id;<br \/><\/strong><strong>}<\/strong><\/pre>\n<p>This function can be called to generate a random identifier of any specified length. Note that we have decorated the method with the <code>export<\/code> keyword. Let\u2019s save this script and name the file \u201c<em>myLibrary.js<\/em>\u201d, which we will reference in the next step.<\/p>\n<h3><a id=\"post-11078-_6li7yril3utm\"><\/a>Importing Your Own Modules<\/h3>\n<p>Using the <a href=\"https:\/\/k6.io\/docs\/using-k6\/modules\/#local-filesystem-modules\" target=\"_blank\" rel=\"noopener\">filesystem module pattern<\/a>, you can bring your custom JavaScript code into k6 using the <code>import<\/code> statement. With the above file \u201c<em>myLibrary.js<\/em>\u201d alongside your test plan, let\u2019s add the following line of code to the top of our k6 test script:<\/p>\n<pre><strong>import { createRandomID } from '.\/myLibrary.js'<\/strong><\/pre>\n<p>Once this reference exists, you can now call the <code>createRandomID()<\/code> function from any place in your k6 test plan. You can use it when forming HTTP requests, filling out form fields, or any other k6 testing task.<\/p>\n<h3><a id=\"post-11078-_em3s4gcu713d\"><\/a>Uploading Your Custom Module to RedLine13<\/h3>\n<p>To use the above pattern in your k6 test plans on RedLine13, we simply need to upload the \u201c<em>myLibrary.js<\/em>\u201d file as an <a href=\"https:\/\/www.redline13.com\/blog\/docs\/attaching-extra-files\/\" target=\"_blank\" rel=\"noopener\">extra file<\/a> attachment:<\/p>\n<figure id=\"attachment_11080\" class=\"wp-caption aligncenter\" style=\"max-width: 650px\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-11080\" src=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11078-2.png\" alt=\"Adding an extra file as an attachment for your k6 test\" width=\"650\" height=\"86\" srcset=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11078-2.png 650w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11078-2-300x40.png 300w, https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11078-2-425x56.png 425w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><figcaption class=\"wp-caption-text\">Adding an extra file as an attachment for your k6 test.<\/figcaption><\/figure>\n<p>This will place your JavaScript file alongside your test for it to be accessible when running your k6 script.<\/p>\n<hr \/>\n<p>Did you know that RedLine13 offers a full-featured, time-limited free trial?\u00a0 <a href=\"https:\/\/www.redline13.com\/Service\" target=\"_blank\" rel=\"noopener\">Sign up now<\/a>, and jumpstart your k6 testing with RedLine13 today.<\/p>","protected":false},"excerpt":{"rendered":"<p>In previous posts we have commented on extensibility as a hallmark of the k6 architecture. While there are a wide variety of official modules you can easily import into your k6 tests, it is also possible to import custom JavaScript code. In this brief post we will show you how to easily import code from your own modules. This can potentially accelerate your development efforts by reusing these modules across multiple tests, while at the same<a class=\"more-link\" href=\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/\">Read More &rarr;<\/a><\/p>\n","protected":false},"author":11,"featured_media":11079,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[128,596,695,674,685,318,424],"class_list":{"0":"entry","1":"post","2":"publish","3":"author-dkoziel","4":"post-11078","6":"format-standard","7":"has-post-thumbnail","8":"category-blog","9":"category-uncategorized","10":"post_tag-cloud-load-testing","11":"post_tag-k6","12":"post_tag-k6-import","13":"post_tag-k6-modules","14":"post_tag-k6-tests","15":"post_tag-load-testing","16":"post_tag-redline13"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Importing User Modules in k6 - RedLine13<\/title>\n<meta name=\"description\" content=\"Extensibility is a hallmark of the k6 architecture. In this brief post we will show you how to easily import code from your own modules.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Importing User Modules in k6 - RedLine13\" \/>\n<meta property=\"og:description\" content=\"Extensibility is a hallmark of the k6 architecture. In this brief post we will show you how to easily import code from your own modules.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/\" \/>\n<meta property=\"og:site_name\" content=\"RedLine13\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-07T05:38:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-07T05:39:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11078-1.png\" \/>\n\t<meta property=\"og:image:width\" content=\"400\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"David Koziel\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"David Koziel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/\"},\"author\":{\"name\":\"David Koziel\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\"},\"headline\":\"Importing User Modules in k6\",\"datePublished\":\"2024-03-07T05:38:47+00:00\",\"dateModified\":\"2024-03-07T05:39:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/\"},\"wordCount\":368,\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"keywords\":[\"Cloud Load Testing\",\"k6\",\"k6 import\",\"k6 modules\",\"k6 tests\",\"Load Testing\",\"RedLine13\"],\"articleSection\":[\"Blog\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/\",\"name\":\"Importing User Modules in k6 - RedLine13\",\"isPartOf\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\"},\"datePublished\":\"2024-03-07T05:38:47+00:00\",\"dateModified\":\"2024-03-07T05:39:39+00:00\",\"description\":\"Extensibility is a hallmark of the k6 architecture. In this brief post we will show you how to easily import code from your own modules.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.redline13.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Importing User Modules in k6\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#website\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"name\":\"RedLine13\",\"description\":\"(Almost) Free Load Testing in the Cloud\",\"publisher\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.redline13.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#organization\",\"name\":\"RedLine13\",\"url\":\"https:\/\/www.redline13.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"contentUrl\":\"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg\",\"width\":300,\"height\":68,\"caption\":\"RedLine13\"},\"image\":{\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20\",\"name\":\"David Koziel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g\",\"caption\":\"David Koziel\"},\"url\":\"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Importing User Modules in k6 - RedLine13","description":"Extensibility is a hallmark of the k6 architecture. In this brief post we will show you how to easily import code from your own modules.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/","og_locale":"en_US","og_type":"article","og_title":"Importing User Modules in k6 - RedLine13","og_description":"Extensibility is a hallmark of the k6 architecture. In this brief post we will show you how to easily import code from your own modules.","og_url":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/","og_site_name":"RedLine13","article_published_time":"2024-03-07T05:38:47+00:00","article_modified_time":"2024-03-07T05:39:39+00:00","og_image":[{"width":400,"height":300,"url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2024\/03\/word-image-11078-1.png","type":"image\/png"}],"author":"David Koziel","twitter_card":"summary_large_image","twitter_misc":{"Written by":"David Koziel","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/#article","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/"},"author":{"name":"David Koziel","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20"},"headline":"Importing User Modules in k6","datePublished":"2024-03-07T05:38:47+00:00","dateModified":"2024-03-07T05:39:39+00:00","mainEntityOfPage":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/"},"wordCount":368,"publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"keywords":["Cloud Load Testing","k6","k6 import","k6 modules","k6 tests","Load Testing","RedLine13"],"articleSection":["Blog"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/","url":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/","name":"Importing User Modules in k6 - RedLine13","isPartOf":{"@id":"https:\/\/www.redline13.com\/blog\/#website"},"datePublished":"2024-03-07T05:38:47+00:00","dateModified":"2024-03-07T05:39:39+00:00","description":"Extensibility is a hallmark of the k6 architecture. In this brief post we will show you how to easily import code from your own modules.","breadcrumb":{"@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.redline13.com\/blog\/2024\/03\/importing-user-modules-in-k6\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.redline13.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Importing User Modules in k6"}]},{"@type":"WebSite","@id":"https:\/\/www.redline13.com\/blog\/#website","url":"https:\/\/www.redline13.com\/blog\/","name":"RedLine13","description":"(Almost) Free Load Testing in the Cloud","publisher":{"@id":"https:\/\/www.redline13.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.redline13.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.redline13.com\/blog\/#organization","name":"RedLine13","url":"https:\/\/www.redline13.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","contentUrl":"https:\/\/www.redline13.com\/blog\/wp-content\/uploads\/2013\/06\/cropped-rl13-header-logo.jpg","width":300,"height":68,"caption":"RedLine13"},"image":{"@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/51d282221e3230ab35f964f98ada9b20","name":"David Koziel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.redline13.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2babf644e0993fc86893c24d7525f1e3be114a8746c01249797f25587ae1697a?s=96&d=mm&r=g","caption":"David Koziel"},"url":"https:\/\/www.redline13.com\/blog\/author\/dkoziel\/"}]}},"_links":{"self":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/comments?post=11078"}],"version-history":[{"count":2,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11078\/revisions"}],"predecessor-version":[{"id":11083,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/posts\/11078\/revisions\/11083"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media\/11079"}],"wp:attachment":[{"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/media?parent=11078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/categories?post=11078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.redline13.com\/blog\/wp-json\/wp\/v2\/tags?post=11078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}