简体   繁体   中英

can't browserify file - “Error: Parsing file” (HTML5 checkValidity() ?)

I want to see if I can test form field validation using mocha & chai.

I have the following files:

./src/index.html :

<!doctype html>
<html>
    <head>
        <title>TDD Project</title>
        <style>
            #container {width: 600px; max-width: 100%; margin: 1em auto;}
        </style>
        <link rel="stylesheet" href="../node_modules/mocha/mocha.css">
    </head>
    <body>
        <div id="container">
            <form>
                <input type="text" id="text" placeholder="enter some text" required maxlength="10" pattern="^[a-z,A-Z]{1,10}$"><br />
                <input type="text" id="number" placeholder="enter a number" required maxlength="10" pattern="\d{10}"><br />
                <input type="submit">
            </form>
        </div>
        <div id="mocha"></div>
        <script src="../node_modules/mocha/mocha.js"></script>
        <script src="../node_modules/chai/chai.js"></script>
        <script>mocha.setup('bdd')</script>

        <script src="../test/test2.js"></script>

        <script>
          mocha.run();
        </script>        
    </body>
</html>

./test/test.js :

var assert = require("chai").assert;

function FormController() {
    function isValidField(fieldID){
        field = document.getElementById(fieldID);
        return(field.checkValidity() = true ? true : false);
    }
    return {
        isValidField
    }
}

describe("Simple assert", function() {
    it("foo != bar", function() {
        assert('foo' != 'bar', 'foo is not bar');
    });
    it('should return true if field is valid', function(){
        var isValidText = FormController.isValidField(text);
        var isValidNumber = FormController.isValidField(number);
        assert.equal(isValidText, true);
        assert.equal(isValidNumber, true);
    });
})

I have run the commands > cd test

> browserify test.js > test2.js

I receive the error:

Error: Parsing file /Users/user/Documents/Projects/Contact_Form/test/test.js: Assigning to rvalue (6:15)

The problem appears to be with field.checkValidity() but I don't understand why.

Is it because the file test.js cannot access the DOM?

checkValidity() is a "HTML5 constraint validation API" discussed on this page .

Help appreciated.

From this answer :

You get rvalue error, when you use = instead of == in a condition checking block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM