├── .npmignore ├── .gitignore ├── karate ├── karate-config.js └── httpbin.feature ├── test.js ├── preinstall.js ├── package.json ├── karate.js └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | *.tgz 2 | karate/ 3 | target/ 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | *.tgz 3 | package-lock.json 4 | target/ 5 | node_modules/ 6 | -------------------------------------------------------------------------------- /karate/karate-config.js: -------------------------------------------------------------------------------- 1 | function fn() { 2 | karate.log('*** in karate-config.js'); 3 | } -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | var karate = require('./karate'); 3 | karate.version = '1.5.0'; 4 | karate.config.dir = 'karate' 5 | karate.exec(); 6 | -------------------------------------------------------------------------------- /preinstall.js: -------------------------------------------------------------------------------- 1 | const karate = require('./karate'); 2 | console.log('pre-installing karate ...'); 3 | karate.exec(''); 4 | console.log('pre-install of karate complete'); 5 | -------------------------------------------------------------------------------- /karate/httpbin.feature: -------------------------------------------------------------------------------- 1 | Feature: Simple Requests 2 | 3 | Scenario: Simple POST 4 | * url 'https://httpbin.org' 5 | * path 'anything' 6 | * request { foo: 'bar' } 7 | * method post 8 | * status 200 9 | * match response contains { json: { foo: 'bar' } } 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@karatelabs/karate", 3 | "version": "0.3.2", 4 | "description": "NPM for Karate", 5 | "homepage": "https://github.com/karatelabs/karate-npm", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/karatelabs/karate-npm.git" 9 | }, 10 | "main": "karate.js", 11 | "scripts": { 12 | "test": "node test.js", 13 | "preinstall": "node preinstall.js" 14 | }, 15 | "author": "Peter Thomas", 16 | "license": "MIT", 17 | "dependencies": { 18 | "@jbangdev/jbang": "^0.2.3" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /karate.js: -------------------------------------------------------------------------------- 1 | const jbang = require('@jbangdev/jbang'); 2 | const karate = {}; 3 | karate.version = 'LATEST'; 4 | karate.config = {}; 5 | karate.jvm = {args:''}; 6 | karate.executable = function () { 7 | let prefix = karate.config.dir ? '-Dkarate.config.dir=' + karate.config.dir + ' ' : ''; 8 | return prefix + 'io.karatelabs:karate-core:' + karate.version + ':all'; 9 | }; 10 | karate.exec = function (args) { 11 | args = (!args ? '' : args + ' ') + process.argv.slice(2).join(' '); 12 | process.env['KARATE_META'] = 'npm:' + process.env.npm_package_version; 13 | jbang.exec(karate.jvm.args + ' ' + karate.executable() + ' ' + args); 14 | }; 15 | module.exports = karate; 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @karatelabs/karate 2 | Seamlessly use the power of [Karate](https://github.com/karatelabs/karate) from Node / JS projects. 3 | 4 | ## Usage 5 | Given this script `test.js`: 6 | 7 | ```js 8 | #! /usr/bin/env node 9 | const karate = require('@karatelabs/karate'); 10 | karate.exec(); 11 | ``` 12 | 13 | And in `package.json` (use the latest version from [npm](https://www.npmjs.com/package/@karatelabs/karate)): 14 | 15 | ```json 16 | { 17 | "scripts": { 18 | "test": "node test.js" 19 | }, 20 | "devDependencies": { 21 | "@karatelabs/karate": "^0.2.2" 22 | } 23 | } 24 | ``` 25 | 26 | When you run `npm install`, [jbang](https://www.jbang.dev/) and other Karate dependencies needed will be installed via [`jbang-npm`](https://github.com/jbangdev/jbang-npm). 27 | 28 | And to run a single test: 29 | 30 | ``` 31 | npm run test karate/httpbin.feature 32 | ``` 33 | 34 | Or to run all tests in a folder: 35 | 36 | ``` 37 | npm run test karate 38 | ``` 39 | 40 | ## Known Issues 41 | Users on Windows have reported [issues](https://github.com/karatelabs/karate-npm/issues/2) such as the `npm install` failing to complete and without any errors shown. 42 | 43 | Please do contribute if you can and improve how JavaScript projects can integrate smoothly with Java projects ! 44 | 45 | As a workaround, please [install JBang manually](https://www.jbang.dev/documentation/guide/latest/installation.html) and re-try the `npm install` step. 46 | 47 | ## Setting Karate Version 48 | 49 | To use a specific version of Karate, just set `karate.version` before calling `karate.exec()`: 50 | 51 | ```js 52 | #! /usr/bin/env node 53 | const karate = require('@karatelabs/karate'); 54 | karate.version = '1.5.0'; 55 | karate.exec(); 56 | ``` 57 | 58 | ## CLI Reference 59 | All Karate capabilities can be invoked by the command-line. 60 | 61 | The most common needs are to: 62 | 63 | * run feature-file(s) or all feature-files in a folder as Karate tests 64 | * start an API mock-server 65 | 66 | The complete documentation can be found [here](https://github.com/karatelabs/karate/tree/master/karate-netty#usage). 67 | 68 | You can also use the `--help` command-line option to see all the possible options and brief descriptions on the console. 69 | 70 | ### `karate-config.js` 71 | 72 | Karate will look for a [`karate-config.js`](https://github.com/karatelabs/karate#configuration) file in the current working directory. 73 | 74 | But if you need to point to a different directory, you can set `karate.config.dir` before calling `karate.exec()` 75 | 76 | To pass arguments to the JVM use the jvm object `karate.jvm.args` 77 | 78 | CLI options can be passed as a string to the `karate.exec('-T=5')` method. 79 | 80 | ```js 81 | #! /usr/bin/env node 82 | const karate = require('@karatelabs/karate'); 83 | karate.config.dir = '/users/myname/some/dir'; 84 | karate.jvm.args = `-Dlogback.configurationFile=${__dirname}/logback-test.xml`; 85 | karate.exec("-T=5"); 86 | ``` 87 | 88 | ## Custom Java Classpath 89 | 90 | For teams that want to customize the Java classpath by adding libraries or custom-code, please refer to the wiki: [Custom Fat JAR](https://github.com/karatelabs/karate/wiki/Get-Started:-Other-Runtime-Options#custom-fat-jar). This may be easier to achieve by using Maven to prepare a Docker image, which can then be used by teams on the command-line without needing NPM, Java or Karate - and only Docker is a pre-requisite. 91 | 92 | If you want to use additional Java libraries or custom code *and* avoid Maven and Java dev-tools, refer to this example: [Using Java Libraries with NPM](https://github.com/karatelabs/karate-examples/blob/main/jbang-npm/README.md). 93 | --------------------------------------------------------------------------------