Question: In this assignment you will create a character creation system for a fantasy video game. You will use TDD principles with Jest and Node.js s
In this assignment you will create a character creation system for a fantasy video game. You will use TDD principles with Jest and Node.jss builtin fs module. You can choose to use either the callback methods or promise methods asyncawait Requirements:
Project Structure: Your project should have the following structure: fantasycharactercreation src charactercreation.js test charactercreation.spec.js package.json
TDD: You must follow TDD principles. Write your tests first, then write the code to make the tests pass.
Unit Tests: You must write at least threeunit tests:
a Test that data can be written to a file.
bb Test that data can be read from a file
cc Test that it handles errors when reading from the file
File Operations: You must use diranme, readFile, and writeFile. And, you must const variables for each file name.
Character Creation: Your program should allow the creation of a character with 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.
File Data: Your program should write character data to a file and read character data from the file.
Package.json: Add a test script to the package.json file to run your tests with Jest Instructions:
Set up your project structure as describe in the Requirements section.
Write your first test in charactercreation.spec.js This test should check that data can be written to a file.
Run your test script to confirm that the test fails.
Write code in the charactercreation.js to make the test pass.
Run your test script again to confirm that the test now passes.
Repeat steps for the remaining two tests.
Make sure to handle errors property in your code and tests.
Hints:
Use the fs modules readFIle and writeFile functions to read from and write to files.
Use dirname to get the directory name of the current module.
Use const variables for each file name.
Remember to follow TDD principles: write your test first, then write code to make the test pass.
"use strict";
This file allows you to choose between using callbacks or promises asyncawait for handling asynchronous operations.
square
If you want to use callbacks:
Uncomment the fs require statement under the "For callbacks" comment.
If you want to use promises asyncawait:
Uncomment the fs require statement under the "For promises" comment.
For callbacks:
const fs requirefs;
For promises:
const fs requirefspromises;
describeCharacter Creation Module",
let createCharacter;
let getCharacters;
beforeEach
jest.resetModules;
Set up your mocks here
createCharacter, getCharacters requiresrccharactercreation';
;
TODO: Write your tests here. You should have at least three tests:
Test that createCharacter writes a new character to the file
Test that getcharacters reads characters from the file
Test that createcharacter handles errors when writing to the file
;
"use strict";
This file allows you to choose between using callbacks or promises asyncawait for handling asynchronous operations.
If you want to use callbacks:
Uncomment the fs require statement under the "For callbacks" comment.
Uncomment the 'createcharacter' and 'getcharacters' functions under the "For callbacks" comment.
Uncomment the 'module.exports' line under the "For callbacks" comment.
If you want to use promises asyncawait:
Uncomment the fs require statement under the "For promises" comment.
Uncomment the 'createcharacter' and 'getcharacters' functions under the "For promises" comment.
Uncomment the 'module.exports' line under the "For promises" comment.
For callbacks:
const fs requirefs;
function createCharactercharacter callback
TODO Implement this function
function getcharacterscallback
TODO Implement this function
For promises:
const fs requirefspromises;
async function createcharactercharacter
TODOH Implement this function
JS charactercreation.js
src JS charactercreation.js
async function getCharacters
TODO Implement this function
Uncomment the appropriate exports depending on whether you're using callbacks or promises:
module.exports createCharacter, getCharacters ; For callbacks
module.exports createCharacter, getCharacters ; For promises
package.json
package.json
"name": "fantasycharactercreation",
"version":
"description":
"main": "index.js
"directories":
"test": "test"
triangleright Debug
"scripts":
"test": "jest"
"keywords":
"author":
"license": "ISC",
"devDependencies":
@typesjest:
"jest
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
