parse-community / parse-community/parse-server
Jest incompatability with `structuredClone` causing schema missmatches in tests
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 21.4k
- Forks
- 4.8k
- Avg merge
- 7h 45m
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Report security issues confidentially.
- Any contribution is under this license.
- Before posting search existing issues.
Issue Description
Since structuredClone was introduced in parse-server@8.6.44 jest test fail from field schema validation due to a known issue with jest globals differing from node globals and not handling instanceof correctly -> https://github.com/jestjs/jest/issues/2549.
Workaround
We've successfully replaced structuredClone in the jest environment with a local implementation as a workaround, but I believe the best thing to do would be to prefer Array.isArray over instanceof Array (and for all other data types ect) to avoid the problem entirely.
Steps to reproduce
Tests to reproduce:
describe('structuredClone instanceof Array', () => {
test('preserves instanceof Array after structuredClone', () => {
const original = [1, 2, 3]
const cloned = structuredClone(original)
expect(Array.isArray(original)).toBe(true) // true
expect(Array.isArray(cloned)).toBe(true) // true
expect(original instanceof Array).toBe(true) // true
expect(cloned instanceof Array).toBe(true) // false
})
test('adds an element to an array field on a Parse Object', async () => {
const entity = await new Parse.Object('SomeClass').save(
{ someField: ['value1', 'value2'] },
{ useMasterKey: true }
)
entity.addUnique('someField', 'value3')
await container.save(null, { useMasterKey: true })
const fetched = await new Parse.Query('SomeClass').get(entity.id, { useMasterKey: true })
expect(fetched.get('someField')).toEqual(['value1', 'value2', 'value3']) // throws "objects to add must be array"
})
})
Actual Outcome
The schema throws "objects to add must be array" incorrectly.
Expected Outcome
Schema to validate array as array
Environment
node@20.19.4
jest@29.6.2
parse-server@8.6.44
Server
- Parse Server version: 8.6.44
- Operating system: Mac OS
- Local or remote host (AWS, Azure, Google Cloud, Heroku, Digital Ocean, etc): local
Database
- System (MongoDB or Postgres): MongoDB
- Database version: 8
- Local or remote host (MongoDB Atlas, mLab, AWS, Azure, Google Cloud, etc): local
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproduced Jest tests and trace the schema validation reached by Parse.Object.addUnique after structuredClone. Confirm how array values are identified across the affected validation path, then run the reproduction and relevant schema tests; done means arrays cloned in Jest validate correctly without the false "objects to add must be array" error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js
- Domain
- backend-api-design, testing-qa
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100