Skip to content

Commit

Permalink
feat(test): test command: now it is possible to run herbs tests and s…
Browse files Browse the repository at this point in the history
…pecs (aloe)
  • Loading branch information
dalssoft committed May 1, 2022
1 parent 9c2b8ae commit 771ca35
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 18 deletions.
97 changes: 88 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
"license": "MIT",
"dependencies": {
"git-user-name": "^2.0.0",
"glob": "^8.0.1",
"gluegun": "^5.1.2",
"inquirer": "^8.2.3",
"inquirer": "^8.2.4",
"update-notifier": "^5.1.0"
},
"devDependencies": {
Expand Down
7 changes: 3 additions & 4 deletions src/commands/herbs.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

const command = {
name: 'herbs',
run: async toolbox => {
const { print } = toolbox

print.info('Welcome to your Herbs CLI')
toolbox.print.info("Welcome to Herbs CLI 🌿")
toolbox.print.info("Type 'herbs help' for more information")
}

}

module.exports = command
6 changes: 3 additions & 3 deletions src/commands/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const questions = [

const cmd = {
name: 'new',
description: 'Create a new back-end structure based on pre defined entities',
description: 'Creates new Herbs project',
alias: ['n'],
run: async (toolbox) => {
let { options } = toolbox.parameters
Expand All @@ -97,8 +97,8 @@ const cmd = {
await generators[layer]()
}
const nextstep = `
A inital Herbs project was created! 🤩
You are ready to unlock your domain! 🌿
Your Herbs project has been created! 🌿
Are you ready to unlock your domain?
Next steps:
Expand Down
24 changes: 24 additions & 0 deletions src/commands/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const glob = require('glob')
const path = require('path')

const cmd = {
name: 'test',
description: 'Run Herbs spec tests',
alias: ['t'],
run: async toolbox => {

const files = glob.sync('./**/*.aloe.test.js')
for (const file of files) {
const instance = require(path.resolve(file))
if (instance.isSpec) {
const ret = await instance.run()
console.log(ret, instance)
}
}
toolbox.print.success('Test finished! 🤩')
}
}



module.exports = cmd
2 changes: 1 addition & 1 deletion src/commands/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const generator = require('../generators')

const cmd = {
name: 'update',
description: 'Create a all layers for new entity(ies) added into project',
description: 'Updates and create files based on the entities and use cases',
alias: ['u'],
run: async toolbox => {
const generators = (await generator(toolbox)).update
Expand Down
28 changes: 28 additions & 0 deletions src/generators/src/domain/usecases/unitTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,33 @@ module.exports = async ({ template: { generate }, filesystem }, command) => asyn
process.stdout.write(` New: ${ucPath}\n`)

})

// aloe
useCases.map(async (action) => {
const useCaseName = `${action} ${name}`
const ucPath = path.normalize(`${filesystem.cwd()}/src/domain/usecases/${camelCase(name)}/${camelCase(useCaseName)}.aloe.test.js`)

if (fs.existsSync(ucPath)) return

const objOptions = { spaces: 4, extraSpaces: 4, removeBraces: true }
await generate({
template: `domain/useCases/tests/${action}.aloe.test.ejs`,
target: ucPath,
props: {
name: {
pascalCase: name,
camelCase: camelCase(name),
raw: camelCase(name).replace(/([a-z0-9])([A-Z])/g, '$1 $2')
},
request: {
valid: objToString(generateRequestObject(schema, action), objOptions),
invalid: objToString(generateRequestObject(schema, action, false), objOptions)
},
mock: objToString(generateMockObj(schema), objOptions)
}
})
process.stdout.write(` New: ${ucPath}\n`)

})
}
}
49 changes: 49 additions & 0 deletions src/templates/domain/useCases/tests/create.aloe.test.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const create<%- props.name.pascalCase %> = require('./create<%- props.name.pascalCase %>')
const assert = require('assert')
const { spec, scenario, given, check } = require('@herbsjs/aloe')
const { herbarium } = require('@herbsjs/herbarium')

const create<%- props.name.pascalCase %>Spec = spec({
usecase: create<%- props.name.pascalCase %>,
'Create a new <%- props.name.raw %> when it is valid': scenario({
'Given a simple <%- props.name.raw %> is valid': given({
request: {
<%- props.request.valid %>
},
user: { hasAccess: true },
injection: {
<%- props.name.camelCase %>Repository: new ( class <%- props.name.pascalCase %>Repository {
async insert(<%- props.name.camelCase %>) { return (<%- props.name.camelCase %>) }
})
},
}),

// when: default when for use case

'Must return Ok': check((ctx) => {
assert.ok(ctx.response.isOk)
}),

'Must return a valid <%- props.name.raw %>': check((ctx) => {
assert.strictEqual(ctx.response.ok.isValid(), true)
// TODO: check if it is really a <%- props.name.raw %>
})

}),

// 'Do not create a new <%- props.name.raw %> when it is invalid': scenario({})
// const injection = {}
// const req = {
// <%- props.request.invalid %>
// }
// assert.ok(ret.isErr)
// assert.ok(ret.isInvalidEntityError)
})

module.exports = create<%- props.name.pascalCase %>Spec

//module.exports =
// herbarium.specs
// .add(create<%- props.name.pascalCase %>Spec, 'Create<%- props.name.pascalCase %>Spec')
// .metadata({ usecase: 'Create<%- props.name.pascalCase %>' })
// .usecase
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 771ca35

Please sign in to comment.