我已经使用vue-google-oauth2来实现Google登录。
我创建了一个名为“ Login.vue”的组件。
import GAuth from 'vue-google-oauth2'
const gauthOption = {
clientId:'xyz.apps.googleusercontent.com',
scope: 'profile email',
prompt: 'select_account'
}
Vue.use(GAuth, gauthOption)
现在可以使用gAuth进行登录了
this.$gAuth.signIn().then(GoogleUser => {
console.log(GoogleUser)
})
该程序包工作正常。
问题是当我尝试对组件进行单元测试时。我正在使用vue-jest进行测试。
当我运行'npm run test'时,出现以下错误:
FAIL test/unit/component/login-test.spec.js
? Test suite failed to run
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot
parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform
your files, ignoring "node_modules".
Here's what you can do:
To have some of your "node_modules" files transformed, you can specify a
custom "transformIgnorePatterns" in your config.
If you need a custom transformation specify a "transform" option in your
config.
If you simply want to mock your non-JS modules (e.g. binary assets) you
ca
n stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the
docs:
https://jestjs.io/docs/en/configuration.html
Details:
C:\rahul\vue-project1\node_modules\vue-google-oauth2\index.js:153
export default installGoogleAuthPlugin
^^^^^^
SyntaxError: Unexpected token export
25 | import Vue from 'vue'
26 | import Vuetify from 'vuetify'
> 27 | import GAuth from 'vue-google-oauth2'
| ^
28 |
29 | const gauthOption = {
30 | clientId:
at ScriptTransformer._transformAndBuildScript
(node_modules/@jest/transfor
m/build/ScriptTransformer.js:451:17) at ScriptTransformer.transform
(node_modules/@jest/transform/build/ScriptTransformer.js:493:19)
at src/components/Login.vue:27:1
at Object.<anonymous> (src/components/Login.vue:126:3)
Test Suites: 1 failed, 1 total
一旦我从“ vue-google-oauth2”中删除了“ import GAuth”,单元测试就可以了。
如何使单元测试生效?
将vue-google-oauth2用于vuejs应用程序的Google登录,并且由于这种开玩笑的单元测试框架而变形。