Skip to content

Jest ignoring browser field in package.json of a module dependency sub-directory #4756

@jasonmacdonald

Description

@jasonmacdonald

bug

What is the current behavior?

I'm using a package called Markojs, this package makes heavy use of the "browser" field in package.json - not just at the root, but throughout the module src hierarchy. Jest does not abide by these file remaps in sub directories, causing errors when trying to test Marko components with Jest. Everything works fine in Node, as the resolver is able to properly resolve the remapped files.

I am setting the "browser" flag on Jest to true, and it appears to be carrying through the proper code execution in Jest. But, I believe the issue is that the sync.js file in Jest, on line 24, first tries to load the file as a file, which passes, so the loadAsDirectorySync function never fires, and that appears to be the only place where the package.json browser file remap is checked. Since it's a file in a sub directory, and the file does exists (however is not the correct one) the browser package logic is never checked in that directory.

For more details, the first file that I am encountering such a failure on is in the https://github.com/marko-js/marko/tree/master/src/components directory on the utils.js file, which as you can see in that directories package.json file should be remapped to the utils-browser.js file.

What is the expected behavior?

Files should be resolved to the files specified in the browser field, regardless of where the package.json lives

Please provide your exact Jest configuration and mention your Jest, node, yarn/npm version and operating system.
Jest 20.0.4
Node v8.6.0
Yarn 1.1.0

 "jest": {
        "coveragePathIgnorePatterns": [
            "/node_modules/",
            "/mocks/",
            "<rootDir>/scripts/test.setup.js"
        ],
        "coverageThreshold": {
            "global": {
                "branches": 0,
                "functions": 0,
                "lines": 0,
                "statements": 0
            }
        },
        "bail": true,
        "browser": true,
        "setupFiles": [
            "<rootDir>/scripts/test/setup.js"
        ],
        "testPathIgnorePatterns": [
            "/mocks/"
        ],
        "moduleFileExtensions": [
            "js"
        ],
        "moduleDirectories": [
            "node_modules"
        ],
        "moduleNameMapper": {
            "\\.(jpg|jpeg|png|svg|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/scripts/test/fileMock.js",
            "\\.(css|scss|sass)$": "identity-obj-proxy"
        },
        "transform": {
            "^.+\\.js$": "babel-jest",
            "^.+\\.(html|marko)$": "<rootDir>/scripts/test/marko.js"
        },
        "transformIgnorePatterns": [
            "node_modules/(?!@lwc-.+)"
        ]
    },

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions