Class: Test
Main class for GameTest functions, with helpers and data for manipulating the respective test. Note that all methods of this class expect BlockLocations and Locations relative to the GameTest structure block.
Constructors
new Test()
private
new Test():Test
Returns
Methods
assert()
assert(
condition
,message
):void
Parameters
Parameter | Type | Description |
---|---|---|
condition | boolean | Expression of the condition to evaluate. |
message | string | Message that is passed if the condition does not evaluate to true. |
Returns
void
Remarks
Tests that the condition specified in condition is true. If not, an error with the specified message is thrown.
Throws
This function can throw errors.
assertBlockPresent()
assertBlockPresent(
blockType
,blockLocation
,isPresent
?):void
Parameters
Parameter | Type | Description |
---|---|---|
blockType | string | BlockType | Expected block type. |
blockLocation | Vector3 | Location of the block to test at. |
isPresent ? | boolean | If true, this function tests whether a block of the specified type is at the location. If false, tests that a block of the specified type is not present. |
Returns
void
Remarks
Tests that a block of the specified type is present at the specified location. If it is not, an exception is thrown.
Throws
This function can throw errors.
assertBlockState()
assertBlockState(
blockLocation
,callback
):void
Parameters
Parameter | Type | Description |
---|---|---|
blockLocation | Vector3 | Location of the block to test at. |
callback | (arg ) => boolean | Callback function that contains additional tests based on the block at the specified location. |
Returns
void
Remarks
Tests that a block has a particular state value at the specified location. If it does not have that state value, an exception is thrown.
Throws
This function can throw errors.
Example
test.assertBlockState(buttonPos, (block) => {
return block.permutation.getProperty("button_pressed_bit") == 0;
});
assertCanReachLocation()
assertCanReachLocation(
mob
,blockLocation
,canReach
?):void
Parameters
Parameter | Type | Description |
---|---|---|
mob | Entity | Entity that you wish to test the location against. |
blockLocation | Vector3 | Structure-relative location to test whether the specified mob can reach. |
canReach ? | boolean | If true, tests whether the mob can reach the location. If false, tests whether the mob is not able to reach the location. |
Returns
void
Remarks
Tests that an entity can reach a particular location. Depending on the value of canReach, throws an exception if the condition is not met.
Throws
This function can throw errors.
assertContainerContains()
assertContainerContains(
itemStack
,blockLocation
):void
Parameters
Parameter | Type | Description |
---|---|---|
itemStack | ItemStack | Represents the type of item to check for. The specified container must contain at least 1 item matching the item type defined in itemStack. |
blockLocation | Vector3 | Location of the block with a container (for example, a chest) to test the contents of. |
Returns
void
Remarks
Tests that a container (e.g., a chest) at the specified location contains a specified of item stack. If not, an error is thrown.
Throws
This function can throw errors.
assertContainerEmpty()
assertContainerEmpty(
blockLocation
):void
Parameters
Parameter | Type | Description |
---|---|---|
blockLocation | Vector3 | Location of the block with a container (for example, a chest) to test is empty of contents. |
Returns
void
Remarks
Tests that a container (e.g., a chest) at the specified location is empty. If not, an error is thrown.
Throws
This function can throw errors.
assertEntityHasArmor()
assertEntityHasArmor(
entityTypeIdentifier
,armorSlot
,armorName
,armorData
,blockLocation
,hasArmor
?):void
Parameters
Parameter | Type | Description |
---|---|---|
entityTypeIdentifier | string | Identifier of the entity to match (e.g., 'minecraft:skeleton'). |
armorSlot | number | Container slot index to test. |
armorName | string | Name of the armor to look for. |
armorData | number | Data value integer to look for. |
blockLocation | Vector3 | Location of the entity with armor to test for. |
hasArmor ? | boolean | Whether or not the entity is expected to have the specified armor equipped. |
Returns
void
Remarks
Tests that an entity has a specific piece of armor equipped. If not, an error is thrown.
Throws
This function can throw errors.
Example
test.assertEntityHasArmor("minecraft:horse", armorSlotTorso, "diamond_horse_armor", 0, horseLocation, true);
assertEntityHasComponent()
assertEntityHasComponent(
entityTypeIdentifier
,componentIdentifier
,blockLocation
,hasComponent
?):void
Parameters
Parameter | Type | Description |
---|---|---|
entityTypeIdentifier | string | Identifier of the specified entity (e.g., 'minecraft:skeleton'). If the namespace is not specified, 'minecraft:' is assumed. |
componentIdentifier | string | Identifier of the component to check for. If the namespace is not specified, 'minecraft:' is assumed. |
blockLocation | Vector3 | Location of the block with a container (for example, a chest.) |
hasComponent ? | boolean | Determines whether to test that the component exists, or does not. |
Returns
void
Remarks
Tests that an entity has a particular component. If not, an exception is thrown.
Throws
This function can throw errors.
Example
test.assertEntityHasComponent("minecraft:sheep", "minecraft:is_sheared", entityLoc, false);
assertEntityInstancePresent()
assertEntityInstancePresent(
entity
,blockLocation
,isPresent
?):void
Parameters
Parameter | Type | Description |
---|---|---|
entity | Entity | Specific entity to test for. |
blockLocation | Vector3 | Location of the entity to test for. |
isPresent ? | boolean | Whether to test that an entity is present or not present at the specified location. |
Returns
void
Remarks
Depending on the value for isPresent, tests that a particular entity is present or not present at the specified location. Depending on the value of isPresent, if the entity is found or not found, an error is thrown.
Throws
This function can throw errors.
assertEntityInstancePresentInArea()
assertEntityInstancePresentInArea(
entity
,isPresent
?):void
Parameters
Parameter | Type | Description |
---|---|---|
entity | Entity | Entity instance to test for. |
isPresent ? | boolean | If true, this function tests whether the specified entity is present in the GameTest area. If false, tests that the specified entity is not present. |
Returns
void
Remarks
Tests that an entity instance is present within the GameTest area. If not, an exception is thrown.
Throws
This function can throw errors.
Example
import * as gameTest from '@minecraft/server-gametest';
gameTest
.register('StarterTests', 'simpleMobTest', (test: gameTest.Test) => {
const attackerId = 'fox';
const victimId = 'chicken';
test.spawn(attackerId, { x: 5, y: 2, z: 5 });
const victim = test.spawn(victimId, { x: 2, y: 2, z: 2 });
test.assertEntityInstancePresentInArea(victim, true);
test.succeedWhen(() => {
test.assertEntityInstancePresentInArea(victim, false);
});
})
.maxTicks(400)
.structureName('gametests:mediumglass');
assertEntityPresent()
assertEntityPresent(
entityTypeIdentifier
,blockLocation
,searchDistance
?,isPresent
?):void
Parameters
Parameter | Type | Description |
---|---|---|
entityTypeIdentifier | string | Type of entity to test for (e.g., 'minecraft:skeleton'). If an entity namespace is not specified, 'minecraft:' is assumed. |
blockLocation | Vector3 | Location of the entity to test for. |
searchDistance ? | number | The distance to search for the entity from the blockLocation. |
isPresent ? | boolean | If true, this function tests whether an entity of the specified type is present. If false, tests that an entity of the specified type is not present. |
Returns
void
Remarks
Depending on the value of isPresent, tests for the presence or non-presence of entity of a specified type at a particular location. If the condition is not met, an exception is thrown.
Throws
This function can throw errors.
assertEntityPresentInArea()
assertEntityPresentInArea(
entityTypeIdentifier
,isPresent
?):void
Parameters
Parameter | Type | Description |
---|---|---|
entityTypeIdentifier | string | Type of entity to test for (e.g., 'minecraft:skeleton'). If an entity namespace is not specified, 'minecraft:' is assumed. |
isPresent ? | boolean | If true, this function tests whether an entity of the specified type is present in the GameTest area. If false, tests that an entity of the specified type is not present. |
Returns
void
Remarks
Tests that an entity of a specified type is present within the GameTest area. If not, an exception is thrown.
Throws
This function can throw errors.
Example
import * as gameTest from '@minecraft/server-gametest';
gameTest
.register('StarterTests', 'simpleMobTest', (test: gameTest.Test) => {
const attackerId = 'fox';
const victimId = 'chicken';
test.spawn(attackerId, { x: 5, y: 2, z: 5 });
test.spawn(victimId, { x: 2, y: 2, z: 2 });
test.assertEntityPresentInArea(victimId, true);
test.succeedWhen(() => {
test.assertEntityPresentInArea(victimId, false);
});
})
.maxTicks(400)
.structureName('gametests:mediumglass');
assertEntityState()
assertEntityState(
blockLocation
,entityTypeIdentifier
,callback
):void
Parameters
Parameter | Type | Description |
---|---|---|
blockLocation | Vector3 | Location of the entity to look for. |
entityTypeIdentifier | string | Identifier of the entity (e.g., 'minecraft:skeleton') to look for. Note if no namespace is specified, 'minecraft:' is assumed. |
callback | (arg ) => boolean | Callback function where facets of the selected entity can be tested for. If this callback function returns false or no entity with the specified identifier is found, an exception is thrown. |
Returns
void
Remarks
Tests that an entity (e.g., a skeleton) at the specified location has a particular piece of data. If not, an error is thrown.
Throws
This function can throw errors.
Example
test.assertEntityState(
villagerPos,
"minecraft:villager_v2",
(entity) => entity.getEffect(MinecraftEffectTypes.Regeneration).duration > 120
); // At least 6 seconds remaining in the villagers' effect
assertEntityTouching()
assertEntityTouching(
entityTypeIdentifier
,location
,isTouching
?):void
Parameters
Parameter | Type | Description |
---|---|---|
entityTypeIdentifier | string | Type of entity to test for (e.g., 'minecraft:skeleton'). If an entity namespace is not specified, 'minecraft:' is assumed. |
location | Vector3 | Location of the entity to test for. |
isTouching ? | boolean | If true, this function tests whether the entity is touching the specified location. If false, tests that an entity is not testing the specified location. |
Returns
void
Remarks
Depending on the value of isTouching, tests that an entity of a specified type is touching or connected to another entity. If the condition is not met, an exception is thrown.
Throws
This function can throw errors.
assertIsWaterlogged()
assertIsWaterlogged(
blockLocation
,isWaterlogged
?):void
Parameters
Parameter | Type | Description |
---|---|---|
blockLocation | Vector3 | Location of the block to test for. |
isWaterlogged ? | boolean | Whether to test that the block at position is expected to be waterlogged. |
Returns
void
Remarks
Depending on the value of isWaterlogged, tests that a block at a location contains water. If the condition is not met, an error is thrown. Pure water blocks are not considered to be waterlogged.
Throws
This function can throw errors.
assertItemEntityCountIs()
assertItemEntityCountIs(
itemType
,blockLocation
,searchDistance
,count
):void
Parameters
Parameter | Type | Description |
---|---|---|
itemType | string | ItemType | Type of item to look for. |
blockLocation | Vector3 | Location to search around for the specified set of items. |
searchDistance | number | Range, in blocks, to aggregate a count of items around. If 0, will only search the particular block at position. |
count | number | Number of items, at minimum, to look and test for. |
Returns
void
Remarks
Tests that items of a particular type and count are present within an area. If not, an error is thrown.
Throws
This function can throw errors.
Example
test.assertItemEntityCountIs(Items.feather, expectedFeatherLoc, 0, 1);
assertItemEntityPresent()
assertItemEntityPresent(
itemType
,blockLocation
,searchDistance
?,isPresent
?):void
Parameters
Parameter | Type | Description |
---|---|---|
itemType | string | ItemType | Type of item to test for. |
blockLocation | Vector3 | Location of the item entity to test for. |
searchDistance ? | number | Radius in blocks to look for the item entity. |
isPresent ? | boolean | If true, this function tests whether an item entity of the specified type is present. If false, tests that an item entity of the specified type is not present. |
Returns
void
Remarks
Depending on the value of isPresent, tests whether a particular item entity is present or not at a particular location. If the condition is not met, an exception is thrown.
Throws
This function can throw errors.
assertRedstonePower()
assertRedstonePower(
blockLocation
,power
):void
Parameters
Parameter | Type | Description |
---|---|---|
blockLocation | Vector3 | Location to test. |
power | number | Expected power level. |
Returns
void
Remarks
Tests that Redstone power at a particular location matches a particular value. If not, an exception is thrown.
Throws
This function can throw errors.
destroyBlock()
destroyBlock(
blockLocation
,dropResources
?):void
Parameters
Parameter | Type | Description |
---|---|---|
blockLocation | Vector3 | Location of the block to destroy. |
dropResources ? | boolean | Whether to add resources exposed with a particular drop. |
Returns
void
Remarks
Destroys a block at a particular location.
This function can't be called in read-only mode.
Throws
This function can throw errors.
fail()
fail(
errorMessage
):void
Parameters
Parameter | Type | Description |
---|---|---|
errorMessage | string | Error message summarizing the failure condition. |
Returns
void
Remarks
Marks the current test as a failure case.
Throws
This function can throw errors.
failIf()
failIf(
callback
):void
Parameters
Parameter | Type | Description |
---|---|---|
callback | () => void | Callback function that runs. If the function runs successfully, the test is marked as a failure. Typically, this function will have .assertXyz method calls within it. |
Returns
void
Remarks
Runs the given callback. If the callback does not throw an exception, the test is marked as a failure.
Throws
This function can throw errors.
getBlock()
getBlock(
blockLocation
):Block
Parameters
Parameter | Type | Description |
---|---|---|
blockLocation | Vector3 | Location of the block to retrieve. |
Returns
Block