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: Object, 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 default mimetype if not specified on the file

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: Object, mimeType: String): Any

Evals a test with a given input values as a context and using the specified mimeType as default one when not specified in the file

Parameters
Name Type Description

testUrl

{ content: String, url: String }

The test

context

Object

The inputs

mimeType

String

The default mimetype

in

in<Ctx <: Object>(testSetup: { config: TestConfig<Ctx>, testName: String }, test: (c: Ctx) -> MatcherResult): TestResult

Defines a new test case inside a test suite with its relevant context. Intended to be used in combination with withConfig

Example
Source
1
2
3
4
5
6
7
8
9
var config = {
  setup: () -> { contextString: "context" },
  teardown: () -> {}
}
---
"It should generate context for following tests" withConfig config in do {
  $.contextString must beString()
 }

in<Ctx <: Object>(testSetup: { config: TestConfig<Ctx>, testName: String }, test: Array<(c: Ctx) -> MatcherResult>): TestResult

Defines multiple new test cases inside a test suite that share the same context. Intended to be used in combination with withConfig

Example
Source
1
2
3
4
5
6
7
8
9
10
var config = {
  setup: () -> { contextString: "context" },
  teardown: () -> {}
}
---
"It should generate context for following tests" withConfig config in  [
  do { $.contextString must beString() },
  do { $.otherContext must equalTo(3) }
]

in<T>(testName: String, testCases: (Null) -> 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<(Null) -> 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)
          ]
      ]
 }

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.

withConfig

withConfig<Ctx <: Object>(testName: String, config: TestConfig<Ctx>)

Generates configuration for a test(s) that needs setup/teardown stages. Intended for it to used in combination with the in function.

Example
Source
1
2
3
4
5
6
7
8
9
10
var config = {
  setup: () -> { contextString: "context", otherContext: 3 },
  teardown: () -> {}
}
---
"It should generate context for following tests" withConfig config in  [
  do { $.contextString must beString() },
  do { $.otherContext must equalTo(3) }
]

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"

TestConfig

Definition
1
{| setup: () -> Ctx, teardown: (c: Ctx) -> Any |}

TestResult

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

Annotations

@Skip(reason: String)