Jelajahi Sumber

[improvement] Add assertion to verify using of new (#698)

* [improvement] Add assertion to verify using of new

I see people are asking about this problem so often, that
I feel it worth to put this assertion into the code to
save some time for those developers.

* [fix] Revert dist folder
Stanislav Termosa 8 tahun lalu
induk
melakukan
e0504cced2
2 mengubah file dengan 7 tambahan dan 0 penghapusan
  1. 1 0
      src/store.js
  2. 6 0
      test/unit/store.spec.js

+ 1 - 0
src/store.js

@@ -9,6 +9,7 @@ export class Store {
   constructor (options = {}) {
     assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`)
     assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)
+    assert(this instanceof Store, `Store must be called with the new operator.`)
 
     const {
       plugins = [],

+ 6 - 0
test/unit/store.spec.js

@@ -334,6 +334,12 @@ describe('Store', () => {
     })
   })
 
+  it('asserts the call with the new operator', () => {
+    expect(() => {
+      Vuex.Store({})
+    }).toThrowError(/Store must be called with the new operator/)
+  })
+
   it('should accept state as function', () => {
     const store = new Vuex.Store({
       state: () => ({