Question: In this assignment, you will be building a GameCharacters class that retrieves and processes data from a separate JavaScript file using Node.js child processes. This

In this assignment, you will be building a GameCharacters class that retrieves and processes data from a separate JavaScript file using Node.js child processes. This class will have a getCharacters method that spawns a child process to run a script, captures the data it sends back, and passes this data to a callback function.
You will also need to handle any errors that might occur during the process. If theres an error while spawning the child process, it should be logged to the console. If the child process sends any data to stderr, this should also be logged to the console and passed to the callback function. You will also create a failing script to simulate and test error handling scenarios.
Additionally, you will create a data script (game-characters-data.js) that the GameCharacters class will use to retrieve the characters. This script should log a JSON stringified array of game characters to the console. Requirements:
1. Project Structure: Your project should have the following structure: fantasy-game-characters src game-characters.js game-characters-data.js failing-script.js test game-characters.spec.js package.json
2. TDD: You must follow TDD principles. Write your tests first, then write the code to make them pass.
3. Unit Tests: You must write at least three-unit tests
a. Test that data is being returned from the game-characters-data script.
b. Test that it handles an error when the game-characters-data script is not found. 202
c. Test that it handles an error when the game-characters-data script fails.
4. Character data (game-characters-data.js): Characters should have the following properties:
a. Class (Warrior, Mage, Rogue)
b. Gender (Male, Female, Other)
c. An additional property for something neat and fun about your character.
5. GameCharacters module (game-characters.js): Define the GameCharacters class with a constructor that accepts a script file name. The constructor should use the join function from the path module to create the path to the script file and store the path in an instance variable.
6. Package.json: Add a test script to the package.json file to run your tests with Jest.
Instructions:
1. Set up your project structure as described in the Requirements section.
2. Create the game-characters-data.js file. This script should log a JSON stringified array of game characters objects to the console.
3. Create the failing-script.js file. This script should log an error message to the stderr. This will be used to test the error handling in the GameCharacters class.
4. Write your first test in game-characters.spec.js. This test should test that data is being returned from the game-characters-data script.
5. Write code in the game-characters.js to make the test pass.
6. Run your test again to confirm that the test now passes.
7. Repeat steps 4-6 for the remaining two tests.
8. Make sure to handle errors properly in your code and tests.
Hints:
Use the child_process.spawn function to run your script in a child process.
Use the path.join function to create the path to your script file. 203
Remember to call done() in your Jest test to signal that the asynchronous test is complete.
Use the beforeEach function in Jest to create a new GameCharacters instance before each test.
Use the toEqual function in Jest to compare the data returned by the getCharacters method to the expected data.
Use the toBeNull function in Jest to check that an error is null when theres no error and that data is null when theres an error.
Remember to follow TDD principles: write your test first, then write code to make the test pass.
Refer to the examples in this chapter to complete this assignment. ```
JS game-characters.spec.js
test > JS game-characters.spec.js >...
V/ game-characters.spec.js
const { GameCharacters }= require("../src/game-characters");
describe("GameCharacters",()=>{
let gameCharacters;
beforeEach(()=>{
gameCharacters = new GameCharacters();
});
test("should return game characters data", (done)=>{
//\ Implement this test
});
test("should handle an error when the game characters data script is not found", (done)=>{
//\ Implement this test
});
test("should handle an error when the game characters data script fails", (done)=>{
// rODO: Implement this test
});
});
``````
Js game-characters.js
src > JS game-characters.js >...
// game-characters.js
const { spawn }= r..rquire("child_process");
class GameCharacters {
constructor(){
// rODO: set the script file path
}
getCharacters(callback){
// IODO: Implement this method
}
}
module.exports ={ GameCharacters };
``````
{} package.json >...
1{
"name": "fantasy-game-characters",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"test": "test"
},
\ Debug
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/jest": "^29.5.11",
"jest":
In this assignment, you will be building a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!