# Unit Tests

# Prerequisites

  1. You must know Jest
  2. The tests apply to the server side

# Create a unit test

  1. Create a file in the tests folder. This folder must be outside the src folder.
  2. the file must end with .spec.ts
import { RpgWorld, RpgPlayer } from '@rpgjs/server'
import { testing } 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()
})

In the beforeEach() function, you must:

  1. Wrap your server class with the testing() function. It will allow you to emulate a client.
  2. Create a client
  3. Using the RpgWorld class, retrieve the player according to his identifier

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