@swimlane/cy-mockapi
Advanced tools
Comparing version 1.1.0 to 2.0.0
@@ -65,5 +65,13 @@ "use strict"; | ||
mocks.forEach((mock) => { | ||
cy.route(mock).as(mock.alias); | ||
cy.intercept(mock.matcher, (req) => { | ||
req.alias = mock.alias; | ||
if (mock.handler.redirect) { | ||
req.redirect(mock.handler.redirect, mock.handler.statusCode); | ||
} | ||
else { | ||
req.reply(mock.handler); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -43,57 +43,56 @@ "use strict"; | ||
const mockFiles = []; | ||
try { | ||
glob | ||
.sync(`**/${fileGlob}.${extGlobs}`, { cwd }) | ||
.forEach((path) => { | ||
const unescapedPath = path.replace(/__/g, '*').replace(/--/g, '?'); | ||
let { dir, name } = path_1.parse(unescapedPath); | ||
if (name.includes('.')) { | ||
const s = name.split('.'); | ||
dir += s[0]; | ||
name = s[1]; | ||
} | ||
const sp = name.split('-'); | ||
let method = sp[0]; | ||
const alt = sp[1]; | ||
method = method.toUpperCase(); | ||
const response = `fx:${slash_1.default(path_1.join(mocksFolder, path))}`; | ||
const url = slash_1.default(path_1.join(apiPath, dir)); | ||
const alias = alt ? `${method}:${dir}:${alt}` : `${method}:${dir}`; | ||
mockFiles.push({ | ||
alt, | ||
response, | ||
glob | ||
.sync(`**/${fileGlob}.${extGlobs}`, { cwd }) | ||
.forEach((path) => { | ||
const unescapedPath = path.replace(/__/g, '*').replace(/--/g, '?'); | ||
let { dir, name } = path_1.parse(unescapedPath); | ||
if (name.includes('.')) { | ||
const s = name.split('.'); | ||
dir += s[0]; | ||
name = s[1]; | ||
} | ||
const sp = name.split('-'); | ||
let method = sp[0]; | ||
const alt = sp[1]; | ||
method = method.toUpperCase(); | ||
const fixture = slash_1.default(path_1.join(mocksFolder, path)); | ||
const url = slash_1.default(path_1.join(apiPath, dir)); | ||
const alias = alt ? `${method}:${dir}:${alt}` : `${method}:${dir}`; | ||
mockFiles.push({ | ||
alt, | ||
alias, | ||
matcher: { | ||
method, | ||
url, | ||
method, | ||
alias, | ||
}); | ||
}, | ||
handler: { | ||
fixture, | ||
}, | ||
}); | ||
glob.sync('**/options.json', { cwd }).forEach((path) => { | ||
const raw = fs_1.readFileSync(path_1.join(cwd, path)); | ||
const opts = JSON.parse(String(raw)); | ||
const { dir } = path_1.parse(path); | ||
const dirEscaped = dir.replace(/__/g, '*'); | ||
opts.forEach((opt) => { | ||
opt.method = (opt.method || 'GET').toUpperCase(); | ||
if (typeof opt.response === 'undefined') { | ||
opt.response = `fx:${path_1.join(mocksFolder, dir, opt.method.toLowerCase())}`; | ||
} | ||
else if (typeof opt.response === 'string' && | ||
!opt.response.startsWith('fx:') && | ||
!opt.response.startsWith('fixture:')) { | ||
opt.response = `fx:${slash_1.default(path_1.join(mocksFolder, dir, opt.response))}`; | ||
} | ||
if (!(opt.url && opt.url.startsWith(apiPath))) { | ||
opt.url = path_1.join(apiPath, dirEscaped + (opt.url || '')); | ||
} | ||
opt.url = slash_1.default(opt.url); | ||
opt.alias = | ||
opt.alias || | ||
`${opt.method}:${opt.url.replace(slash_1.default(apiPath), '')}`; | ||
mockFiles.push(opt); | ||
}); | ||
}); | ||
glob.sync('**/options.json', { cwd }).forEach((path) => { | ||
const raw = fs_1.readFileSync(path_1.join(cwd, path)); | ||
const opts = JSON.parse(String(raw)); | ||
const { dir } = path_1.parse(path); | ||
const dirEscaped = dir.replace(/__/g, '*'); | ||
opts.forEach((opt) => { | ||
opt.matcher || (opt.matcher = {}); | ||
opt.handler || (opt.handler = {}); | ||
const { matcher, handler } = opt; | ||
matcher.method || (matcher.method = 'GET'); | ||
matcher.method = matcher.method.toUpperCase(); | ||
if (!(matcher.url && matcher.url.startsWith(apiPath))) { | ||
matcher.url = path_1.join(apiPath, dirEscaped + (matcher.url || '')); | ||
} | ||
matcher.url = slash_1.default(matcher.url); | ||
if (!handler.body && !handler.fixture) { | ||
handler.fixture = matcher.method.toLowerCase(); | ||
} | ||
if (handler.fixture) { | ||
handler.fixture = slash_1.default(path_1.join(mocksFolder, dir, handler.fixture)); | ||
} | ||
opt.alias || (opt.alias = `${matcher.method}:${matcher.url.replace(slash_1.default(apiPath), '')}`); | ||
mockFiles.push(opt); | ||
}); | ||
} | ||
catch (err) { | ||
// nop | ||
} | ||
}); | ||
mocksCache.set(mocksFolder, mockFiles); | ||
@@ -100,0 +99,0 @@ return mockFiles; |
@@ -65,5 +65,13 @@ "use strict"; | ||
mocks.forEach((mock) => { | ||
cy.route(mock).as(mock.alias); | ||
cy.intercept(mock.matcher, (req) => { | ||
req.alias = mock.alias; | ||
if (mock.handler.redirect) { | ||
req.redirect(mock.handler.redirect, mock.handler.statusCode); | ||
} | ||
else { | ||
req.reply(mock.handler); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -18,57 +18,56 @@ import { resolve, parse, join } from 'path'; | ||
const mockFiles = []; | ||
try { | ||
glob | ||
.sync(`**/${fileGlob}.${extGlobs}`, { cwd }) | ||
.forEach((path) => { | ||
const unescapedPath = path.replace(/__/g, '*').replace(/--/g, '?'); | ||
let { dir, name } = parse(unescapedPath); | ||
if (name.includes('.')) { | ||
const s = name.split('.'); | ||
dir += s[0]; | ||
name = s[1]; | ||
} | ||
const sp = name.split('-'); | ||
let method = sp[0]; | ||
const alt = sp[1]; | ||
method = method.toUpperCase(); | ||
const response = `fx:${slash(join(mocksFolder, path))}`; | ||
const url = slash(join(apiPath, dir)); | ||
const alias = alt ? `${method}:${dir}:${alt}` : `${method}:${dir}`; | ||
mockFiles.push({ | ||
alt, | ||
response, | ||
glob | ||
.sync(`**/${fileGlob}.${extGlobs}`, { cwd }) | ||
.forEach((path) => { | ||
const unescapedPath = path.replace(/__/g, '*').replace(/--/g, '?'); | ||
let { dir, name } = parse(unescapedPath); | ||
if (name.includes('.')) { | ||
const s = name.split('.'); | ||
dir += s[0]; | ||
name = s[1]; | ||
} | ||
const sp = name.split('-'); | ||
let method = sp[0]; | ||
const alt = sp[1]; | ||
method = method.toUpperCase(); | ||
const fixture = slash(join(mocksFolder, path)); | ||
const url = slash(join(apiPath, dir)); | ||
const alias = alt ? `${method}:${dir}:${alt}` : `${method}:${dir}`; | ||
mockFiles.push({ | ||
alt, | ||
alias, | ||
matcher: { | ||
method, | ||
url, | ||
method, | ||
alias, | ||
}); | ||
}, | ||
handler: { | ||
fixture, | ||
}, | ||
}); | ||
glob.sync('**/options.json', { cwd }).forEach((path) => { | ||
const raw = readFileSync(join(cwd, path)); | ||
const opts = JSON.parse(String(raw)); | ||
const { dir } = parse(path); | ||
const dirEscaped = dir.replace(/__/g, '*'); | ||
opts.forEach((opt) => { | ||
opt.method = (opt.method || 'GET').toUpperCase(); | ||
if (typeof opt.response === 'undefined') { | ||
opt.response = `fx:${join(mocksFolder, dir, opt.method.toLowerCase())}`; | ||
} | ||
else if (typeof opt.response === 'string' && | ||
!opt.response.startsWith('fx:') && | ||
!opt.response.startsWith('fixture:')) { | ||
opt.response = `fx:${slash(join(mocksFolder, dir, opt.response))}`; | ||
} | ||
if (!(opt.url && opt.url.startsWith(apiPath))) { | ||
opt.url = join(apiPath, dirEscaped + (opt.url || '')); | ||
} | ||
opt.url = slash(opt.url); | ||
opt.alias = | ||
opt.alias || | ||
`${opt.method}:${opt.url.replace(slash(apiPath), '')}`; | ||
mockFiles.push(opt); | ||
}); | ||
}); | ||
glob.sync('**/options.json', { cwd }).forEach((path) => { | ||
const raw = readFileSync(join(cwd, path)); | ||
const opts = JSON.parse(String(raw)); | ||
const { dir } = parse(path); | ||
const dirEscaped = dir.replace(/__/g, '*'); | ||
opts.forEach((opt) => { | ||
opt.matcher ||= {}; | ||
opt.handler ||= {}; | ||
const { matcher, handler } = opt; | ||
matcher.method ||= 'GET'; | ||
matcher.method = matcher.method.toUpperCase(); | ||
if (!(matcher.url && matcher.url.startsWith(apiPath))) { | ||
matcher.url = join(apiPath, dirEscaped + (matcher.url || '')); | ||
} | ||
matcher.url = slash(matcher.url); | ||
if (!handler.body && !handler.fixture) { | ||
handler.fixture = matcher.method.toLowerCase(); | ||
} | ||
if (handler.fixture) { | ||
handler.fixture = slash(join(mocksFolder, dir, handler.fixture)); | ||
} | ||
opt.alias ||= `${matcher.method}:${matcher.url.replace(slash(apiPath), '')}`; | ||
mockFiles.push(opt); | ||
}); | ||
} | ||
catch (err) { | ||
// nop | ||
} | ||
}); | ||
mocksCache.set(mocksFolder, mockFiles); | ||
@@ -75,0 +74,0 @@ return mockFiles; |
@@ -9,2 +9,5 @@ CHANGELOG | ||
## 2.0.0 (2021-04-20) | ||
- Use `cy.intercept` | ||
## 1.1.0 (2021-04-16) | ||
@@ -11,0 +14,0 @@ - Support windows paths |
{ | ||
"name": "@swimlane/cy-mockapi", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Easily mock your REST API in Cypress using fixtures", | ||
@@ -42,3 +42,3 @@ "main": "build/main/index.js", | ||
"chg": "^0.4.0", | ||
"cypress": "^6.4.0", | ||
"cypress": "^7.1.0", | ||
"eslint": "^7.14.0", | ||
@@ -45,0 +45,0 @@ "eslint-config-prettier": "^7.2.0", |
20013
343