Oh I see, thanks for the clarification! Here’s what you could try out:
- In your package.json (the one that is at the same level as the Asset folder), for reference it should start with :
{
"name": "your-project",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "jest"
},
"keywords": [],
"author": "",
Make sure that the script element is:
"scripts": {"test": "jest"}
and also that the “jest” element is:
"jest": {
"moduleFileExtensions": [
"es10",
"js"
],
"testMatch": [
"**/*.test.es10",
"**/*.test.js"
]
}
- Create a new file, if you script is called inventory.js => the new file could be inventory.test.js.
Here’s the proposed content
const { InventoryApi } = require("@unity-services/economy-2.4");
const { DataApi } = require("@unity-services/cloud-save-1.2");
jest.mock("@unity-services/economy-2.4", () => ({ InventoryApi: jest.fn() }));
jest.mock("@unity-services/cloud-save-1.2", () => ({ DataApi: jest.fn() }));
const getInventory = require("./inventory.js");
const mockTavern = {
heroesInTavern: ["bd17d622-54e4-4378-85e3-4ff32be1b0c7"],
level: 1,
slotsCount: 2,
};
const heroData = {
Name: "mockedName",
attributes: [
{ Name: "Strength", Value: 1 },
{ Name: "Perception", Value: 1 },
{ Name: "Endurance", Value: 1 },
{ Name: "Charisma", Value: 1 },
{ Name: "Intelligence", Value: 1 },
{ Name: "Agility", Value: 1 },
{ Name: "Luck", Value: 1 },
],
};
const addInventoryRequest = {
inventoryItemId: "HERO",
instanceData: heroData,
};
const mockAddInventoryResponse = {
playersInventoryItemId: "mock-guid",
inventoryItemId: "HERO",
instanceData: heroData,
};
describe("inventory tests", () => {
let mockDataApi, mockInventoryApi;
beforeEach(() => {
mockDataApi = {
getItems: jest.fn(() => ({
data: { results: [{ value: mockTavern }] },
})),
setItem: jest.fn(() => ({})),
};
mockInventoryApi = {
addInventoryItem: jest.fn(() => ({
data: mockAddInventoryResponse,
})),
};
DataApi.mockReturnValue(mockDataApi);
InventoryApi.mockReturnValue(mockInventoryApi);
});
it("testGetInventory", async () => {
await getInventory({
params: {
name: "mockHeroName",
slotId: 1,
},
context: {
projectId: "",
playerId: "",
accesssToken: "",
},
logger: {
info: console.log,
},
});
expect(mockDataApi.getItems).toHaveBeenCalled();
expect(mockDataApi.setItem).toHaveBeenCalled();
expect(mockInventoryApi.addInventoryItem).toHaveBeenCalled();
});
});
Note that refers to what your script is called, so adjust in consequence the line:
const getInventory = require("./inventory.js");
And finally for VS Code, you can look up the “Jest Runner” extension, it will allow you to trigger debug on the “describe” test line. You can also run the test from the command-line: npm run test.
Let me know if you have any issues with this, hope it help!