diff --git a/docs/sources/k6/next/get-started/write-your-first-test.md b/docs/sources/k6/next/get-started/write-your-first-test.md index 233fb7462..45c719973 100644 --- a/docs/sources/k6/next/get-started/write-your-first-test.md +++ b/docs/sources/k6/next/get-started/write-your-first-test.md @@ -45,25 +45,29 @@ Let’s walk through creating a simple test which performs 10 `GET` HTTP request 2. **Import k6 modules**: As the end goal here is to perform HTTP requests, import the k6 `http` module at the top of the file. To help simulate a real-world scenario, import the `sleep` function from the `k6` module as well. - ```javascript - // Import the http module to make HTTP requests. From this point, you can use `http` methods to make HTTP requests. - import http from 'k6/http'; + - // Import the sleep function to introduce delays. From this point, you can use the `sleep` function to introduce delays in your test script. - import { sleep } from 'k6'; - ``` +```javascript +// Import the http module to make HTTP requests. From this point, you can use `http` methods to make HTTP requests. +import http from 'k6/http'; + +// Import the sleep function to introduce delays. From this point, you can use the `sleep` function to introduce delays in your test script. +import { sleep } from 'k6'; +``` 3. **Define options**: To perform 10 HTTP requests, define an options block to configure the test execution. In this case, set the number of iterations to 10 to instruct k6 to execute the default function 10 times. Right beneath the imports, add the following code: - ```javascript - import http from 'k6/http'; - import { sleep } from 'k6'; + - export const options = { - // Define the number of iterations for the test - iterations: 10, - }; - ``` +```javascript +import http from 'k6/http'; +import { sleep } from 'k6'; + +export const options = { + // Define the number of iterations for the test + iterations: 10, +}; +``` 4. **Define a default function**: The default exported function is the entry point for the test script. It will be executed repeatedly the number of times you define with the `iterations` option. In this function, make a `GET` request to a URL and introduce a 1-second delay between requests. Add the following code to your script: diff --git a/docs/sources/k6/next/using-k6/modules.md b/docs/sources/k6/next/using-k6/modules.md index 70878b7ce..fd529dbfa 100644 --- a/docs/sources/k6/next/using-k6/modules.md +++ b/docs/sources/k6/next/using-k6/modules.md @@ -24,6 +24,8 @@ For example, the `http` client make requests against the system under test. For the full list of built-in modules, refer to the [API documentation](https://grafana.com/docs/k6//javascript-api). + + ```javascript import http from 'k6/http'; ``` @@ -120,6 +122,8 @@ The following options for distributing and sharing JavaScript libraries are avai You can host your modules in a public webserver like GitHub and any CDN and be imported remotely. + + ```javascript // As GitHub release assets import { @@ -142,6 +146,8 @@ Be aware that k6 automatically executes remote modules, making it crucial to tru In this example, the previous remote modules have been downloaded to the `lib` folder of the testing project and imported as follows: + + ```javascript import { WorkloadConfig, sayHello } from './libs/test-commons.js'; @@ -162,6 +168,8 @@ k6 isn't Node.js or a browser. Packages that rely on APIs provided by Node.js, f In a JavaScript project running Node.js, modules are imported using either `import` or `require()`, using the Node.js module resolution algorithm. This means that a user can import modules by name, without providing the full filesystem path to the module. For instance: + + ```javascript import { ClassInAModule } from 'cool-module'; ``` @@ -230,6 +238,8 @@ $ npm install --save-dev \ Once these packages have been added, the next step will be to set up a `webpack.config.js` file: + + ```javascript const path = require('path'); @@ -263,6 +273,8 @@ The files Webpack will use as its entry points while performing the bundling. Fr Webpack will automatically traverse all imports recursively until every possible dependency path has been exhausted. For instance: + + ```javascript // login.test.js @@ -273,6 +285,8 @@ const svc = new SomeService(); and: + + ```javascript // some.service.js @@ -384,6 +398,8 @@ export default function () { {{< code >}} + + ```javascript export function hello_world() { console.log('Hello world');