Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
flowt-au opened this issue Nov 2, 2018 · 2 comments
Open

Comments

@flowt-au
Copy link

flowt-au commented Nov 2, 2018

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!

@JohnLukeBentley
Copy link

I strongly endorse this suggestion, for the documentation, too.

My version of the question (and answer with help from my friends) is at: https://stackoverflow.com/questions/72137229/can-we-directly-unit-test-html-embedded-javascript-functions

Testing html embedded, top level, javascript functions was first thing I was looking for a web tester to do. I became wonderfully seduced into Cypress once I installed it and saw it's a Selenium replacement in being able to do some user like UI testing. However, I was some significant way into the documentation, admiring the way Cypress hangs together, but all the while scratching my head on how to do this basic, first, thing.

So indeed I suggest giving prominent place to this basic thing. For example, under "Getting Started" create a new page "Writing JavaScript Unit Tests" with two sections:

  • (The suggestion in this thread) Test Javascript functions that are coded in the top level of a <script>; and
  • Test JavaScript functions coming from a separate .js file, are ES6/ES2015 export/imported (or CommonJS required), and used in a <script type="module">;

@flowt-au
Copy link
Author

flowt-au commented May 6, 2022

I support that too. Testing the Business Logic layer (all JS classes and a message bus etc, no ui, no selenium etc etc). It was the first thing I did. It took a while to work out a pattern for doing that from Gherkin => test reports. Still not completely happy with the outcome but it works.

Cheers,
Murray

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

No branches or pull requests

3 participants