Я работаю с кодом Visual Studio, а также с Webpack, и недавно я познакомился с разработкой через тестирование (TDD) с помощью Jest. В настоящее время я работаю над проектом Odin.
Когда я запускаю npm test в консоли, появляется это сообщение об ошибке.

Но когда я
Код: Выделить всё
console.log(this.cells[y][x])
Поэтому я предполагаю, что что-то не понял в рабочем процессе тестирования.
Вот код gameboard.js, а также gameboard.test.js.
Код: Выделить всё
const Ship = require("./ship");
class Gameboard {
constructor(rows, cols) {
this.rows = rows;
this.cols = cols;
this.cells = this.initializeCells();
this.ships = [];
this.hits = [];
this.missedAttacks = [];
}
initializeCells() {
let my2DArray = [];
for (let i = 0; i < this.rows; i++) {
my2DArray[i] = [];
for (let j = 0; j < this.cols; j++) {
my2DArray[i][j] = null; // write the value "null" in all the cells
}
}
return my2DArray;
}
placeShip(ship, startCol, startRow, orientation) {
if (!this.isPlacementValid(ship, startCol, startRow, orientation)) {
console.error("Invalid ship placement!");
return false;
}
for (let i = 0; i < ship.length; i++) {
let x = startCol;
let y = startRow;
if (orientation === "horizontal") {
x += i;
} else if (orientation === "vertical") {
y += i;
}
// Mark the cell as occupied by this specific ship object
this.cells[y][x] = ship;
// Optionally, store the coordinates within the ship object itself
ship.coordinates.push({ x, y });
}
this.ships.push(ship);
return true;
}
isPlacementValid(ship, startCol, startRow, orientation) {
for (let i = 0; i < ship.length; i++) {
let x = startCol;
let y = startRow;
if (orientation === "horizontal") {
x += i;
} else if (orientation === "vertical") {
y += i;
}
if (x >= this.size || y >= this.size || x < 0 || y < 0) {
return false;
}
// Check if the cell is already occupied by another ship
// console.log(this.cells[y][x])
if (this.cells[y][x] != null) {
return false;
}
}
return true;
}
receiveAttack(col, row) {
const target = this.cells[row][col];
if (target != null && target != "miss" && target != "hit") {
target.hit();
this.hits.push({ col, row });
this.cells[row][col] = "hit";
return true; // It was a hit
} else if (target === null) {
this.missedAttacks.push({ col, row });
this.cells[row][col] = "miss";
return false; // It was a miss
}
console.log("Already attacked this spot");
return false;
}
allShipsSunk() {
return this.ships.every((ship) => ship.isSunk());
}
}
module.exports = Gameboard;
Код: Выделить всё
const Ship = require("./ship");
const Gameboard = require("./gameboard");
describe("Gameboard Class", () => {
let testShip;
let gameBoard;
// beforeEach runs before each individual test (it/test block)
beforeEach(() => {
testShip = new Ship(3);
gameBoard = new Gameboard();
});
// --- Test Placement ---
test("places a ship correctly on the board horizontally", () => {
gameBoard.placeShip(testShip, 0, 0, "horizontal");
expect(gameBoard.cells[0][0]).toBe(testShip);
expect(gameBoard.cells[0][1]).toBe(testShip);
expect(gameBoard.cells[0][2]).toBe(testShip);
});
});
Подробнее здесь: https://stackoverflow.com/questions/798 ... javascript
Мобильная версия