# Unit Tests
# Prerequisites
- You must know Jest (opens new window)
- The tests apply to the server side
# Create a unit test
- Create a file in the
tests
folder. This folder must be outside thesrc
folder. - the file must end with
.spec.ts
import { RpgWorld, RpgPlayer } from '@rpgjs/server'
import { testing, clear } from '@rpgjs/testing'
import modules from '../src/modules'
let player: RpgPlayer
beforeEach(async () => {
const fixture = testing(modules, {
basePath: __dirname + '/../'
})
const client = await fixture.createClient()
player = RpgWorld.getPlayer(client.playerId)
})
test('test player', () => {
expect(player).toBeDefined()
})
afterEach(() => {
clear()
})
In the beforeEach()
function, you must:
- Wrap your server class with the
testing()
function. It will allow you to emulate a client. - Create a client
- Using the RpgWorld class, retrieve the player according to his identifier
- Add the
clear()
function inafterEach
to empty the cards and players in memory, and start from 0 for the next test
You can make tests !
# Example of a test
test('check that after the connection, the player is on the map named town', () => {
const map = player.getCurrentMap()
expect(map.id).toBe('town')
})
# Launch unit tests
Add the code in jest.config.js
:
const jestConfig = require('@rpgjs/compiler/jest')
module.exports = jestConfig
If you want to extend the configuration of Jest:
const jestConfig = require('@rpgjs/compiler/jest') module.exports = { ...jestConfig, verbose: true }
Run the following command line :
NODE_ENV=test npx jest