Ver código fonte

build: migrate tests to jest (#1738)

* build: migrate tests to jest

* build: remove babel object-rest-spread plugin

* chore: rename `.eslintrc` to `.eslintrc.json`

* chore: update `.eslintrc` in the test folder too
Kia King Ishii 5 anos atrás
pai
commit
dfacba6a95

+ 1 - 2
.babelrc

@@ -1,4 +1,3 @@
 {
-  "presets": ["env"],
-  "plugins": ["transform-object-rest-spread"]
+  "presets": ["@babel/preset-env"]
 }

+ 0 - 0
.eslintrc → .eslintrc.json


+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
+/coverage
 /docs/.vuepress/dist
 /examples/**/build.js
 /test/e2e/reports

+ 0 - 1
examples/chat/app.js

@@ -1,4 +1,3 @@
-import 'babel-polyfill'
 import Vue from 'vue'
 import App from './components/App.vue'
 import store from './store'

+ 0 - 1
examples/counter/app.js

@@ -1,4 +1,3 @@
-import 'babel-polyfill'
 import Vue from 'vue'
 import Counter from './Counter.vue'
 import store from './store'

+ 0 - 1
examples/shopping-cart/app.js

@@ -1,4 +1,3 @@
-import 'babel-polyfill'
 import Vue from 'vue'
 import App from './components/App.vue'
 import store from './store'

+ 0 - 1
examples/todomvc/app.js

@@ -1,4 +1,3 @@
-import 'babel-polyfill'
 import Vue from 'vue'
 import store from './store'
 import App from './components/App.vue'

+ 24 - 0
jest.config.js

@@ -0,0 +1,24 @@
+module.exports = {
+  rootDir: __dirname,
+  globals: {
+    __DEV__: true
+  },
+  moduleNameMapper: {
+    '^@/(.*)$': '<rootDir>/src/$1',
+    '^test/(.*)$': '<rootDir>/test/$1'
+  },
+  testMatch: ['<rootDir>/test/unit/**/*.spec.js'],
+  testPathIgnorePatterns: ['/node_modules/'],
+  setupFilesAfterEnv: [
+    './test/setup.js'
+  ],
+  "transform": {
+    "^.+\\.js$": "<rootDir>/node_modules/babel-jest"
+  },
+  coverageDirectory: 'coverage',
+  coverageReporters: ['json', 'lcov', 'text-summary', 'clover'],
+  collectCoverageFrom: [
+    'src/**/*.js',
+    '!src/index.cjs.js'
+  ]
+}

+ 9 - 9
package.json

@@ -21,10 +21,11 @@
     "build:logger": "node scripts/build-logger.js",
     "lint": "eslint src test",
     "test": "npm run lint && npm run test:types && npm run test:unit && npm run test:ssr && npm run test:e2e",
-    "test:unit": "jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
+    "test:unit": "jest --testPathIgnorePatterns test/e2e",
     "test:e2e": "node test/e2e/runner.js",
-    "test:ssr": "cross-env VUE_ENV=server jasmine JASMINE_CONFIG_PATH=test/unit/jasmine.json",
+    "test:ssr": "cross-env VUE_ENV=server jest --testPathIgnorePatterns test/e2e",
     "test:types": "tsc -p types/test",
+    "coverage": "jest --testPathIgnorePatterns test/e2e --coverage",
     "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
     "release": "node scripts/release.js",
     "docs": "vuepress dev docs",
@@ -44,16 +45,15 @@
     "vue": "^2.0.0"
   },
   "devDependencies": {
+    "@babel/core": "^7.9.0",
+    "@babel/preset-env": "^7.9.5",
     "@rollup/plugin-buble": "^0.21.3",
     "@rollup/plugin-commonjs": "^11.1.0",
     "@rollup/plugin-node-resolve": "^7.1.3",
     "@rollup/plugin-replace": "^2.3.2",
     "@types/node": "^13.13.2",
-    "babel-core": "^6.22.1",
-    "babel-loader": "^7.1.2",
-    "babel-plugin-transform-object-rest-spread": "^6.23.0",
-    "babel-polyfill": "^6.22.0",
-    "babel-preset-env": "^1.5.1",
+    "babel-jest": "^25.4.0",
+    "babel-loader": "^8.1.0",
     "brotli": "^1.3.2",
     "chalk": "^4.0.0",
     "chromedriver": "^80.0.1",
@@ -66,10 +66,10 @@
     "eslint-plugin-vue-libs": "^4.0.0",
     "execa": "^4.0.0",
     "express": "^4.17.1",
-    "jasmine": "2.8.0",
-    "jasmine-core": "2.8.0",
+    "jest": "^25.4.0",
     "nightwatch": "^1.3.1",
     "nightwatch-helpers": "^1.2.0",
+    "regenerator-runtime": "^0.13.5",
     "rollup": "^2.7.2",
     "rollup-plugin-terser": "^5.3.0",
     "semver": "^7.3.2",

+ 5 - 0
test/.eslintrc.json

@@ -0,0 +1,5 @@
+{
+  "env": {
+    "jest": true
+  }
+}

+ 5 - 0
test/setup.js

@@ -0,0 +1,5 @@
+import 'regenerator-runtime/runtime'
+import Vue from 'vue'
+import Vuex from '@/index'
+
+Vue.use(Vuex)

+ 0 - 5
test/unit/.eslintrc

@@ -1,5 +0,0 @@
-{
-  "env": {
-    "jasmine": true
-  }
-}

+ 17 - 17
test/unit/helpers.spec.js

@@ -95,7 +95,7 @@ describe('Helpers', () => {
   })
 
   it('mapState (with undefined states)', () => {
-    spyOn(console, 'error')
+    jest.spyOn(console, 'error').mockImplementation()
     const store = new Vuex.Store({
       modules: {
         foo: {
@@ -225,7 +225,7 @@ describe('Helpers', () => {
   })
 
   it('mapMutations (with undefined mutations)', () => {
-    spyOn(console, 'error')
+    jest.spyOn(console, 'error').mockImplementation()
     const store = new Vuex.Store({
       modules: {
         foo: {
@@ -393,7 +393,7 @@ describe('Helpers', () => {
   })
 
   it('mapGetters (with undefined getters)', () => {
-    spyOn(console, 'error')
+    jest.spyOn(console, 'error').mockImplementation()
     const store = new Vuex.Store({
       modules: {
         foo: {
@@ -420,8 +420,8 @@ describe('Helpers', () => {
   })
 
   it('mapActions (array)', () => {
-    const a = jasmine.createSpy()
-    const b = jasmine.createSpy()
+    const a = jest.fn()
+    const b = jest.fn()
     const store = new Vuex.Store({
       actions: {
         a,
@@ -440,8 +440,8 @@ describe('Helpers', () => {
   })
 
   it('mapActions (object)', () => {
-    const a = jasmine.createSpy()
-    const b = jasmine.createSpy()
+    const a = jest.fn()
+    const b = jest.fn()
     const store = new Vuex.Store({
       actions: {
         a,
@@ -463,7 +463,7 @@ describe('Helpers', () => {
   })
 
   it('mapActions (function)', () => {
-    const a = jasmine.createSpy()
+    const a = jest.fn()
     const store = new Vuex.Store({
       actions: { a }
     })
@@ -476,12 +476,12 @@ describe('Helpers', () => {
       })
     })
     vm.foo('foo')
-    expect(a.calls.argsFor(0)[1]).toBe('foobar')
+    expect(a.mock.calls[0][1]).toBe('foobar')
   })
 
   it('mapActions (with namespace)', () => {
-    const a = jasmine.createSpy()
-    const b = jasmine.createSpy()
+    const a = jest.fn()
+    const b = jest.fn()
     const store = new Vuex.Store({
       modules: {
         foo: {
@@ -508,7 +508,7 @@ describe('Helpers', () => {
   })
 
   it('mapActions (function with namespace)', () => {
-    const a = jasmine.createSpy()
+    const a = jest.fn()
     const store = new Vuex.Store({
       modules: {
         foo: {
@@ -526,12 +526,12 @@ describe('Helpers', () => {
       })
     })
     vm.foo('foo')
-    expect(a.calls.argsFor(0)[1]).toBe('foobar')
+    expect(a.mock.calls[0][1]).toBe('foobar')
   })
 
   it('mapActions (with undefined actions)', () => {
-    spyOn(console, 'error')
-    const a = jasmine.createSpy()
+    jest.spyOn(console, 'error').mockImplementation()
+    const a = jest.fn()
     const store = new Vuex.Store({
       modules: {
         foo: {
@@ -552,8 +552,8 @@ describe('Helpers', () => {
   })
 
   it('createNamespacedHelpers', () => {
-    const actionA = jasmine.createSpy()
-    const actionB = jasmine.createSpy()
+    const actionA = jest.fn()
+    const actionB = jest.fn()
     const store = new Vuex.Store({
       modules: {
         foo: {

+ 11 - 11
test/unit/hot-reload.spec.js

@@ -251,7 +251,7 @@ describe('Hot Reload', () => {
       }
     })
 
-    const spy = jasmine.createSpy()
+    const spy = jest.fn()
     const vm = new Vue({
       computed: {
         a: () => store.getters.count
@@ -292,7 +292,7 @@ describe('Hot Reload', () => {
   it('provide warning if a new module is given', () => {
     const store = new Vuex.Store({})
 
-    spyOn(console, 'warn')
+    jest.spyOn(console, 'warn').mockImplementation()
 
     store.hotUpdate({
       modules: {
@@ -312,10 +312,10 @@ describe('Hot Reload', () => {
 
   it('update namespace', () => {
     // prevent to print notification of unknown action/mutation
-    spyOn(console, 'error')
+    jest.spyOn(console, 'error').mockImplementation()
 
-    const actionSpy = jasmine.createSpy()
-    const mutationSpy = jasmine.createSpy()
+    const actionSpy = jest.fn()
+    const mutationSpy = jest.fn()
 
     const store = new Vuex.Store({
       modules: {
@@ -332,9 +332,9 @@ describe('Hot Reload', () => {
     expect(store.state.a.value).toBe(1)
     expect(store.getters['a/foo']).toBe(1)
     store.dispatch('a/foo')
-    expect(actionSpy.calls.count()).toBe(1)
+    expect(actionSpy).toHaveBeenCalledTimes(1)
     store.commit('a/foo')
-    expect(actionSpy.calls.count()).toBe(1)
+    expect(actionSpy).toHaveBeenCalledTimes(1)
 
     store.hotUpdate({
       modules: {
@@ -350,18 +350,18 @@ describe('Hot Reload', () => {
 
     // should not be called
     store.dispatch('a/foo')
-    expect(actionSpy.calls.count()).toBe(1)
+    expect(actionSpy).toHaveBeenCalledTimes(1)
 
     // should be called
     store.dispatch('foo')
-    expect(actionSpy.calls.count()).toBe(2)
+    expect(actionSpy).toHaveBeenCalledTimes(2)
 
     // should not be called
     store.commit('a/foo')
-    expect(mutationSpy.calls.count()).toBe(1)
+    expect(mutationSpy).toHaveBeenCalledTimes(1)
 
     // should be called
     store.commit('foo')
-    expect(mutationSpy.calls.count()).toBe(2)
+    expect(mutationSpy).toHaveBeenCalledTimes(2)
   })
 })

+ 0 - 10
test/unit/jasmine.json

@@ -1,10 +0,0 @@
-{
-  "spec_dir": "test/unit",
-  "spec_files": [
-    "**/*.spec.js"
-  ],
-  "helpers": [
-    "../../node_modules/babel-register/lib/node.js",
-    "setup.js"
-  ]
-}

+ 1 - 1
test/unit/module/module-collection.spec.js

@@ -1,4 +1,4 @@
-import ModuleCollection from '../../../src/module/module-collection'
+import ModuleCollection from '@/module/module-collection'
 
 describe('ModuleCollection', () => {
   it('get', () => {

+ 1 - 1
test/unit/module/module.spec.js

@@ -1,4 +1,4 @@
-import Module from '../../../src/module/module'
+import Module from '@/module/module'
 
 describe('Module', () => {
   it('get state', () => {

+ 26 - 26
test/unit/modules.spec.js

@@ -62,8 +62,8 @@ describe('Modules', () => {
           }
         }
       })
-      const actionSpy = jasmine.createSpy()
-      const mutationSpy = jasmine.createSpy()
+      const actionSpy = jest.fn()
+      const mutationSpy = jest.fn()
       store.registerModule(['a', 'b'], {
         state: { value: 1 },
         getters: { foo: state => state.value },
@@ -94,8 +94,8 @@ describe('Modules', () => {
     it('dynamic module registration preserving hydration', () => {
       const store = new Vuex.Store({})
       store.replaceState({ a: { foo: 'state' }})
-      const actionSpy = jasmine.createSpy()
-      const mutationSpy = jasmine.createSpy()
+      const actionSpy = jest.fn()
+      const mutationSpy = jest.fn()
       store.registerModule('a', {
         namespaced: true,
         getters: { foo: state => state.foo },
@@ -116,7 +116,7 @@ describe('Modules', () => {
 
   // #524
   it('should not fire an unrelated watcher', done => {
-    const spy = jasmine.createSpy()
+    const spy = jest.fn()
     const store = new Vuex.Store({
       modules: {
         a: {
@@ -349,8 +349,8 @@ describe('Modules', () => {
     })
 
     it('module: namespace', () => {
-      const actionSpy = jasmine.createSpy()
-      const mutationSpy = jasmine.createSpy()
+      const actionSpy = jest.fn()
+      const mutationSpy = jest.fn()
 
       const store = new Vuex.Store({
         modules: {
@@ -385,8 +385,8 @@ describe('Modules', () => {
       const actionSpys = []
       const mutationSpys = []
       const createModule = (name, namespaced, children) => {
-        const actionSpy = jasmine.createSpy()
-        const mutationSpy = jasmine.createSpy()
+        const actionSpy = jest.fn()
+        const mutationSpy = jest.fn()
 
         actionSpys.push(actionSpy)
         mutationSpys.push(mutationSpy)
@@ -435,7 +435,7 @@ describe('Modules', () => {
         store.dispatch(type)
       })
       actionSpys.forEach(spy => {
-        expect(spy.calls.count()).toBe(1)
+        expect(spy).toHaveBeenCalledTimes(1)
       })
 
       // mutations
@@ -443,7 +443,7 @@ describe('Modules', () => {
         store.commit(type)
       })
       mutationSpys.forEach(spy => {
-        expect(spy.calls.count()).toBe(1)
+        expect(spy).toHaveBeenCalledTimes(1)
       })
     })
 
@@ -472,10 +472,10 @@ describe('Modules', () => {
     })
 
     it('module: action context is namespaced in namespaced module', done => {
-      const rootActionSpy = jasmine.createSpy()
-      const rootMutationSpy = jasmine.createSpy()
-      const moduleActionSpy = jasmine.createSpy()
-      const moduleMutationSpy = jasmine.createSpy()
+      const rootActionSpy = jest.fn()
+      const rootMutationSpy = jest.fn()
+      const moduleActionSpy = jest.fn()
+      const moduleMutationSpy = jest.fn()
 
       const store = new Vuex.Store({
         state: { value: 'root' },
@@ -494,14 +494,14 @@ describe('Modules', () => {
                 expect(rootGetters.foo).toBe('root')
 
                 dispatch('foo')
-                expect(moduleActionSpy.calls.count()).toBe(1)
+                expect(moduleActionSpy).toHaveBeenCalledTimes(1)
                 dispatch('foo', null, { root: true })
-                expect(rootActionSpy.calls.count()).toBe(1)
+                expect(rootActionSpy).toHaveBeenCalledTimes(1)
 
                 commit('foo')
-                expect(moduleMutationSpy.calls.count()).toBe(1)
+                expect(moduleMutationSpy).toHaveBeenCalledTimes(1)
                 commit('foo', null, { root: true })
-                expect(rootMutationSpy.calls.count()).toBe(1)
+                expect(rootMutationSpy).toHaveBeenCalledTimes(1)
 
                 done()
               }
@@ -515,8 +515,8 @@ describe('Modules', () => {
     })
 
     it('module: use other module that has same namespace', done => {
-      const actionSpy = jasmine.createSpy()
-      const mutationSpy = jasmine.createSpy()
+      const actionSpy = jest.fn()
+      const mutationSpy = jest.fn()
 
       const store = new Vuex.Store({
         modules: {
@@ -558,7 +558,7 @@ describe('Modules', () => {
     })
 
     it('module: warn when module overrides state', () => {
-      spyOn(console, 'warn')
+      jest.spyOn(console, 'warn').mockImplementation()
       const store = new Vuex.Store({
         modules: {
           foo: {
@@ -662,9 +662,9 @@ describe('Modules', () => {
 
     it('plugins', function () {
       let initState
-      const actionSpy = jasmine.createSpy()
+      const actionSpy = jest.fn()
       const mutations = []
-      const subscribeActionSpy = jasmine.createSpy()
+      const subscribeActionSpy = jest.fn()
       const store = new Vuex.Store({
         state: {
           a: 1
@@ -702,8 +702,8 @@ describe('Modules', () => {
     })
 
     it('action before/after subscribers', (done) => {
-      const beforeSpy = jasmine.createSpy()
-      const afterSpy = jasmine.createSpy()
+      const beforeSpy = jest.fn()
+      const afterSpy = jest.fn()
       const store = new Vuex.Store({
         actions: {
           [TEST]: () => Promise.resolve()

+ 0 - 7
test/unit/setup.js

@@ -1,7 +0,0 @@
-import 'babel-polyfill'
-import Vue from 'vue'
-import Vuex from '../../src/index'
-
-Vue.use(Vuex)
-
-global.__DEV__ = true

+ 20 - 17
test/unit/store.spec.js

@@ -1,5 +1,5 @@
 import Vue from 'vue'
-import Vuex from '../../src/index'
+import Vuex from '@/index'
 
 const TEST = 'TEST'
 const isSSR = process.env.VUE_ENV === 'server'
@@ -171,11 +171,11 @@ describe('Store', () => {
         }
       }
     })
-    const spy = jasmine.createSpy()
+    const spy = jest.fn()
     store._devtoolHook = {
       emit: spy
     }
-    const thenSpy = jasmine.createSpy()
+    const thenSpy = jest.fn()
     store.dispatch(TEST)
       .then(thenSpy)
       .catch(err => {
@@ -249,7 +249,7 @@ describe('Store', () => {
   })
 
   it('should warn silent option depreciation', () => {
-    spyOn(console, 'warn')
+    jest.spyOn(console, 'warn').mockImplementation()
 
     const store = new Vuex.Store({
       mutations: {
@@ -287,7 +287,7 @@ describe('Store', () => {
   })
 
   it('should not call root state function twice', () => {
-    const spy = jasmine.createSpy().and.returnValue(1)
+    const spy = jest.fn().mockReturnValue(1)
     new Vuex.Store({
       state: spy
     })
@@ -295,8 +295,8 @@ describe('Store', () => {
   })
 
   it('subscribe: should handle subscriptions / unsubscriptions', () => {
-    const subscribeSpy = jasmine.createSpy()
-    const secondSubscribeSpy = jasmine.createSpy()
+    const subscribeSpy = jest.fn()
+    const secondSubscribeSpy = jest.fn()
     const testPayload = 2
     const store = new Vuex.Store({
       state: {},
@@ -316,12 +316,12 @@ describe('Store', () => {
       store.state
     )
     expect(secondSubscribeSpy).toHaveBeenCalled()
-    expect(subscribeSpy.calls.count()).toBe(1)
-    expect(secondSubscribeSpy.calls.count()).toBe(2)
+    expect(subscribeSpy).toHaveBeenCalledTimes(1)
+    expect(secondSubscribeSpy).toHaveBeenCalledTimes(2)
   })
 
   it('subscribe: should handle subscriptions with synchronous unsubscriptions', () => {
-    const subscribeSpy = jasmine.createSpy()
+    const subscribeSpy = jest.fn()
     const testPayload = 2
     const store = new Vuex.Store({
       state: {},
@@ -338,11 +338,11 @@ describe('Store', () => {
       { type: TEST, payload: testPayload },
       store.state
     )
-    expect(subscribeSpy.calls.count()).toBe(1)
+    expect(subscribeSpy).toHaveBeenCalledTimes(1)
   })
 
   it('subscribeAction: should handle subscriptions with synchronous unsubscriptions', () => {
-    const subscribeSpy = jasmine.createSpy()
+    const subscribeSpy = jest.fn()
     const testPayload = 2
     const store = new Vuex.Store({
       state: {},
@@ -359,12 +359,14 @@ describe('Store', () => {
       { type: TEST, payload: testPayload },
       store.state
     )
-    expect(subscribeSpy.calls.count()).toBe(1)
+    expect(subscribeSpy).toHaveBeenCalledTimes(1)
   })
 
   // store.watch should only be asserted in non-SSR environment
   if (!isSSR) {
     it('strict mode: warn mutations outside of handlers', () => {
+      const spy = jest.spyOn(console, 'error').mockImplementation()
+
       const store = new Vuex.Store({
         state: {
           a: 1
@@ -372,7 +374,8 @@ describe('Store', () => {
         strict: true
       })
       Vue.config.silent = true
-      expect(() => { store.state.a++ }).toThrow()
+      store.state.a++
+      expect(spy).toHaveBeenCalled()
       Vue.config.silent = false
     })
 
@@ -386,7 +389,7 @@ describe('Store', () => {
         }
       })
 
-      const spy = jasmine.createSpy()
+      const spy = jest.fn()
       store.watch(state => state.count, spy)
 
       // reset store vm
@@ -419,8 +422,8 @@ describe('Store', () => {
       const getter = function getter (state, getters) {
         return state.count
       }
-      const spy = spyOn({ getter }, 'getter').and.callThrough()
-      const spyCb = jasmine.createSpy()
+      const spy = jest.spyOn({ getter }, 'getter')
+      const spyCb = jest.fn()
 
       store.watch(spy, spyCb)
 

+ 1 - 1
test/unit/util.spec.js

@@ -1,4 +1,4 @@
-import { find, deepCopy, forEachValue, isObject, isPromise, assert } from '../../src/util'
+import { find, deepCopy, forEachValue, isObject, isPromise, assert } from '@/util'
 
 describe('util', () => {
   it('find', () => {

Diferenças do arquivo suprimidas por serem muito extensas
+ 566 - 3
yarn.lock


Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff