Skip to content

Commit dbd8ef8

Browse files
committed
Run unit tests in Deno
* Node js usual require too much polyfilling here ( Ava, Jest, and so on) * adjust types accordingly
1 parent a26c531 commit dbd8ef8

11 files changed

+71
-70
lines changed

README.md

+8-12
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ It is an implementation of the game of snake using reference JavaScript, and `<c
44

55
There is experimental sound: unplug your headphones to rip beeps over your speakers.
66

7-
[Click here to play!](https://avindra.github.io/snake)
7+
[Click to play 🐍!](https://avindra.github.io/snake)
88

9-
## Development
9+
## Requirements
1010

1111
* Node.js 15 or higher
12+
* Deno (for unit tests)
1213

13-
`yarn install && yarn dev` to get a watch/recompile server, otherwise just use the build script.
14+
## Usage
1415

15-
Unit tests are co-located with the code.
16-
17-
Visit http://localhost:8080 in your browser
18-
19-
## Production build
20-
21-
```
22-
yarn build
23-
```
16+
* `yarn build` to bundle a build in `dist/`
17+
* `yarn install && yarn dev` to get a watch/recompile server
18+
* Unit tests are co-located with the code.
19+
* Run [`deno test src`](https://deno.land/manual/testing#assertions) to run unit tests.

package.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"sim": "bin/tsnode src/net/sim.ts",
1212
"train": "bin/tsnode src/net/train.js",
1313
"test-model": "node src/net/check_model.js",
14-
"tsc": "tsc",
15-
"test": "bin/tsnode node_modules/mocha/bin/_mocha src/**/*.spec.ts"
14+
"tsc": "tsc --noEmit",
15+
"test": "deno test src"
1616
},
1717
"keywords": [
1818
"snake",
@@ -25,11 +25,8 @@
2525
"@tensorflow/tfjs": "^2.6.0",
2626
"@tensorflow/tfjs-node": "^2.6.0",
2727
"@tensorflow/tfjs-node-gpu": "^2.6.0",
28-
"@types/mocha": "^8.0.0",
2928
"browser-sync": "^2.26.13",
3029
"esbuild": "^0.7.21",
31-
"expect": "^26.6.1",
32-
"mocha": "^8.2.0",
3330
"typescript": "^4.0.3"
3431
},
3532
"author": "Avindra Goolcharan",

src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ingame from './screens/ingame';
88

99
const $ = a => document.getElementById(a);
1010
const canvas = $('game') as HTMLCanvasElement;
11+
const ctx = canvas.getContext('2d');
1112

1213
const width = 20;
1314
const height = 20;

src/player.spec.ts

-10
This file was deleted.

src/player.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {Player} from './player.ts';
2+
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
3+
4+
const p = new Player();
5+
6+
Deno.test('player has a head', () => {
7+
assertEquals(isNaN(p.getHead().x), false);
8+
});

src/player.ts

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import Point from './point';
2-
import keys from './keys';
1+
// @ts-ignore
2+
import Point from './point.ts';
3+
// @ts-ignore
4+
import keys from './keys.ts';
35

4-
const inc = N => N + 1;
5-
const dec = N => N - 1;
6+
type NumIdent = (N: number) => number;
7+
8+
const inc: NumIdent = (N) => N + 1;
9+
const dec: NumIdent = (N) => N - 1;
610

711
class Player {
812
points: Point[];
@@ -34,21 +38,23 @@ class Player {
3438

3539
const head = this.getHead();
3640

37-
const newHead = new Point(head.x, head.y);
41+
const newHead = new Point();
42+
newHead.move(head.x, head.y);
3843

39-
let direction;
44+
let direction: NumIdent;
4045

4146
switch(this.headed) {
4247
case up: case left: direction = dec; break;
4348
case down: case right: direction = inc; break;
4449
}
4550

46-
let att;
51+
let att: string;
4752
switch (this.headed) {
4853
case up: case down: att = 'y'; break;
4954
case left: case right: att = 'x'; break;
5055
}
5156

57+
// @ts-ignore
5258
newHead[att] = direction(newHead[att]);
5359

5460
this.points.unshift(newHead);

src/point.spec.ts

-25
This file was deleted.

src/point.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Point from './point.ts'
2+
import { assertEquals, assert } from "https://deno.land/[email protected]/testing/asserts.ts";
3+
4+
const mkPoint = (x: number, y: number) => {
5+
const p = new Point();
6+
p.move(x, y);
7+
return p;
8+
}
9+
10+
const p = mkPoint(4, 8);
11+
12+
Deno.test('stores coordinates as x, y pairs', () => {
13+
assertEquals(p.x, 4);
14+
assertEquals(p.y, 8);
15+
});
16+
17+
Deno.test('can be moved', () => {
18+
p.move(15, 16);
19+
assertEquals(p.x, 15);
20+
assertEquals(p.y, 16);
21+
})
22+
23+
Deno.test('can determine the equality of two points', () => {
24+
const p2 = mkPoint(23, 42);
25+
const p3 = mkPoint(108, 1337);
26+
const p4 = mkPoint(23, 42);
27+
assert(p2.equals(p4));
28+
assert(!p4.equals(p3));
29+
})

src/point.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
export default class Point {
2-
x: number;
3-
y: number;
4-
5-
constructor(x = 0, y = 0) {
6-
this.move(x, y);
7-
}
2+
x: number = 0;
3+
y: number = 0;
84

95
move(x: number, y: number) {
106
this.x = x;
117
this.y = y;
128
}
139

14-
equals(p) {
10+
equals(p: Point) {
1511
return this.x === p.x && this.y === p.y;
1612
}
1713
}

src/world.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ function moveFood(w: IWorld) {
3838
let potentialTarget;
3939

4040
do {
41-
potentialTarget = new Point(
41+
potentialTarget = new Point();
42+
potentialTarget.move(
4243
rand(0, width - 1),
4344
rand(0, height - 1),
4445
);

tsconfig.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"compilerOptions": {
3-
"module": "ESNext"
3+
"module": "ESNext",
4+
"skipLibCheck": true,
5+
"lib": ["dom"]
46
},
57
"exclude": [
6-
"**/*.spec.ts"
8+
"**/*.test.ts"
79
]
8-
}
10+
}

0 commit comments

Comments
 (0)