Skip to content

Commit

Permalink
Merge pull request #1296 from ml5js/joeyklee.637-refactor-tests-to-jest
Browse files Browse the repository at this point in the history
refactor(#637): Refactor BodyPix and CharRNN tests to jest
  • Loading branch information
joeyklee authored Apr 6, 2022
2 parents 388cc9a + fc3f82d commit 44b710c
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 73 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ module.exports = {
env: {
browser: true,
jasmine: true,
"jest/globals": true,
},
plugins: ["jest"],
overrides: [
{
files: ["examples/**"],
Expand Down
172 changes: 171 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"eslint-loader": "2.0.0",
"eslint-nibble": "^5.1.0",
"eslint-plugin-import": "2.9.0",
"eslint-plugin-jest": "^26.0.0",
"extract-text-webpack-plugin": "4.0.0-beta.0",
"ghooks": "2.0.2",
"html-webpack-plugin": "^3.0.7",
Expand Down Expand Up @@ -118,4 +119,4 @@
"face-api.js": "~0.22.2",
"onchange": "^6.1.0"
}
}
}
108 changes: 66 additions & 42 deletions src/BodyPix/__tests__/BodyPix.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-use-before-define */
// Copyright (c) 2018 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
import bodyPix from "../index";
import * as tf from "@tensorflow/tfjs";
import * as tfBodyPix from "@tensorflow-models/body-pix";
import bodyPix from "../index";

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

Expand All @@ -14,6 +14,70 @@ const BODYPIX_DEFAULTS = {
segmentationThreshold: 0.5,
};

const mockBodyPix = {
mobileNet: {
predict: jest.fn(),
convToOutput: jest.fn(),
dispose: jest.fn(),
},
predictForSegmentation: jest.fn(),
predictForPartMap: jest.fn(),
estimatePersonSegmentationActivation: jest.fn(),
estimatePersonSegmentation: jest.fn(),
estimatePartSegmentationActivation: jest.fn(),
estimatePartSegmentation: jest.fn(),
dispose: jest.fn(),
};

describe("bodyPix", () => {
let bp;

beforeAll(async () => {
tfBodyPix.load = jest.fn().mockResolvedValue(mockBodyPix);
bp = await bodyPix();
});

afterAll(async () => {
jest.clearAllMocks();
});

it("Should create bodyPix with all the defaults", async () => {
expect(bp.config.multiplier).toBe(BODYPIX_DEFAULTS.multiplier);
expect(bp.config.outputStride).toBe(BODYPIX_DEFAULTS.outputStride);
expect(bp.config.segmentationThreshold).toBe(BODYPIX_DEFAULTS.segmentationThreshold);
});

it("segment calls segmentInternal and returns", async () => {
bp.segmentInternal = jest.fn().mockResolvedValue({
segmentation: {
data: [],
width: 200,
height: 50,
},
});
const img = await getImageData();
const results = await bp.segment(img);
expect(bp.segmentInternal).toHaveBeenCalledTimes(1);
expect(results.segmentation.width).toBe(200);
expect(results.segmentation.height).toBe(50);
});

it("segmentWithParts calls segmentWithPartsInternal and returns", async () => {
bp.segmentWithPartsInternal = jest.fn().mockResolvedValue({
segmentation: {
data: [],
width: 200,
height: 50,
},
});
const img = await getImageData();
const results = await bp.segmentWithParts(img);
expect(bp.segmentWithPartsInternal).toHaveBeenCalledTimes(1);
expect(results.segmentation.width).toBe(200);
expect(results.segmentation.height).toBe(50);
});
});

async function getImage() {
const img = new Image();
img.crossOrigin = true;
Expand Down Expand Up @@ -49,43 +113,3 @@ async function getImageData() {
const img = new ImageData(arr, 200);
return img;
}

describe("bodyPix", () => {
let bp;

beforeAll(async () => {
tfBodyPix.load = jest.fn().mockResolvedValue({});
bp = await bodyPix();
});

it("Should create bodyPix with all the defaults", async () => {
console.log(bp);
expect(bp.config.multiplier).toBe(BODYPIX_DEFAULTS.multiplier);
expect(bp.config.outputStride).toBe(BODYPIX_DEFAULTS.outputStride);
expect(bp.config.segmentationThreshold).toBe(BODYPIX_DEFAULTS.segmentationThreshold);
});

// it("segment takes ImageData", async () => {
// const img = await getImageData();
// const results = await bp.segment(img);
// // 200 * 50 == 10,000 * 4 == 40,000 the size of the array
// expect(results.segmentation.width).toBe(200);
// expect(results.segmentation.height).toBe(50);
// });

// describe("segmentation", () => {
// it("Should segment an image of a Harriet Tubman with a width and height of 128", async () => {
// const img = await getImage();
// await bp.segment(img).then(results => {
// expect(results.segmentation.width).toBe(128);
// expect(results.segmentation.height).toBe(128);

// expect(results.segmentation.width).toBe(128);
// expect(results.segmentation.height).toBe(128);

// expect(results.segmentation.width).toBe(128);
// expect(results.segmentation.height).toBe(128);
// });
// });
// });
});
Loading

0 comments on commit 44b710c

Please sign in to comment.