Skip to content

Suggestion: Add info about using Cypress to test JS that has no UI. #1109

Open
@flowt-au

Description

@flowt-au

Suggestion:

I am looking at using Cypress to test modules / functions that have no UI. Much of the code is legacy - i.e. uses functions, not exports, so there needs to be a way to "import" the function into the step_definition from the "global" name space.

eg the following silly simple example:

    // app.js in http://127.0.0.1:8500/Sites/CypressTestApp/index.html

    var myFunc = {
        lastMsg : '',
        say: function(msg){
            console.log('Last msg: '+this.lastMsg)
            console.log('You said: '+msg)
            this.lastMsg = msg;
        },
        getLastMsg : function(){
            return this.lastMsg
        }
    }

It took me a while to find this issue post that answered the question: How do I test the function directly without reference to the DOM? Answer below.

    describe('The Home Page', function () {

        it('successfully loads', function () {
            cy.visit('http://127.0.0.1:8500/Sites/CypressTestApp')
            cy.window().should('have.property', 'myFunc')
        })
        
        it('Should set name to Jane',function(){

            cy.window().then(function(win){
                win.myFunc.say('Fred')
                win.myFunc.say('Jane')
                var lm = win.myFunc.getLastMsg()
                expect(lm).to.equal("Jane")
            })

        })
    })

Thanks!

Metadata

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