Mocking grecaptcha in tests
See original GitHub issueUsing 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:
- Created 6 years ago
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks. This will include in next release. I’ll close this issue.
@aphofstede Can you help me to test it? Just download and copy dist/vue-recaptcha.js to your project.