Testing
Extensions are essentially Node.js projects with some extra bits; meaning that the extensions can be tested in the same way.
You are free to use any testing framework and any testing methodology you wish.
|
DevNote provide some more examples for other frameworks? |
An example setup
/ .gitignore
/ .eslintrc.js
/ .mocharc.js
/ package.json
/ server-scripts
/ Sample.js
/ Sample.test.js
/ ...
/ client-scripts
/ ....
.vscode
node_modules
.nyc_output
coverage
yarn-error.log
module.exports = {
root: false,
env: {
node: true,
es6: true,
},
extends: [
'standard',
],
}
module.exports = {
require: [
'esm',
],
'full-trace': true,
bail: true,
recursive: true,
extension: ['.test.js'],
spec: [
'client-scripts/**/*.test.js',
'server-scripts/**/*.test.js',
],
'watch-files': [ 'src/**' ],
}
{
"scripts": {
"lint": "eslint {server-scripts,client-scripts}/**/* --ignore-pattern *.test.js",
"test:unit": "mocha",
"test:unit:cc": "nyc mocha"
},
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^6.8.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"esm": "^3.2.25",
"mocha": "^7.0.1",
"nyc": "^14.1.1",
"sinon": "^8.1.1"
},
"nyc": {
"all": true,
"reporter": [
"lcov",
"text"
],
"include": [
"client-scripts/**/*.js",
"server-scripts/**/*.js"
],
"exclude": [
"**/*.test.js"
],
"check-coverage": true,
"per-file": true,
"branches": 0,
"lines": 0,
"functions": 0,
"statements": 0
}
}
export default {
/* istanbul ignore next */
trigger ({ before }) {
return before('create')
},
exec () {
return 'Hello World!'
}
}
|
Note this part:
|
import { expect } from 'chai'
import Sample from './Sample'
describe(__filename, () => {
describe('Sample exec result', () => {
it('should return a string', () => {
expect(Sample.exec()).to.eq("HelloWorld")
})
})
})
package.json defines three scripts:-
lint: lint the code using the default ES6 standard (can be configured; see here), -
test:unit: unit test the code with your.test.jsfiles (can be configured in the.mocharc.jsfile), -
test:unit:cc: unit test the code and return a code coverage report.
|
The code coverage report gets generated into the |
|
Inspect the I usually use the http-server package to help with this, but a simple "Open in <browser name here>" will do.
|
|
DevNote: provide some complex examples using Sinon and promises. |