Skip to content

[Bug]: jsdom test environment fails require of json file on alterschema library #14623

Open
@luiz00martins

Description

Version

29.7.0

Steps to reproduce

  1. Clone my repo
  2. Run npm install
  3. Run npm test

Expected behavior

The function alterschema is asynchronous. Therefore, when these two tests run:

const alterschema = require('alterschema');

describe('Test', function() {
	const schema = {
		foo: {
			type: 'string',
			required: true,
		},
	};

	it('should fail schema', async () => {
		expect(alterschema(schema, 'draft4', 'draft6')).toEqual(schema);
	});

	it('should succeed schema', async () => {
		expect(await alterschema(schema, 'draft4', 'draft6')).toEqual(schema);
	});
});

I expect 'should fail schema' one to fail, as Promise will not equal the given object. I also expect 'should succeed schema' to succeed, as the JSON objects match.

Actual behavior

However, what happens is that it crashes:

C:\Users\luizh\Local\jest-test\node_modules\jest-runtime\build\index.js:1016
        this._environment.global.JSON.parse(transformedFile);
                                 ^

TypeError: Cannot read properties of null (reading 'JSON')
    at Runtime._loadModule (C:\Users\luizh\Local\jest-test\node_modules\jest-runtime\build\index.js:1016:34)
    at Runtime.requireModule (C:\Users\luizh\Local\jest-test\node_modules\jest-runtime\build\index.js:882:12)
    at Runtime.requireModuleOrMock (C:\Users\luizh\Local\jest-test\node_modules\jest-runtime\build\index.js:1048:21)
    at Object.<anonymous>.module.exports (C:\Users\luizh\Local\jest-test\node_modules\alterschema\bindings\node\walker.js:6:19)
    at Object.<anonymous>.module.exports (C:\Users\luizh\Local\jest-test\node_modules\alterschema\bindings\node\index.js:71:20)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v18.17.0

If the first test gets commented, the second one succeeds as expected. However, if the first test is present, jest crashes before the second one is run.

Additional context

I'm using jest-environment-jsdom, with testEnvironment: "jsdom".

The only reason I'm posting this bug, is because I wasted a bunch of time trying to find what the actual issue was, i.e. the missing await. Usually you'll just get a failed test, as the Promise object won't match the passed one, and you'll realize you forgot an await. On this one however, I has to spend a couple days trying to decrypt in which part of my environment/code the problem was. So even if it's unsolvable/my problem/won't fix, I hope somebody else finds the issue useful.

Environment

System:
    OS: Windows 10 10.0.19045
    CPU: (12) x64 AMD Ryzen 5 5600G with Radeon Graphics
  Binaries:
    Node: 18.17.0 - ~\scoop\apps\nvm\current\nodejs\nodejs\node.EXE
    Yarn: 1.22.19 - ~\scoop\apps\yarn\current\bin\yarn.CMD
    npm: 9.8.1 - ~\scoop\apps\nvm\current\nodejs\nodejs\npm.CMD
    pnpm: 8.6.10 - ~\AppData\Local\pnpm\pnpm.CMD
  npmPackages:
    jest: ^29.7.0 => 29.7.0

Activity

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

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions