question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocking grecaptcha in tests

See original GitHub issue

Using Vue-test-utils and Sinon, I’m mocking the execute() function in my contact form tests, so I can bypass the actual validation, but I suspect some things remain in global scope; grecaptchaMock.execute.called only succeeds the first time. Any examples of usage in tests would be greatly appreciated.

import { mount } from 'vue-test-utils';
import ContactForm from '../assets/js/components/ContactForm.vue';
import expect from 'expect';
import Vue from 'vue'
import moxios from 'moxios'
import sinon from 'sinon'
import { equal } from 'assert'

// ... snip

describe ('Contact Form', () => {
    let wrapper, grecaptchaMock, widgetId;

    beforeEach(() => {
        // Ajax stub
        moxios.install()

        // reCaptcha stub
        widgetId = "WidgetId"
        grecaptchaMock = sinon.stub({
            render: function (options) { },
            reset: function (id) { },
            getResponse: function (id) { },
            execute: function() { }
        })
        grecaptchaMock.render.returns(widgetId)
        grecaptchaMock.execute.callsFake(function fakeFn() {
            wrapper.vm.onVerify('testtoken')
        })
        window.grecaptcha = grecaptchaMock

        // Mount the Vue component
        wrapper = mount(ContactForm)
    });
    afterEach(() => {
        // reCaptcha stub
        grecaptchaMock = null
        widgetId = null
        delete window.grecaptcha;

        // Ajax stub
        moxios.uninstall()
    });

// ... snip

    it ('should have errors if input was omitted', (done) => {

// ... snip (moxios setup)

        sinon.spy(wrapper.vm, "onVerify")

        // When the user submits the for without input
        wrapper.find('form').trigger('submit')

        // Then the execute flow should follow
        expect(grecaptchaMock.execute.called).toBe(true); // XXX only works if no tests were run before this
        expect(wrapper.vm.onVerify.called).toBe(true); // XXX seems to work every time

// ... snip

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
DanSnowcommented, Nov 19, 2017

Thanks. This will include in next release. I’ll close this issue.

1reaction
DanSnowcommented, Nov 18, 2017

@aphofstede Can you help me to test it? Just download and copy dist/vue-recaptcha.js to your project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Account for Google reCaptcha in Jest Unit Test with ...
This will mock the reset and execute function in the library. The execute function will always pass test-v2-token at place of v2 captcha...
Read more >
How to write Unit Test and Integration Test to reCAPTCHA?
https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha-v2-what-should-i-do.
Read more >
mock recaptcha in RegisterController
The problem that i have is that i can't mock the Recaptcha class. In order to do that, i have to pass the...
Read more >
Google reCAPTCHA test
Google reCAPTCHA test. Adds the vanilla reCAPTCHA widget, for testing...
Read more >
Can I test a register page protected by Google ReCaptcha?
To write unit tests specifically, since recaptcha relies on querying an external service, a good option would be to mock that outgoing request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found