refactor: change mock files to commonjs

This commit is contained in:
花裤衩 2020-06-15 17:07:49 +08:00
parent efc976ae5a
commit 5c87772025
6 changed files with 37 additions and 15 deletions

View File

@ -1,8 +1,8 @@
import Mock from 'mockjs'
import { param2Obj } from '../src/utils'
const Mock = require('mockjs')
const { param2Obj } = require('./utils')
import user from './user'
import table from './table'
const user = require('./user')
const table = require('./table')
const mocks = [
...user,
@ -12,7 +12,7 @@ const mocks = [
// for front mock
// please use it cautiously, it will redefine XMLHttpRequest,
// which will cause many of your third-party libraries to be invalidated(like progress event).
export function mockXHR() {
function mockXHR() {
// mock patch
// https://github.com/nuysoft/Mock/issues/300
Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send
@ -50,4 +50,8 @@ export function mockXHR() {
}
}
export default mocks
module.exports = {
mocks,
mockXHR
}

View File

@ -8,7 +8,7 @@ const mockDir = path.join(process.cwd(), 'mock')
function registerRoutes(app) {
let mockLastIndex
const { default: mocks } = require('./index.js')
const { mocks } = require('./index.js')
const mocksForServer = mocks.map(route => {
return responseFake(route.url, route.type, route.response)
})
@ -44,9 +44,6 @@ const responseFake = (url, type, respond) => {
}
module.exports = app => {
// es6 polyfill
require('@babel/register')
// parse app.body
// https://expressjs.com/en/4x/api.html#req.body
app.use(bodyParser.json())

View File

@ -1,4 +1,4 @@
import Mock from 'mockjs'
const Mock = require('mockjs')
const data = Mock.mock({
'items|30': [{
@ -11,7 +11,7 @@ const data = Mock.mock({
}]
})
export default [
module.exports = [
{
url: '/vue-admin-template/table/list',
type: 'get',

View File

@ -23,7 +23,7 @@ const users = {
}
}
export default [
module.exports = [
// user login
{
url: '/vue-admin-template/user/login',

23
mock/utils.js Normal file
View File

@ -0,0 +1,23 @@
/**
* @param {string} url
* @returns {Object}
*/
function param2Obj(url) {
const search = url.split('?')[1]
if (!search) {
return {}
}
return JSON.parse(
'{"' +
decodeURIComponent(search)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"')
.replace(/\+/g, ' ') +
'"}'
)
}
module.exports = {
param2Obj
}

View File

@ -26,8 +26,6 @@
"vuex": "3.1.0"
},
"devDependencies": {
"@babel/core": "7.0.0",
"@babel/register": "7.0.0",
"@vue/cli-plugin-babel": "3.6.0",
"@vue/cli-plugin-eslint": "^3.9.1",
"@vue/cli-plugin-unit-jest": "3.6.3",