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 most tests to Jest #1345

Merged
merged 3 commits into from
May 8, 2022
Merged

Conversation

lindapaiste
Copy link
Contributor

@lindapaiste lindapaiste commented Apr 11, 2022

Partially fixes #1330

Settings:

  • Install jest and supporting packages (babel-jest, etc.)
  • Install node polyfill for canvas.
  • Add scripts to package.json to execute all .test.js files using Jest.
  • Set env: { jest: true } in .eslint.js to fix es-lint errors re: undefined variable jest.
  • Add testEnvironmentOptions: { resources: "usable" } to Jest config so that images will call the onload function.
    • This doesn't work for videos. Tests which load video still hang forever and are not included in this PR.
  • Create a setupTests.js file in order to load the tfjs-node backend.
    • I tried to polyfill ImageData here, but it doesn't seem to work.

Utilities:

  • Use utility functions asyncLoadImage and randomImageData instead of repeating code in multiple test files.
  • Export a polyfillImageData function which can be used in a beforeAll function.

Test Modifications:

  • Import the functions to test from local files instead of a global ml5 variable.
    • Rewrite test imports from const { charRNN } = ml5; to import charRNN from './index';.
    • import tf directly from @tensorflow/tfjs in NeuralNetwork instead of using ml5.tf alias.
  • Replace all instances of jasmine.DEFAULT_TIMEOUT_INTERVAL = n; with jest.setTimeout(n);.
  • Replace spyOn with jest.spyOn (which internally calls the jasmine spyOn function).
  • Modify chained function calls, eg. .and.callFake to .mockImplementation, .and.returnValue to .mockReturnValue, etc.

Migrated Tests:

  • BodyPix
  • CVAE
  • CartoonGAN
  • CharRNN
  • DBSCAN
  • DCGAN
  • FaceApi
  • Facemesh
  • FeatureExtractor
  • Handpose
  • ImageClassifier
  • KMeans
  • KNNClassifier
  • NeuralNetwork
  • ObjectDetector
  • PitchDetection
  • Pix2pix
  • PoseNet
  • Sentiment
  • SketchRNN
  • SoundClassifier
  • StyleTransfer
  • UNET
  • UniversalSentenceEncoder
  • Word2vec

image
The 57 skipped tests are all xdescribe placeholders in the NeuralNetwork file.

Copy link
Contributor

@joeyklee joeyklee left a comment

Choose a reason for hiding this comment

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

Wow! Thanks so much for this. I'd be totally happy to merge and release this so that we can add tests incrementally in case you think that would be helpful for you. Either way, this is great to see! 🏅

@lindapaiste lindapaiste added tests Label for unit testing related items. devops labels May 8, 2022
@lindapaiste lindapaiste merged commit 1f5f8b1 into ml5js:main May 8, 2022
@lindapaiste lindapaiste deleted the fix/jest-tests branch May 9, 2022 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devops tests Label for unit testing related items.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tests: update tests to use Jest - todo list
2 participants