Skip to content

Commit 44b710c

Browse files
authored
Merge pull request #1296 from ml5js/joeyklee.637-refactor-tests-to-jest
refactor(#637): Refactor BodyPix and CharRNN tests to jest
2 parents 388cc9a + fc3f82d commit 44b710c

File tree

6 files changed

+287
-73
lines changed

6 files changed

+287
-73
lines changed

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ module.exports = {
1313
env: {
1414
browser: true,
1515
jasmine: true,
16+
"jest/globals": true,
1617
},
18+
plugins: ["jest"],
1719
overrides: [
1820
{
1921
files: ["examples/**"],

package-lock.json

Lines changed: 171 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"eslint-loader": "2.0.0",
6969
"eslint-nibble": "^5.1.0",
7070
"eslint-plugin-import": "2.9.0",
71+
"eslint-plugin-jest": "^26.0.0",
7172
"extract-text-webpack-plugin": "4.0.0-beta.0",
7273
"ghooks": "2.0.2",
7374
"html-webpack-plugin": "^3.0.7",
@@ -118,4 +119,4 @@
118119
"face-api.js": "~0.22.2",
119120
"onchange": "^6.1.0"
120121
}
121-
}
122+
}

src/BodyPix/__tests__/BodyPix.test.js

Lines changed: 66 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
/* eslint-disable no-use-before-define */
12
// Copyright (c) 2018 ml5
23
//
34
// This software is released under the MIT License.
45
// https://opensource.org/licenses/MIT
5-
import bodyPix from "../index";
6-
import * as tf from "@tensorflow/tfjs";
76
import * as tfBodyPix from "@tensorflow-models/body-pix";
7+
import bodyPix from "../index";
88

99
jest.mock("@tensorflow-models/body-pix");
1010

@@ -14,6 +14,70 @@ const BODYPIX_DEFAULTS = {
1414
segmentationThreshold: 0.5,
1515
};
1616

17+
const mockBodyPix = {
18+
mobileNet: {
19+
predict: jest.fn(),
20+
convToOutput: jest.fn(),
21+
dispose: jest.fn(),
22+
},
23+
predictForSegmentation: jest.fn(),
24+
predictForPartMap: jest.fn(),
25+
estimatePersonSegmentationActivation: jest.fn(),
26+
estimatePersonSegmentation: jest.fn(),
27+
estimatePartSegmentationActivation: jest.fn(),
28+
estimatePartSegmentation: jest.fn(),
29+
dispose: jest.fn(),
30+
};
31+
32+
describe("bodyPix", () => {
33+
let bp;
34+
35+
beforeAll(async () => {
36+
tfBodyPix.load = jest.fn().mockResolvedValue(mockBodyPix);
37+
bp = await bodyPix();
38+
});
39+
40+
afterAll(async () => {
41+
jest.clearAllMocks();
42+
});
43+
44+
it("Should create bodyPix with all the defaults", async () => {
45+
expect(bp.config.multiplier).toBe(BODYPIX_DEFAULTS.multiplier);
46+
expect(bp.config.outputStride).toBe(BODYPIX_DEFAULTS.outputStride);
47+
expect(bp.config.segmentationThreshold).toBe(BODYPIX_DEFAULTS.segmentationThreshold);
48+
});
49+
50+
it("segment calls segmentInternal and returns", async () => {
51+
bp.segmentInternal = jest.fn().mockResolvedValue({
52+
segmentation: {
53+
data: [],
54+
width: 200,
55+
height: 50,
56+
},
57+
});
58+
const img = await getImageData();
59+
const results = await bp.segment(img);
60+
expect(bp.segmentInternal).toHaveBeenCalledTimes(1);
61+
expect(results.segmentation.width).toBe(200);
62+
expect(results.segmentation.height).toBe(50);
63+
});
64+
65+
it("segmentWithParts calls segmentWithPartsInternal and returns", async () => {
66+
bp.segmentWithPartsInternal = jest.fn().mockResolvedValue({
67+
segmentation: {
68+
data: [],
69+
width: 200,
70+
height: 50,
71+
},
72+
});
73+
const img = await getImageData();
74+
const results = await bp.segmentWithParts(img);
75+
expect(bp.segmentWithPartsInternal).toHaveBeenCalledTimes(1);
76+
expect(results.segmentation.width).toBe(200);
77+
expect(results.segmentation.height).toBe(50);
78+
});
79+
});
80+
1781
async function getImage() {
1882
const img = new Image();
1983
img.crossOrigin = true;
@@ -49,43 +113,3 @@ async function getImageData() {
49113
const img = new ImageData(arr, 200);
50114
return img;
51115
}
52-
53-
describe("bodyPix", () => {
54-
let bp;
55-
56-
beforeAll(async () => {
57-
tfBodyPix.load = jest.fn().mockResolvedValue({});
58-
bp = await bodyPix();
59-
});
60-
61-
it("Should create bodyPix with all the defaults", async () => {
62-
console.log(bp);
63-
expect(bp.config.multiplier).toBe(BODYPIX_DEFAULTS.multiplier);
64-
expect(bp.config.outputStride).toBe(BODYPIX_DEFAULTS.outputStride);
65-
expect(bp.config.segmentationThreshold).toBe(BODYPIX_DEFAULTS.segmentationThreshold);
66-
});
67-
68-
// it("segment takes ImageData", async () => {
69-
// const img = await getImageData();
70-
// const results = await bp.segment(img);
71-
// // 200 * 50 == 10,000 * 4 == 40,000 the size of the array
72-
// expect(results.segmentation.width).toBe(200);
73-
// expect(results.segmentation.height).toBe(50);
74-
// });
75-
76-
// describe("segmentation", () => {
77-
// it("Should segment an image of a Harriet Tubman with a width and height of 128", async () => {
78-
// const img = await getImage();
79-
// await bp.segment(img).then(results => {
80-
// expect(results.segmentation.width).toBe(128);
81-
// expect(results.segmentation.height).toBe(128);
82-
83-
// expect(results.segmentation.width).toBe(128);
84-
// expect(results.segmentation.height).toBe(128);
85-
86-
// expect(results.segmentation.width).toBe(128);
87-
// expect(results.segmentation.height).toBe(128);
88-
// });
89-
// });
90-
// });
91-
});

0 commit comments

Comments
 (0)