Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate tests to Jest #5431

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"Config": false, "Monitor": false, "toId": false, "Dex": false, "LoginServer": false,
"Users": false, "Punishments": false, "Rooms": false, "Verifier": false, "Chat": false,
"Tournaments": false, "Dnsbl": false, "Sockets": false, "TeamValidator": false,
"TeamValidatorAsync": false, "Ladders": false
"TeamValidatorAsync": false, "Ladders": false, "beforeAll": false, "afterAll": false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set env: {jest: true} in test/.eslintrc instead

},
"extends": "eslint:recommended",
"rules": {
Expand Down
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
testEnvironment: 'node',
setupFilesAfterEnv: ['<rootDir>/test/main.js'],
testMatch: ['<rootDir>/test/simulator/**/*.js', '<rootDir>/test/application/*.js', '<rootDir>/test/chat-plugins/*.js', '<rootDir>/test/dev-tools/*.js'],
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "The server for the Pokémon Showdown battle simulator",
"version": "0.11.2",
"dependencies": {
"jest": "^24.7.1",
"probe-image-size": "^4.0.0",
"sockjs": "0.3.19",
"sucrase": "^3.10.1"
Expand All @@ -21,7 +22,7 @@
"scripts": {
"start": "node pokemon-showdown start",
"build": "node build",
"test": "eslint --cache . && tslint --project . && node build && mocha && tsc",
"test": "eslint --cache . && tslint --project . && node build && jest && tsc",
"tsc": "tsc",
"lint": "eslint --cache ."
},
Expand Down Expand Up @@ -63,7 +64,6 @@
"@types/sockjs": "^0.3.31",
"eslint": "^5.16.0",
"husky": "^1.1.2",
"mocha": "^6.1.2",
"tslint": "^5.15.0",
"typescript": "^3.4.2"
}
Expand Down
91 changes: 47 additions & 44 deletions test/application/ladders-matchmaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ const assert = require('assert');
global.Ladders = require('../../server/ladders');
const {Connection, User} = require('../../dev-tools/users-utils');

let p1, p2;
let s1, s2;

describe('Matchmaker', function () {
const FORMATID = 'gen7ou';
const addSearch = (player, rating = 1000, formatid = FORMATID) => {
Expand All @@ -19,64 +22,64 @@ describe('Matchmaker', function () {
return null;
};

before(function () {
beforeAll(function () {
clearInterval(Ladders.periodicMatchInterval);
Ladders.periodicMatchInterval = null;
});

beforeEach(function () {
this.p1 = new User(new Connection('127.0.0.1'));
this.p1.forceRename('Morfent', true);
this.p1.connected = true;
this.p1.team = 'Gengar||||lick||252,252,4,,,|||||';
Users.users.set(this.p1.userid, this.p1);

this.p2 = new User(new Connection('0.0.0.0'));
this.p2.forceRename('Mrofnet', true);
this.p2.connected = true;
this.p2.team = 'Gengar||||lick||252,252,4,,,|||||';
Users.users.set(this.p2.userid, this.p2);
p1 = new User(new Connection('127.0.0.1'));
p1.forceRename('Morfent', true);
p1.connected = true;
p1.team = 'Gengar||||lick||252,252,4,,,|||||';
Users.users.set(p1.userid, p1);

p2 = new User(new Connection('0.0.0.0'));
p2.forceRename('Mrofnet', true);
p2.connected = true;
p2.team = 'Gengar||||lick||252,252,4,,,|||||';
Users.users.set(p2.userid, p2);
});

afterEach(function () {
this.p1 = destroyPlayer(this.p1);
this.p2 = destroyPlayer(this.p2);
p1 = destroyPlayer(p1);
p2 = destroyPlayer(p2);
});

it('should add a search', function () {
let s1 = addSearch(this.p1);
let s1 = addSearch(p1);
assert.ok(Ladders.searches.has(FORMATID));

let formatSearches = Ladders.searches.get(FORMATID);
assert.ok(formatSearches instanceof Map);
assert.strictEqual(formatSearches.size, 1);
assert.strictEqual(s1.userid, this.p1.userid);
assert.strictEqual(s1.team, this.p1.team);
assert.strictEqual(s1.userid, p1.userid);
assert.strictEqual(s1.team, p1.team);
assert.strictEqual(s1.rating, 1000);
});

it('should matchmake users when appropriate', function () {
addSearch(this.p1);
addSearch(this.p2);
addSearch(p1);
addSearch(p2);
assert.strictEqual(Ladders.searches.get(FORMATID).size, 0);
});

it('should matchmake users within a reasonable rating range', function () {
addSearch(this.p1);
addSearch(this.p2, 2000);
addSearch(p1);
addSearch(p2, 2000);
assert.strictEqual(Ladders.searches.get(FORMATID).size, 2);
});

it('should cancel searches', function () {
addSearch(this.p1);
Ladders(FORMATID).cancelSearch(this.p1);
Ladders.cancelSearches(this.p2);
addSearch(p1);
Ladders(FORMATID).cancelSearch(p1);
Ladders.cancelSearches(p2);
assert.strictEqual(Ladders.searches.get(FORMATID).size, 0);
});

it('should periodically matchmake users when appropriate', function () {
addSearch(this.p1);
let s2 = addSearch(this.p2, 2000);
addSearch(p1);
let s2 = addSearch(p2, 2000);
assert.strictEqual(Ladders.searches.get(FORMATID).size, 2);

s2.rating = 1000;
Expand All @@ -85,59 +88,59 @@ describe('Matchmaker', function () {
});

it('should create a new battle room after matchmaking', function () {
assert.strictEqual(this.p1.games.size, 0);
addSearch(this.p1);
addSearch(this.p2);
assert.strictEqual(this.p1.games.size, 1);
for (const roomid of this.p1.games) {
assert.strictEqual(p1.games.size, 0);
addSearch(p1);
addSearch(p2);
assert.strictEqual(p1.games.size, 1);
for (const roomid of p1.games) {
assert.ok(Rooms(roomid).battle);
}
});

it('should cancel search on disconnect', function () {
addSearch(this.p1);
this.p1.onDisconnect(this.p1.connections[0]);
addSearch(p1);
p1.onDisconnect(p1.connections[0]);
assert.strictEqual(Ladders.searches.get(FORMATID).size, 0);
});

it('should cancel search on merge', function () {
addSearch(this.p1);
this.p2.merge(this.p1);
addSearch(p1);
p2.merge(p1);
assert.strictEqual(Ladders.searches.get(FORMATID).size, 0);
});

describe('#startBattle', function () {
beforeEach(function () {
this.s1 = addSearch(this.p1);
this.s2 = addSearch(this.p2);
s1 = addSearch(p1);
s2 = addSearch(p2);
});

afterEach(function () {
this.s1 = null;
this.s2 = null;
s1 = null;
s2 = null;
});

it('should prevent battles from starting if both players are identical', function () {
Object.assign(this.s2, this.s1);
Object.assign(s2, s1);
let room;
try {
room = Rooms.createBattle(FORMATID, {p1: this.p1, p2: this.p1, p1team: this.s1.team, p2team: this.s2.team, rated: 1000});
room = Rooms.createBattle(FORMATID, {p1: p1, p2: p1, p1team: s1.team, p2team: s2.team, rated: 1000});
} catch (e) {}
assert.strictEqual(room, undefined);
});

before(function () {
beforeAll(function () {
this.lockdown = Rooms.global.lockdown;
Rooms.global.lockdown = true;
});

after(function () {
afterAll(function () {
Rooms.global.lockdown = this.lockdown;
this.lockdown = null;
});

it('should prevent battles from starting if the server is in lockdown', function () {
let room = Rooms.createBattle(FORMATID, {p1: this.p1, p2: this.p2, p1team: this.s1.team, p2team: this.s2.team, rated: 1000});
let room = Rooms.createBattle(FORMATID, {p1: p1, p2: p2, p1team: s1.team, p2team: s2.team, rated: 1000});
assert.strictEqual(room, undefined);
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/application/sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe.skip('Sockets', function () {
})
);

before(function () {
beforeAll(function () {
cluster.settings.silent = true;
cluster.removeAllListeners('disconnect');
});
Expand Down
26 changes: 14 additions & 12 deletions test/application/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ let userUtils = require('./../../dev-tools/users-utils');
let Connection = userUtils.Connection;
let User = userUtils.User;

let connection;

describe('Users features', function () {
describe('Users', function () {
describe('get', function () {
Expand All @@ -30,17 +32,17 @@ describe('Users features', function () {
describe('Connection', function () {
describe('#onDisconnect', function () {
beforeEach(function () {
this.connection = new Connection('127.0.0.1');
connection = new Connection('127.0.0.1');
});

it('should remove the connection from Users.connections', function () {
let connectionid = this.connection.id;
this.connection.destroy();
let connectionid = connection.id;
connection.destroy();
assert.strictEqual(Users.connections.has(connectionid), false);
});

it('should destroy any user on the connection as well', function () {
let user = new User(this.connection);
let user = new User(connection);
let {userid} = user;
user.disconnectAll();
user.destroy();
Expand All @@ -50,25 +52,25 @@ describe('Users features', function () {

describe('#joinRoom', function () {
beforeEach(function () {
this.connection = new Connection('127.0.0.1');
connection = new Connection('127.0.0.1');
});

afterEach(function () {
this.connection.destroy();
connection.destroy();
});

it('should join a room if not already present', function () {
this.connection.joinRoom(Rooms.lobby);
assert.ok(this.connection.inRooms.has('lobby'));
connection.joinRoom(Rooms.lobby);
assert.ok(connection.inRooms.has('lobby'));
});
});

describe('#leaveRoom', function () {
it('should leave a room that is present', function () {
this.connection = new Connection('127.0.0.1');
this.connection.joinRoom(Rooms.lobby);
this.connection.leaveRoom(Rooms.lobby);
assert.ok(!this.connection.inRooms.has('lobby'));
connection = new Connection('127.0.0.1');
connection.joinRoom(Rooms.lobby);
connection.leaveRoom(Rooms.lobby);
assert.ok(!connection.inRooms.has('lobby'));
});
});
});
Expand Down
Loading