Skip to content

Conversation

nowgnuesLeee
Copy link

Setting up Jest with jsdom environment and writing unit test code for Lottie Player

changes

  • I created a folder named tests and wrote Jest-related code.
  • Since we are using dynamic imports when importing the WASM module, we are using Node’s --experimental-vm-modules flag. ref.
var h = moduleArg, aa = "object" == typeof window, ba = "undefined" != typeof WorkerGlobalScope, ca = "object" == typeof process && process.versions?.node && "renderer" != process.type;
      if (ca) {
          const { createRequire: a } = await import('module');
  • The nodePolyfills() plugin generates polyfills that are not usable in the Node environment, so I added a build:node script that can build without it.
if (ca) {
          const { createRequire: a } = await Promise.resolve().then(function () { return _polyfillNode_module; });
...
  var _polyfillNode_module = /*#__PURE__*/Object.freeze({
    __proto__: null
  });

test

  • After running npm run build:node, please run npm run test.
  • I got TypeError cannot read properties of undefined reading refcount when calling term() in the test environment

Copy link

vercel bot commented Sep 10, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
thorvg-perf-test Ready Ready Preview Comment Sep 14, 2025 10:37am

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR sets up Jest testing environment with jsdom for the Lottie Player project, enabling unit tests for the web component functionality. The setup includes mocking of browser APIs, WASM module handling, and comprehensive test coverage for the LottiePlayer component.

Key changes:

  • Added Jest configuration with jsdom environment and Node.js ESM support
  • Created comprehensive test mocks for browser APIs (IntersectionObserver, fetch, Canvas APIs)
  • Implemented unit tests covering all major LottiePlayer methods and functionality
  • Added Node.js-specific build configuration to handle WASM polyfills properly

Reviewed Changes

Copilot reviewed 5 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/setup.ts Test environment setup with mocks for browser APIs and WASM module handling
tests/lottie-player.test.ts Comprehensive unit tests for LottiePlayer component methods
tests/fixtures/base-template.html HTML template for test DOM setup
rollup.config.js Added conditional Node.js build configuration to exclude polyfills
package.json Added Jest dependencies and test scripts with ESM support

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

package.json Outdated
"rollup-plugin-swc3": "^0.10.4",
"rollup-plugin-terser": "^7.0.2",
"typescript": "*"
"ts-jest": "^29.4.1",
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

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

The ts-jest dependency appears unused since the Jest configuration uses native Node.js ESM modules with --experimental-vm-modules flag instead of ts-jest transformation. Consider removing this dependency if it's not being used elsewhere.

Suggested change
"ts-jest": "^29.4.1",

Copilot uses AI. Check for mistakes.

@tinyjin tinyjin self-requested a review September 11, 2025 06:28
Copy link
Member

@tinyjin tinyjin left a comment

Choose a reason for hiding this comment

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

Thank you. Please check comments



(global as any).TextDecoder = TextDecoder;
(global as any).TextEncoder = TextEncoder;
Copy link
Member

Choose a reason for hiding this comment

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

Don't use any. If it's only option, please add comment.

package.json Outdated
"rollup-plugin-terser": "^7.0.2",
"typescript": "*"
"ts-jest": "^29.4.1",
"typescript": "^5.9.2"
Copy link
Member

Choose a reason for hiding this comment

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

No need to change existing dependency.

Copy link
Author

Choose a reason for hiding this comment

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

Sorry, this is my mistake. I'll fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

infrastructure Dev infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants