This module contains all functions required to create a DataWeave test

Example

Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
%dw 2.0
import * from dw::test::Tests
---
 "Matcher api" describedBy [
     "It should support nested matching" in  do {
         var payload = {}
         ---
         payload must [
             beObject(),
             $.foo must [
                 beNull()
             ]
         ]
     },
     "It should support simple matching" in do {
         var payload = {}
         ---
         payload must beObject()
     },

     "It should support multiple root cases" in do {
         var payload = {}
         var flowVar = {a: 123}
         ---
         [
             payload must beObject(),
             flowVar must [
                 beObject(),
                 $.a must equalTo(123)
             ]
         ]
     },
     "It should support using custom assertions" in  do {
         var payload = []
         ---
         payload must sizeOf($) > 2
     }
 ]

Functions

describedBy

describedBy(suite: String, testsToRun: Array<() -> TestResult>): TestResult

Defines a new test suite with the list of test cases.

Example
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
%dw 2.0
import * from dw::test::Tests
 ---

 "Matcher api" describedBy [
     "It should support nested matching" in  do {
         var payload = {}
         ---
         payload must [
             beObject(),
             $.foo must [
                 beNull()
             ]
         ]
     },
]

evalPath

evalPath(dwlFilePath: String, context: { _?: Any }, mimeType: String): Any

Runs a specific mapping with the given context and mimetype.

Parameters
Name Description

dwlFilePath

Path inside the classpath where the runnable dataweave file is present.

context

Object which contains different input values (each input name would be the key of the object).

mimeType

The expected output mimetype.

Example
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import * from dw::test::Tests
import * from dw::test::Asserts
---
"Test MyMapping" describedBy [
    "Assert SimpleScenario" in do {
        evalPath("MyMapping.dwl", inputsFrom("MyMapping/SimpleScenario"), "application/json" ) must
                  equalTo(outputFrom("MyMapping/SimpleScenario"))
    }
 ]

evalPath(testUrl: { content: String, url: String }, context: { _?: Any }, mimeType: String): Any

in

in(testName: String, testCases: () -> MatcherResult): TestResult

Defines a new test case inside a test suite with a single assertion.

Example
Source
1
2
3
"It should support nested matching" in  do {
   "foo" must beString()
}

in(testName: String, callback: Array<() -> MatcherResult>): TestResult

Defines a new test case with multiple assertions

Example
Source
1
2
3
4
5
6
7
8
9
10
11
12
 "It should support multiple root cases" in do {
     var payload = {}
     var flowVar = {a: 123}
     ---
    [
        payload must beObject(),
        flowVar must [
             beObject(),
             $.a must equalTo(123)
          ]
      ]
 }

in(testName: String, callback: Array<() -> MatcherResult>, skipAll: Boolean): TestResult

inputsFrom

inputsFrom(dir: String): { _?: Any }

Builds an object with all the inputs to be used as context for a specific mapping.

Parameters
Name Description

dir

Directory where to look for the inputs folder and build the context from.

outputFrom

outputFrom(dir: String)

Returns the result of reading the expected output

Parameters
Name Description

dir

Directory where to look for the out file to make assertions on your mapping test.

Variables

ERROR_STATUS

FAIL_STATUS

OK_STATUS

SKIP_STATUS

Types

TEST_STATUS

Data Type that describes the result of a Test Execution

Definition
1
"ERROR" | "OK" | "FAIL" | "SKIP"

TestResult

Definition
1
{ name: String, time: Number, status: TEST_STATUS, tests?: Array<TestResult>, errorMessage?: String, location?: Location }